From 84dd22f7320ea755fb70e1618f2d3e70618a4265 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?William=20B=C3=A9rub=C3=A9?= Date: Sat, 14 Sep 2024 02:13:37 -0400 Subject: [PATCH] Reverting to selects for video threads - Edge-triggered epolls, for the most part, do not respond correctly to frame transfers - For the sake of simplicity, especially with such a low file descriptor count to scan, we can live with the tiny penalty --- src/hal/hisi/v1_hal.c | 70 +++++++++++++++++++----------------- src/hal/hisi/v1_hal.h | 2 +- src/hal/hisi/v2_hal.c | 69 +++++++++++++++++++----------------- src/hal/hisi/v2_hal.h | 2 +- src/hal/hisi/v3_hal.c | 68 ++++++++++++++++++----------------- src/hal/hisi/v3_hal.h | 2 +- src/hal/hisi/v4_hal.c | 68 ++++++++++++++++++----------------- src/hal/hisi/v4_hal.h | 2 +- src/hal/inge/t31_hal.c | 80 ++++++++++++++++++++---------------------- src/hal/inge/t31_hal.h | 2 +- src/hal/plus/cvi_hal.c | 68 ++++++++++++++++++----------------- src/hal/plus/cvi_hal.h | 2 +- src/hal/star/i3_hal.c | 70 +++++++++++++++++++----------------- src/hal/star/i3_hal.h | 2 +- src/hal/star/i6_hal.c | 70 +++++++++++++++++++----------------- src/hal/star/i6_hal.h | 2 +- src/hal/star/i6c_hal.c | 70 +++++++++++++++++++----------------- src/hal/star/i6c_hal.h | 2 +- src/hal/star/i6f_hal.c | 71 +++++++++++++++++++------------------ src/hal/star/i6f_hal.h | 2 +- 20 files changed, 375 insertions(+), 349 deletions(-) diff --git a/src/hal/hisi/v1_hal.c b/src/hal/hisi/v1_hal.c index 5892979..9c9d9db 100644 --- a/src/hal/hisi/v1_hal.c +++ b/src/hal/hisi/v1_hal.c @@ -591,9 +591,7 @@ void v1_video_request_idr(char index) int v1_video_snapshot_grab(char index, hal_jpegdata *jpeg) { - int ret, epollEvt; - struct epoll_event events[5], event = { .events = EPOLLIN|EPOLLET }; - int epollFd = epoll_create1(0); + int ret; if (ret = v1_channel_bind(index)) { HAL_DANGER("v1_venc", "Binding the encoder channel " @@ -609,15 +607,21 @@ int v1_video_snapshot_grab(char index, hal_jpegdata *jpeg) } int fd = v1_venc.fnGetDescriptor(index); - event.data.fd = fd; - if (ret = epoll_ctl(epollFd, EPOLL_CTL_ADD, fd, &event)) { - HAL_DANGER("v1_venc", "Adding the encoder descriptor to " - "the polling set failed with %#x!\n", ret); + + struct timeval timeout = { .tv_sec = 2, .tv_usec = 0 }; + fd_set readFds; + FD_ZERO(&readFds); + FD_SET(fd, &readFds); + ret = select(fd + 1, &readFds, NULL, NULL, &timeout); + if (ret < 0) { + HAL_DANGER("v1_venc", "Select operation failed!\n"); + goto abort; + } else if (ret == 0) { + HAL_DANGER("v1_venc", "Capture stream timed out!\n"); goto abort; } - epollEvt = epoll_wait(epollFd, events, 5, 2000); - for (int e = 0; e < epollEvt; e++) { + if (FD_ISSET(fd, &readFds)) { v1_venc_stat stat; if (ret = v1_venc.fnQuery(index, &stat)) { HAL_DANGER("v1_venc", "Querying the encoder channel " @@ -668,9 +672,6 @@ int v1_video_snapshot_grab(char index, hal_jpegdata *jpeg) v1_venc.fnFreeStream(index, &strm); } - if (close(epollFd)) - HAL_DANGER("v1_venc", "Closing the polling descriptor failed!\n"); - v1_venc.fnStopReceiving(index); v1_channel_unbind(index); @@ -680,14 +681,7 @@ int v1_video_snapshot_grab(char index, hal_jpegdata *jpeg) void *v1_video_thread(void) { - int ret, epollEvt; - struct epoll_event events[5], event = { .events = EPOLLIN|EPOLLET }; - int epollFd = epoll_create1(0); - - if (epollFd < 0) { - HAL_DANGER("v1_venc", "Creating the polling descriptor failed!\n"); - return (void*)0; - } + int ret, maxFd = 0; for (int i = 0; i < V1_VENC_CHN_NUM; i++) { if (!v1_state[i].enable) continue; @@ -699,24 +693,38 @@ void *v1_video_thread(void) return (void*)0; } v1_state[i].fileDesc = ret; - event.data.fd = ret; - if (ret = epoll_ctl(epollFd, EPOLL_CTL_ADD, ret, &event)) { - HAL_DANGER("v1_venc", "Adding the encoder descriptor to " - "the polling set failed with %#x!\n", ret); - return (void*)0; - } + + if (maxFd <= v1_state[i].fileDesc) + maxFd = v1_state[i].fileDesc; } v1_venc_stat stat; v1_venc_strm stream; + struct timeval timeout; + fd_set readFds; while (keepRunning) { - epollEvt = epoll_wait(epollFd, events, 5, 2000); - for (int e = 0; e < epollEvt; e++) { + FD_ZERO(&readFds); + for(int i = 0; i < V1_VENC_CHN_NUM; i++) { + if (!v1_state[i].enable) continue; + if (!v1_state[i].mainLoop) continue; + FD_SET(v1_state[i].fileDesc, &readFds); + } + + timeout.tv_sec = 2; + timeout.tv_usec = 0; + ret = select(maxFd + 1, &readFds, NULL, NULL, &timeout); + if (ret < 0) { + HAL_DANGER("v1_venc", "Select operation failed!\n"); + break; + } else if (ret == 0) { + HAL_WARNING("v1_venc", "Main stream loop timed out!\n"); + continue; + } else { for (int i = 0; i < V1_VENC_CHN_NUM; i++) { if (!v1_state[i].enable) continue; if (!v1_state[i].mainLoop) continue; - if (v1_state[i].fileDesc == events[e].data.fd) { + if (FD_ISSET(v1_state[i].fileDesc, &readFds)) { memset(&stream, 0, sizeof(stream)); if (ret = v1_venc.fnQuery(i, &stat)) { @@ -774,15 +782,11 @@ void *v1_video_thread(void) } free(stream.packet); stream.packet = NULL; - break; } } } } - if (close(epollFd)) - HAL_DANGER("v1_venc", "Closing the polling descriptor failed!\n"); - HAL_INFO("v1_venc", "Shutting down encoding thread...\n"); } diff --git a/src/hal/hisi/v1_hal.h b/src/hal/hisi/v1_hal.h index 4ecfe22..6e8b43f 100644 --- a/src/hal/hisi/v1_hal.h +++ b/src/hal/hisi/v1_hal.h @@ -13,8 +13,8 @@ #include "v1_vpss.h" #include -#include #include +#include #include extern char keepRunning; diff --git a/src/hal/hisi/v2_hal.c b/src/hal/hisi/v2_hal.c index db4727a..c6bd9f0 100644 --- a/src/hal/hisi/v2_hal.c +++ b/src/hal/hisi/v2_hal.c @@ -634,9 +634,7 @@ void v2_video_request_idr(char index) int v2_video_snapshot_grab(char index, hal_jpegdata *jpeg) { - int ret, epollEvt; - struct epoll_event events[5], event = { .events = EPOLLIN|EPOLLET }; - int epollFd = epoll_create1(0); + int ret; if (ret = v2_channel_bind(index)) { HAL_DANGER("v2_venc", "Binding the encoder channel " @@ -652,15 +650,21 @@ int v2_video_snapshot_grab(char index, hal_jpegdata *jpeg) } int fd = v2_venc.fnGetDescriptor(index); - event.data.fd = fd; - if (ret = epoll_ctl(epollFd, EPOLL_CTL_ADD, fd, &event)) { - HAL_DANGER("v2_venc", "Adding the encoder descriptor to " - "the polling set failed with %#x!\n", ret); + + struct timeval timeout = { .tv_sec = 2, .tv_usec = 0 }; + fd_set readFds; + FD_ZERO(&readFds); + FD_SET(fd, &readFds); + ret = select(fd + 1, &readFds, NULL, NULL, &timeout); + if (ret < 0) { + HAL_DANGER("v2_venc", "Select operation failed!\n"); + goto abort; + } else if (ret == 0) { + HAL_DANGER("v2_venc", "Capture stream timed out!\n"); goto abort; } - epollEvt = epoll_wait(epollFd, events, 5, 2000); - for (int e = 0; e < epollEvt; e++) { + if (FD_ISSET(fd, &readFds)) { v2_venc_stat stat; if (ret = v2_venc.fnQuery(index, &stat)) { HAL_DANGER("v2_venc", "Querying the encoder channel " @@ -711,9 +715,6 @@ int v2_video_snapshot_grab(char index, hal_jpegdata *jpeg) v2_venc.fnFreeStream(index, &strm); } - if (close(epollFd)) - HAL_DANGER("v2_venc", "Closing the polling descriptor failed!\n"); - v2_venc.fnFreeDescriptor(index); v2_venc.fnStopReceiving(index); @@ -725,14 +726,7 @@ int v2_video_snapshot_grab(char index, hal_jpegdata *jpeg) void *v2_video_thread(void) { - int ret, epollEvt; - struct epoll_event events[5], event = { .events = EPOLLIN|EPOLLET }; - int epollFd = epoll_create1(0); - - if (epollFd < 0) { - HAL_DANGER("v2_venc", "Creating the polling descriptor failed!\n"); - return (void*)0; - } + int ret, maxFd = 0; for (int i = 0; i < V2_VENC_CHN_NUM; i++) { if (!v2_state[i].enable) continue; @@ -744,12 +738,10 @@ void *v2_video_thread(void) return (void*)0; } v2_state[i].fileDesc = ret; - event.data.fd = ret; - if (ret = epoll_ctl(epollFd, EPOLL_CTL_ADD, ret, &event)) { - HAL_DANGER("v2_venc", "Adding the encoder descriptor to " - "the polling set failed with %#x!\n", ret); - return (void*)0; - } + + + if (maxFd <= v2_state[i].fileDesc) + maxFd = v2_state[i].fileDesc; } v2_venc_stat stat; @@ -758,12 +750,27 @@ void *v2_video_thread(void) fd_set readFds; while (keepRunning) { - epollEvt = epoll_wait(epollFd, events, 5, 2000); - for (int e = 0; e < epollEvt; e++) { + FD_ZERO(&readFds); + for(int i = 0; i < V2_VENC_CHN_NUM; i++) { + if (!v2_state[i].enable) continue; + if (!v2_state[i].mainLoop) continue; + FD_SET(v2_state[i].fileDesc, &readFds); + } + + timeout.tv_sec = 2; + timeout.tv_usec = 0; + ret = select(maxFd + 1, &readFds, NULL, NULL, &timeout); + if (ret < 0) { + HAL_DANGER("v2_venc", "Select operation failed!\n"); + break; + } else if (ret == 0) { + HAL_WARNING("v2_venc", "Main stream loop timed out!\n"); + continue; + } else { for (int i = 0; i < V2_VENC_CHN_NUM; i++) { if (!v2_state[i].enable) continue; if (!v2_state[i].mainLoop) continue; - if (v2_state[i].fileDesc == events[e].data.fd) { + if (FD_ISSET(v2_state[i].fileDesc, &readFds)) { memset(&stream, 0, sizeof(stream)); if (ret = v2_venc.fnQuery(i, &stat)) { @@ -824,15 +831,11 @@ void *v2_video_thread(void) } free(stream.packet); stream.packet = NULL; - break; } } } } - if (close(epollFd)) - HAL_DANGER("v2_venc", "Closing the polling descriptor failed!\n"); - HAL_INFO("v2_venc", "Shutting down encoding thread...\n"); } diff --git a/src/hal/hisi/v2_hal.h b/src/hal/hisi/v2_hal.h index 8057e98..80b42f7 100644 --- a/src/hal/hisi/v2_hal.h +++ b/src/hal/hisi/v2_hal.h @@ -13,8 +13,8 @@ #include "v2_vpss.h" #include -#include #include +#include #include extern char keepRunning; diff --git a/src/hal/hisi/v3_hal.c b/src/hal/hisi/v3_hal.c index 277ba21..0e64f82 100644 --- a/src/hal/hisi/v3_hal.c +++ b/src/hal/hisi/v3_hal.c @@ -653,9 +653,7 @@ void v3_video_request_idr(char index) int v3_video_snapshot_grab(char index, hal_jpegdata *jpeg) { - int ret, epollEvt; - struct epoll_event events[5], event = { .events = EPOLLIN|EPOLLET }; - int epollFd = epoll_create1(0); + int ret; if (ret = v3_channel_bind(index)) { HAL_DANGER("v3_venc", "Binding the encoder channel " @@ -671,15 +669,21 @@ int v3_video_snapshot_grab(char index, hal_jpegdata *jpeg) } int fd = v3_venc.fnGetDescriptor(index); - event.data.fd = fd; - if (ret = epoll_ctl(epollFd, EPOLL_CTL_ADD, fd, &event)) { - HAL_DANGER("i6_venc", "Adding the encoder descriptor to " - "the polling set failed with %#x!\n", ret); + + struct timeval timeout = { .tv_sec = 2, .tv_usec = 0 }; + fd_set readFds; + FD_ZERO(&readFds); + FD_SET(fd, &readFds); + ret = select(fd + 1, &readFds, NULL, NULL, &timeout); + if (ret < 0) { + HAL_DANGER("v3_venc", "Select operation failed!\n"); + goto abort; + } else if (ret == 0) { + HAL_DANGER("v3_venc", "Capture stream timed out!\n"); goto abort; } - epollEvt = epoll_wait(epollFd, events, 5, 2000); - for (int e = 0; e < epollEvt; e++) { + if (FD_ISSET(fd, &readFds)) { v3_venc_stat stat; if (ret = v3_venc.fnQuery(index, &stat)) { HAL_DANGER("v3_venc", "Querying the encoder channel " @@ -730,9 +734,6 @@ int v3_video_snapshot_grab(char index, hal_jpegdata *jpeg) v3_venc.fnFreeStream(index, &strm); } - if (close(epollFd)) - HAL_DANGER("v3_venc", "Closing the polling descriptor failed!\n"); - v3_venc.fnFreeDescriptor(index); v3_venc.fnStopReceiving(index); @@ -744,14 +745,7 @@ int v3_video_snapshot_grab(char index, hal_jpegdata *jpeg) void *v3_video_thread(void) { - int ret, epollEvt; - struct epoll_event events[5], event = { .events = EPOLLIN|EPOLLET }; - int epollFd = epoll_create1(0); - - if (epollFd < 0) { - HAL_DANGER("v3_venc", "Creating the polling descriptor failed!\n"); - return (void*)0; - } + int ret, maxFd = 0; for (int i = 0; i < V3_VENC_CHN_NUM; i++) { if (!v3_state[i].enable) continue; @@ -763,12 +757,9 @@ void *v3_video_thread(void) return (void*)0; } v3_state[i].fileDesc = ret; - event.data.fd = ret; - if (ret = epoll_ctl(epollFd, EPOLL_CTL_ADD, ret, &event)) { - HAL_DANGER("v3_venc", "Adding the encoder descriptor to " - "the polling set failed with %#x!\n", ret); - return (void*)0; - } + + if (maxFd <= v3_state[i].fileDesc) + maxFd = v3_state[i].fileDesc; } v3_venc_stat stat; @@ -777,12 +768,27 @@ void *v3_video_thread(void) fd_set readFds; while (keepRunning) { - epollEvt = epoll_wait(epollFd, events, 5, 2000); - for (int e = 0; e < epollEvt; e++) { + FD_ZERO(&readFds); + for(int i = 0; i < V3_VENC_CHN_NUM; i++) { + if (!v3_state[i].enable) continue; + if (!v3_state[i].mainLoop) continue; + FD_SET(v3_state[i].fileDesc, &readFds); + } + + timeout.tv_sec = 2; + timeout.tv_usec = 0; + ret = select(maxFd + 1, &readFds, NULL, NULL, &timeout); + if (ret < 0) { + HAL_DANGER("v3_venc", "Select operation failed!\n"); + break; + } else if (ret == 0) { + HAL_WARNING("v3_venc", "Main stream loop timed out!\n"); + continue; + } else { for (int i = 0; i < V3_VENC_CHN_NUM; i++) { if (!v3_state[i].enable) continue; if (!v3_state[i].mainLoop) continue; - if (v3_state[i].fileDesc == events[e].data.fd) { + if (FD_ISSET(v3_state[i].fileDesc, &readFds)) { memset(&stream, 0, sizeof(stream)); if (ret = v3_venc.fnQuery(i, &stat)) { @@ -843,15 +849,11 @@ void *v3_video_thread(void) } free(stream.packet); stream.packet = NULL; - break; } } } } - if (close(epollFd)) - HAL_DANGER("v3_venc", "Closing the polling descriptor failed!\n"); - HAL_INFO("v3_venc", "Shutting down encoding thread...\n"); } diff --git a/src/hal/hisi/v3_hal.h b/src/hal/hisi/v3_hal.h index f616d4a..127e227 100644 --- a/src/hal/hisi/v3_hal.h +++ b/src/hal/hisi/v3_hal.h @@ -13,8 +13,8 @@ #include "v3_vpss.h" #include -#include #include +#include #include extern char keepRunning; diff --git a/src/hal/hisi/v4_hal.c b/src/hal/hisi/v4_hal.c index 53aeec7..76aa0c2 100644 --- a/src/hal/hisi/v4_hal.c +++ b/src/hal/hisi/v4_hal.c @@ -745,9 +745,7 @@ void v4_video_request_idr(char index) int v4_video_snapshot_grab(char index, hal_jpegdata *jpeg) { - int ret, epollEvt; - struct epoll_event events[5], event = { .events = EPOLLIN|EPOLLET }; - int epollFd = epoll_create1(0); + int ret; if (ret = v4_channel_bind(index)) { HAL_DANGER("v4_venc", "Binding the encoder channel " @@ -763,15 +761,21 @@ int v4_video_snapshot_grab(char index, hal_jpegdata *jpeg) } int fd = v4_venc.fnGetDescriptor(index); - event.data.fd = fd; - if (ret = epoll_ctl(epollFd, EPOLL_CTL_ADD, fd, &event)) { - HAL_DANGER("v4_venc", "Adding the encoder descriptor to " - "the polling set failed with %#x!\n", ret); + + struct timeval timeout = { .tv_sec = 2, .tv_usec = 0 }; + fd_set readFds; + FD_ZERO(&readFds); + FD_SET(fd, &readFds); + ret = select(fd + 1, &readFds, NULL, NULL, &timeout); + if (ret < 0) { + HAL_DANGER("v4_venc", "Select operation failed!\n"); + goto abort; + } else if (ret == 0) { + HAL_DANGER("v4_venc", "Capture stream timed out!\n"); goto abort; } - epollEvt = epoll_wait(epollFd, events, 5, 2000); - for (int e = 0; e < epollEvt; e++) { + if (FD_ISSET(fd, &readFds)) { v4_venc_stat stat; if (ret = v4_venc.fnQuery(index, &stat)) { HAL_DANGER("v4_venc", "Querying the encoder channel " @@ -822,9 +826,6 @@ int v4_video_snapshot_grab(char index, hal_jpegdata *jpeg) v4_venc.fnFreeStream(index, &strm); } - if (close(epollFd)) - HAL_DANGER("v4_venc", "Closing the polling descriptor failed!\n"); - v4_venc.fnFreeDescriptor(index); v4_venc.fnStopReceiving(index); @@ -836,14 +837,7 @@ int v4_video_snapshot_grab(char index, hal_jpegdata *jpeg) void *v4_video_thread(void) { - int ret, epollEvt; - struct epoll_event events[5], event = { .events = EPOLLIN|EPOLLET }; - int epollFd = epoll_create1(0); - - if (epollFd < 0) { - HAL_DANGER("v4_venc", "Creating the polling descriptor failed!\n"); - return (void*)0; - } + int ret, maxFd = 0; for (int i = 0; i < V4_VENC_CHN_NUM; i++) { if (!v4_state[i].enable) continue; @@ -855,12 +849,9 @@ void *v4_video_thread(void) return (void*)0; } v4_state[i].fileDesc = ret; - event.data.fd = ret; - if (ret = epoll_ctl(epollFd, EPOLL_CTL_ADD, ret, &event)) { - HAL_DANGER("v4_venc", "Adding the encoder descriptor to " - "the polling set failed with %#x!\n", ret); - return (void*)0; - } + + if (maxFd <= v4_state[i].fileDesc) + maxFd = v4_state[i].fileDesc; } v4_venc_stat stat; @@ -869,12 +860,27 @@ void *v4_video_thread(void) fd_set readFds; while (keepRunning) { - epollEvt = epoll_wait(epollFd, events, 5, 2000); - for (int e = 0; e < epollEvt; e++) { + FD_ZERO(&readFds); + for(int i = 0; i < V4_VENC_CHN_NUM; i++) { + if (!v4_state[i].enable) continue; + if (!v4_state[i].mainLoop) continue; + FD_SET(v4_state[i].fileDesc, &readFds); + } + + timeout.tv_sec = 2; + timeout.tv_usec = 0; + ret = select(maxFd + 1, &readFds, NULL, NULL, &timeout); + if (ret < 0) { + HAL_DANGER("v4_venc", "Select operation failed!\n"); + break; + } else if (ret == 0) { + HAL_WARNING("v4_venc", "Main stream loop timed out!\n"); + continue; + } else { for (int i = 0; i < V4_VENC_CHN_NUM; i++) { if (!v4_state[i].enable) continue; if (!v4_state[i].mainLoop) continue; - if (v4_state[i].fileDesc == events[e].data.fd) { + if (FD_ISSET(v4_state[i].fileDesc, &readFds)) { memset(&stream, 0, sizeof(stream)); if (ret = v4_venc.fnQuery(i, &stat)) { @@ -935,15 +941,11 @@ void *v4_video_thread(void) } free(stream.packet); stream.packet = NULL; - break; } } } } - if (close(epollFd)) - HAL_DANGER("v4_venc", "Closing the polling descriptor failed!\n"); - HAL_INFO("v4_venc", "Shutting down encoding thread...\n"); } diff --git a/src/hal/hisi/v4_hal.h b/src/hal/hisi/v4_hal.h index deaf152..68cc36b 100644 --- a/src/hal/hisi/v4_hal.h +++ b/src/hal/hisi/v4_hal.h @@ -15,8 +15,8 @@ #include "../support.h" #include -#include #include +#include #include extern char keepRunning; diff --git a/src/hal/inge/t31_hal.c b/src/hal/inge/t31_hal.c index 6fe99c7..1a1984c 100644 --- a/src/hal/inge/t31_hal.c +++ b/src/hal/inge/t31_hal.c @@ -462,9 +462,7 @@ void t31_video_request_idr(char index) int t31_video_snapshot_grab(char index, hal_jpegdata *jpeg) { - int ret, epollEvt; - struct epoll_event events[5], event = { .events = EPOLLIN|EPOLLET }; - int epollFd = epoll_create1(0); + int ret, fd; char mjpeg = 0; if (index == -1) { @@ -473,12 +471,7 @@ int t31_video_snapshot_grab(char index, hal_jpegdata *jpeg) for (char i = 0; i < T31_VENC_CHN_NUM; i++) { if (!t31_state[i].enable) continue; if (t31_state[i].payload != HAL_VIDCODEC_MJPG) continue; - event.data.fd = t31_state[i].fileDesc; - if (ret = epoll_ctl(epollFd, EPOLL_CTL_ADD, event.data.fd, &event)) { - HAL_DANGER("i6_venc", "Adding the encoder descriptor to " - "the polling set failed with %#x!\n", ret); - return EXIT_FAILURE; - } + fd = t31_state[i].fileDesc; index = i; mjpeg = 1; } @@ -498,16 +491,23 @@ int t31_video_snapshot_grab(char index, hal_jpegdata *jpeg) goto abort; } - event.data.fd = t31_venc.fnGetDescriptor(index); - if (ret = epoll_ctl(epollFd, EPOLL_CTL_ADD, event.data.fd, &event)) { - HAL_DANGER("i6_venc", "Adding the encoder descriptor to " - "the polling set failed with %#x!\n", ret); - goto abort; - } + fd = t31_venc.fnGetDescriptor(index); } - epollEvt = epoll_wait(epollFd, events, 5, 2000); - for (int e = 0; e < epollEvt; e++) { + struct timeval timeout = { .tv_sec = 2, .tv_usec = 0 }; + fd_set readFds; + FD_ZERO(&readFds); + FD_SET(fd, &readFds); + ret = select(fd + 1, &readFds, NULL, NULL, &timeout); + if (ret < 0) { + HAL_DANGER("t31_venc", "Select operation failed!\n"); + goto abort; + } else if (ret == 0) { + HAL_DANGER("t31_venc", "Capture stream timed out!\n"); + goto abort; + } + + if (FD_ISSET(fd, &readFds)) { t31_venc_stat stat; if (ret = t31_venc.fnQuery(index, &stat)) { HAL_DANGER("t31_venc", "Querying the encoder channel " @@ -558,9 +558,6 @@ int t31_video_snapshot_grab(char index, hal_jpegdata *jpeg) t31_venc.fnFreeStream(index, &strm); } - if (close(epollFd)) - HAL_DANGER("t31_venc", "Closing the polling descriptor failed!\n"); - if (!mjpeg) { t31_venc.fnStopReceiving(index); @@ -572,14 +569,7 @@ int t31_video_snapshot_grab(char index, hal_jpegdata *jpeg) void *t31_video_thread(void) { - int ret, epollEvt; - struct epoll_event events[5], event = { .events = EPOLLIN|EPOLLET }; - int epollFd = epoll_create1(0); - - if (epollFd < 0) { - HAL_DANGER("t31_venc", "Creating the polling descriptor failed!\n"); - return (void*)0; - } + int ret, maxFd = 0; for (int i = 0; i < T31_VENC_CHN_NUM; i++) { if (!t31_state[i].enable) continue; @@ -591,12 +581,9 @@ void *t31_video_thread(void) return NULL; } t31_state[i].fileDesc = ret; - event.data.fd = ret; - if (ret = epoll_ctl(epollFd, EPOLL_CTL_ADD, ret, &event)) { - HAL_DANGER("t31_venc", "Adding the encoder descriptor to " - "the polling set failed with %#x!\n", ret); - return (void*)0; - } + + if (maxFd <= t31_state[i].fileDesc) + maxFd = t31_state[i].fileDesc; } t31_venc_stat stat; @@ -604,12 +591,27 @@ void *t31_video_thread(void) fd_set readFds; while (keepRunning) { - epollEvt = epoll_wait(epollFd, events, 5, 2000); - for (int e = 0; e < epollEvt; e++) { + FD_ZERO(&readFds); + for(int i = 0; i < T31_VENC_CHN_NUM; i++) { + if (!t31_state[i].enable) continue; + if (!t31_state[i].mainLoop) continue; + FD_SET(t31_state[i].fileDesc, &readFds); + } + + timeout.tv_sec = 2; + timeout.tv_usec = 0; + ret = select(maxFd + 1, &readFds, NULL, NULL, &timeout); + if (ret < 0) { + HAL_DANGER("t31_venc", "Select operation failed!\n"); + break; + } else if (ret == 0) { + HAL_WARNING("t31_venc", "Main stream loop timed out!\n"); + continue; + } else { for (int i = 0; i < T31_VENC_CHN_NUM; i++) { if (!t31_state[i].enable) continue; if (!t31_state[i].mainLoop) continue; - if (t31_state[i].fileDesc == events[e].data.fd) { + if (FD_ISSET(t31_state[i].fileDesc, &readFds)) { t31_venc_strm stream; if (ret = t31_venc.fnGetStream(i, &stream, 0)) { @@ -657,15 +659,11 @@ void *t31_video_thread(void) HAL_DANGER("t31_venc", "Releasing the stream on " "channel %d failed with %#x!\n", i, ret); } - break; } } } } - if (close(epollFd)) - HAL_DANGER("t31_venc", "Closing the polling descriptor failed!\n"); - HAL_INFO("t31_venc", "Shutting down encoding thread...\n"); } diff --git a/src/hal/inge/t31_hal.h b/src/hal/inge/t31_hal.h index d038867..9c1a185 100644 --- a/src/hal/inge/t31_hal.h +++ b/src/hal/inge/t31_hal.h @@ -10,7 +10,7 @@ #include "../config.h" -#include +#include #include extern char keepRunning; diff --git a/src/hal/plus/cvi_hal.c b/src/hal/plus/cvi_hal.c index 80e2e49..1826a66 100644 --- a/src/hal/plus/cvi_hal.c +++ b/src/hal/plus/cvi_hal.c @@ -645,9 +645,7 @@ void cvi_video_request_idr(char index) int cvi_video_snapshot_grab(char index, hal_jpegdata *jpeg) { - int ret, epollEvt; - struct epoll_event events[5], event = { .events = EPOLLIN|EPOLLET }; - int epollFd = epoll_create1(0); + int ret; if (ret = cvi_channel_bind(index)) { HAL_DANGER("cvi_venc", "Binding the encoder channel " @@ -663,15 +661,21 @@ int cvi_video_snapshot_grab(char index, hal_jpegdata *jpeg) } int fd = cvi_venc.fnGetDescriptor(index); - event.data.fd = fd; - if (ret = epoll_ctl(epollFd, EPOLL_CTL_ADD, fd, &event)) { - HAL_DANGER("cvi_venc", "Adding the encoder descriptor to " - "the polling set failed with %#x!\n", ret); + + struct timeval timeout = { .tv_sec = 2, .tv_usec = 0 }; + fd_set readFds; + FD_ZERO(&readFds); + FD_SET(fd, &readFds); + ret = select(fd + 1, &readFds, NULL, NULL, &timeout); + if (ret < 0) { + HAL_DANGER("cvi_venc", "Select operation failed!\n"); + goto abort; + } else if (ret == 0) { + HAL_DANGER("cvi_venc", "Capture stream timed out!\n"); goto abort; } - epollEvt = epoll_wait(epollFd, events, 5, 2000); - for (int e = 0; e < epollEvt; e++) { + if (FD_ISSET(fd, &readFds)) { cvi_venc_stat stat; if (cvi_venc.fnQuery(index, &stat)) { HAL_DANGER("cvi_venc", "Querying the encoder channel " @@ -722,9 +726,6 @@ int cvi_video_snapshot_grab(char index, hal_jpegdata *jpeg) cvi_venc.fnFreeStream(index, &strm); } - if (close(epollFd)) - HAL_DANGER("cvi_venc", "Closing the polling descriptor failed!\n"); - cvi_venc.fnFreeDescriptor(index); cvi_venc.fnStopReceiving(index); @@ -736,14 +737,7 @@ int cvi_video_snapshot_grab(char index, hal_jpegdata *jpeg) void *cvi_video_thread(void) { - int ret, epollEvt; - struct epoll_event events[5], event = { .events = EPOLLIN|EPOLLET }; - int epollFd = epoll_create1(0); - - if (epollFd < 0) { - HAL_DANGER("cvi_venc", "Creating the polling descriptor failed!\n"); - return (void*)0; - } + int ret, maxFd = 0; for (int i = 0; i < CVI_VENC_CHN_NUM; i++) { if (!cvi_state[i].enable) continue; @@ -755,12 +749,9 @@ void *cvi_video_thread(void) return (void*)0; } cvi_state[i].fileDesc = ret; - event.data.fd = ret; - if (ret = epoll_ctl(epollFd, EPOLL_CTL_ADD, ret, &event)) { - HAL_DANGER("cvi_venc", "Adding the encoder descriptor to " - "the polling set failed with %#x!\n", ret); - return (void*)0; - } + + if (maxFd <= cvi_state[i].fileDesc) + maxFd = cvi_state[i].fileDesc; } cvi_venc_stat stat; @@ -769,12 +760,27 @@ void *cvi_video_thread(void) fd_set readFds; while (keepRunning) { - epollEvt = epoll_wait(epollFd, events, 5, 2000); - for (int e = 0; e < epollEvt; e++) { + FD_ZERO(&readFds); + for(int i = 0; i < CVI_VENC_CHN_NUM; i++) { + if (!cvi_state[i].enable) continue; + if (!cvi_state[i].mainLoop) continue; + FD_SET(cvi_state[i].fileDesc, &readFds); + } + + timeout.tv_sec = 2; + timeout.tv_usec = 0; + ret = select(maxFd + 1, &readFds, NULL, NULL, &timeout); + if (ret < 0) { + HAL_DANGER("cvi_venc", "Select operation failed!\n"); + break; + } else if (ret == 0) { + HAL_WARNING("cvi_venc", "Main stream loop timed out!\n"); + continue; + } else { for (int i = 0; i < CVI_VENC_CHN_NUM; i++) { if (!cvi_state[i].enable) continue; if (!cvi_state[i].mainLoop) continue; - if (cvi_state[i].fileDesc == events[e].data.fd) { + if (FD_ISSET(cvi_state[i].fileDesc, &readFds)) { memset(&stream, 0, sizeof(stream)); if (ret = cvi_venc.fnQuery(i, &stat)) { @@ -835,14 +841,10 @@ void *cvi_video_thread(void) } free(stream.packet); stream.packet = NULL; - break; } } } } - - if (close(epollFd)) - HAL_DANGER("cvi_venc", "Closing the polling descriptor failed!\n"); HAL_INFO("cvi_venc", "Shutting down encoding thread...\n"); } diff --git a/src/hal/plus/cvi_hal.h b/src/hal/plus/cvi_hal.h index 67fdd2a..e35c370 100644 --- a/src/hal/plus/cvi_hal.h +++ b/src/hal/plus/cvi_hal.h @@ -12,7 +12,7 @@ #include "cvi_vi.h" #include "cvi_vpss.h" -#include +#include #include extern char keepRunning; diff --git a/src/hal/star/i3_hal.c b/src/hal/star/i3_hal.c index b42301c..8dfbda7 100644 --- a/src/hal/star/i3_hal.c +++ b/src/hal/star/i3_hal.c @@ -575,9 +575,7 @@ void i3_video_request_idr(char index) int i3_video_snapshot_grab(char index, char quality, hal_jpegdata *jpeg) { - int ret, epollEvt; - struct epoll_event events[5], event = { .events = EPOLLIN|EPOLLET }; - int epollFd = epoll_create1(0); + int ret; if (ret = i3_channel_bind(index, 1)) { HAL_DANGER("i3_venc", "Binding the encoder channel " @@ -608,15 +606,21 @@ int i3_video_snapshot_grab(char index, char quality, hal_jpegdata *jpeg) } int fd = i3_venc.fnGetDescriptor(index); - event.data.fd = fd; - if (ret = epoll_ctl(epollFd, EPOLL_CTL_ADD, fd, &event)) { - HAL_DANGER("i3_venc", "Adding the encoder descriptor to " - "the polling set failed with %#x!\n", ret); + + struct timeval timeout = { .tv_sec = 2, .tv_usec = 0 }; + fd_set readFds; + FD_ZERO(&readFds); + FD_SET(fd, &readFds); + ret = select(fd + 1, &readFds, NULL, NULL, &timeout); + if (ret < 0) { + HAL_DANGER("i3_venc", "Select operation failed!\n"); + goto abort; + } else if (ret == 0) { + HAL_DANGER("i3_venc", "Capture stream timed out!\n"); goto abort; } - epollEvt = epoll_wait(epollFd, events, 5, 2000); - for (int e = 0; e < epollEvt; e++) { + if (FD_ISSET(fd, &readFds)) { i3_venc_stat stat; if (ret = i3_venc.fnQuery(index, &stat)) { HAL_DANGER("i3_venc", "Querying the encoder channel " @@ -667,9 +671,6 @@ int i3_video_snapshot_grab(char index, char quality, hal_jpegdata *jpeg) i3_venc.fnFreeStream(index, &strm); } - if (close(epollFd)) - HAL_DANGER("i3_venc", "Closing the polling descriptor failed!\n"); - i3_venc.fnStopReceiving(index); i3_channel_unbind(index); @@ -679,14 +680,7 @@ int i3_video_snapshot_grab(char index, char quality, hal_jpegdata *jpeg) void *i3_video_thread(void) { - int ret, epollEvt; - struct epoll_event events[5], event = { .events = EPOLLIN|EPOLLET }; - int epollFd = epoll_create1(0); - - if (epollFd < 0) { - HAL_DANGER("i3_venc", "Creating the polling descriptor failed!\n"); - return (void*)0; - } + int ret, maxFd = 0; for (int i = 0; i < I3_VENC_CHN_NUM; i++) { if (!i3_state[i].enable) continue; @@ -698,24 +692,38 @@ void *i3_video_thread(void) return NULL; } i3_state[i].fileDesc = ret; - event.data.fd = ret; - if (ret = epoll_ctl(epollFd, EPOLL_CTL_ADD, ret, &event)) { - HAL_DANGER("i3_venc", "Adding the encoder descriptor to " - "the polling set failed with %#x!\n", ret); - return (void*)0; - } + + if (maxFd <= i3_state[i].fileDesc) + maxFd = i3_state[i].fileDesc; } i3_venc_stat stat; i3_venc_strm stream; + struct timeval timeout; + fd_set readFds; while (keepRunning) { - epollEvt = epoll_wait(epollFd, events, 5, 2000); - for (int e = 0; e < epollEvt; e++) { + FD_ZERO(&readFds); + for(int i = 0; i < I3_VENC_CHN_NUM; i++) { + if (!i3_state[i].enable) continue; + if (!i3_state[i].mainLoop) continue; + FD_SET(i3_state[i].fileDesc, &readFds); + } + + timeout.tv_sec = 2; + timeout.tv_usec = 0; + ret = select(maxFd + 1, &readFds, NULL, NULL, &timeout); + if (ret < 0) { + HAL_DANGER("i3_venc", "Select operation failed!\n"); + break; + } else if (ret == 0) { + HAL_WARNING("i3_venc", "Main stream loop timed out!\n"); + continue; + } else { for (int i = 0; i < I3_VENC_CHN_NUM; i++) { if (!i3_state[i].enable) continue; if (!i3_state[i].mainLoop) continue; - if (i3_state[i].fileDesc == events[e].data.fd) { + if (FD_ISSET(i3_state[i].fileDesc, &readFds)) { memset(&stream, 0, sizeof(stream)); if (ret = i3_venc.fnQuery(i, &stat)) { @@ -817,15 +825,11 @@ void *i3_video_thread(void) } free(stream.packet); stream.packet = NULL; - break; } } } } - if (close(epollFd)) - HAL_DANGER("i3_venc", "Closing the polling descriptor failed!\n"); - HAL_INFO("i3_venc", "Shutting down encoding thread...\n"); }*/ diff --git a/src/hal/star/i3_hal.h b/src/hal/star/i3_hal.h index e957940..333d628 100644 --- a/src/hal/star/i3_hal.h +++ b/src/hal/star/i3_hal.h @@ -10,7 +10,7 @@ #include "../support.h" -#include +#include #include extern char keepRunning; diff --git a/src/hal/star/i6_hal.c b/src/hal/star/i6_hal.c index 1132d95..fcc45ea 100644 --- a/src/hal/star/i6_hal.c +++ b/src/hal/star/i6_hal.c @@ -648,9 +648,7 @@ void i6_video_request_idr(char index) int i6_video_snapshot_grab(char index, char quality, hal_jpegdata *jpeg) { - int ret, epollEvt; - struct epoll_event events[5], event = { .events = EPOLLIN|EPOLLET }; - int epollFd = epoll_create1(0); + int ret; if (ret = i6_channel_bind(index, 1)) { HAL_DANGER("i6_venc", "Binding the encoder channel " @@ -681,15 +679,21 @@ int i6_video_snapshot_grab(char index, char quality, hal_jpegdata *jpeg) } int fd = i6_venc.fnGetDescriptor(index); - event.data.fd = fd; - if (ret = epoll_ctl(epollFd, EPOLL_CTL_ADD, fd, &event)) { - HAL_DANGER("i6_venc", "Adding the encoder descriptor to " - "the polling set failed with %#x!\n", ret); + + struct timeval timeout = { .tv_sec = 2, .tv_usec = 0 }; + fd_set readFds; + FD_ZERO(&readFds); + FD_SET(fd, &readFds); + ret = select(fd + 1, &readFds, NULL, NULL, &timeout); + if (ret < 0) { + HAL_DANGER("i6_venc", "Select operation failed!\n"); + goto abort; + } else if (ret == 0) { + HAL_DANGER("i6_venc", "Capture stream timed out!\n"); goto abort; } - epollEvt = epoll_wait(epollFd, events, 5, 2000); - for (int e = 0; e < epollEvt; e++) { + if (FD_ISSET(fd, &readFds)) { i6_venc_stat stat; if (ret = i6_venc.fnQuery(index, &stat)) { HAL_DANGER("i6_venc", "Querying the encoder channel " @@ -740,9 +744,6 @@ int i6_video_snapshot_grab(char index, char quality, hal_jpegdata *jpeg) i6_venc.fnFreeStream(index, &strm); } - if (close(epollFd)) - HAL_DANGER("i6_venc", "Closing the polling descriptor failed!\n"); - i6_venc.fnFreeDescriptor(index); i6_venc.fnStopReceiving(index); @@ -754,14 +755,7 @@ int i6_video_snapshot_grab(char index, char quality, hal_jpegdata *jpeg) void *i6_video_thread(void) { - int ret, epollEvt; - struct epoll_event events[5], event = { .events = EPOLLIN|EPOLLET }; - int epollFd = epoll_create1(0); - - if (epollFd < 0) { - HAL_DANGER("i6_venc", "Creating the polling descriptor failed!\n"); - return (void*)0; - } + int ret, maxFd = 0; for (int i = 0; i < I6_VENC_CHN_NUM; i++) { if (!i6_state[i].enable) continue; @@ -773,24 +767,38 @@ void *i6_video_thread(void) return (void*)0; } i6_state[i].fileDesc = ret; - event.data.fd = ret; - if (ret = epoll_ctl(epollFd, EPOLL_CTL_ADD, ret, &event)) { - HAL_DANGER("i6_venc", "Adding the encoder descriptor to " - "the polling set failed with %#x!\n", ret); - return (void*)0; - } + + if (maxFd <= i6_state[i].fileDesc) + maxFd = i6_state[i].fileDesc; } i6_venc_stat stat; i6_venc_strm stream; + struct timeval timeout; + fd_set readFds; while (keepRunning) { - epollEvt = epoll_wait(epollFd, events, 5, 2000); - for (int e = 0; e < epollEvt; e++) { + FD_ZERO(&readFds); + for(int i = 0; i < I6_VENC_CHN_NUM; i++) { + if (!i6_state[i].enable) continue; + if (!i6_state[i].mainLoop) continue; + FD_SET(i6_state[i].fileDesc, &readFds); + } + + timeout.tv_sec = 2; + timeout.tv_usec = 0; + ret = select(maxFd + 1, &readFds, NULL, NULL, &timeout); + if (ret < 0) { + HAL_DANGER("i6_venc", "Select operation failed!\n"); + break; + } else if (ret == 0) { + HAL_WARNING("i6_venc", "Main stream loop timed out!\n"); + continue; + } else { for (int i = 0; i < I6_VENC_CHN_NUM; i++) { if (!i6_state[i].enable) continue; if (!i6_state[i].mainLoop) continue; - if (i6_state[i].fileDesc == events[e].data.fd) { + if (FD_ISSET(i6_state[i].fileDesc, &readFds)) { memset(&stream, 0, sizeof(stream)); if (ret = i6_venc.fnQuery(i, &stat)) { @@ -892,15 +900,11 @@ void *i6_video_thread(void) } free(stream.packet); stream.packet = NULL; - break; } } } } - if (close(epollFd)) - HAL_DANGER("i6_venc", "Closing the polling descriptor failed!\n"); - HAL_INFO("i6_venc", "Shutting down encoding thread...\n"); } diff --git a/src/hal/star/i6_hal.h b/src/hal/star/i6_hal.h index 0ad951a..f396930 100644 --- a/src/hal/star/i6_hal.h +++ b/src/hal/star/i6_hal.h @@ -12,7 +12,7 @@ #include "../support.h" -#include +#include #include extern char keepRunning; diff --git a/src/hal/star/i6c_hal.c b/src/hal/star/i6c_hal.c index 9f6900b..882a2ef 100644 --- a/src/hal/star/i6c_hal.c +++ b/src/hal/star/i6c_hal.c @@ -748,9 +748,7 @@ void i6c_video_request_idr(char index) int i6c_video_snapshot_grab(char index, char quality, hal_jpegdata *jpeg) { - int ret, epollEvt; - struct epoll_event events[5], event = { .events = EPOLLIN|EPOLLET }; - int epollFd = epoll_create1(0); + int ret; if (ret = i6c_channel_bind(index, 1)) { HAL_DANGER("i6c_venc", "Binding the encoder channel " @@ -781,15 +779,21 @@ int i6c_video_snapshot_grab(char index, char quality, hal_jpegdata *jpeg) } int fd = i6c_venc.fnGetDescriptor(_i6c_venc_dev[index], index); - event.data.fd = fd; - if (ret = epoll_ctl(epollFd, EPOLL_CTL_ADD, fd, &event)) { - HAL_DANGER("i6c_venc", "Adding the encoder descriptor to " - "the polling set failed with %#x!\n", ret); + + struct timeval timeout = { .tv_sec = 2, .tv_usec = 0 }; + fd_set readFds; + FD_ZERO(&readFds); + FD_SET(fd, &readFds); + ret = select(fd + 1, &readFds, NULL, NULL, &timeout); + if (ret < 0) { + HAL_DANGER("i6c_venc", "Select operation failed!\n"); + goto abort; + } else if (ret == 0) { + HAL_DANGER("i6c_venc", "Capture stream timed out!\n"); goto abort; } - epollEvt = epoll_wait(epollFd, events, 5, 2000); - for (int e = 0; e < epollEvt; e++) { + if (FD_ISSET(fd, &readFds)) { i6c_venc_stat stat; if (ret = i6c_venc.fnQuery(_i6c_venc_dev[index], index, &stat)) { HAL_DANGER("i6c_venc", "Querying the encoder channel " @@ -840,9 +844,6 @@ int i6c_video_snapshot_grab(char index, char quality, hal_jpegdata *jpeg) i6c_venc.fnFreeStream(_i6c_venc_dev[index], index, &strm); } - if (close(epollFd)) - HAL_DANGER("i6c_venc", "Closing the polling descriptor failed!\n"); - i6c_venc.fnFreeDescriptor(_i6c_venc_dev[index], index); i6c_venc.fnStopReceiving(_i6c_venc_dev[index], index); @@ -854,14 +855,7 @@ int i6c_video_snapshot_grab(char index, char quality, hal_jpegdata *jpeg) void *i6c_video_thread(void) { - int ret, epollEvt; - struct epoll_event events[5], event = { .events = EPOLLIN|EPOLLET }; - int epollFd = epoll_create1(0); - - if (epollFd < 0) { - HAL_DANGER("i6c_venc", "Creating the polling descriptor failed!\n"); - return (void*)0; - } + int ret, maxFd = 0; for (int i = 0; i < I6C_VENC_CHN_NUM; i++) { if (!i6c_state[i].enable) continue; @@ -873,24 +867,38 @@ void *i6c_video_thread(void) return (void*)0; } i6c_state[i].fileDesc = ret; - event.data.fd = ret; - if (ret = epoll_ctl(epollFd, EPOLL_CTL_ADD, ret, &event)) { - HAL_DANGER("i6c_venc", "Adding the encoder descriptor to " - "the polling set failed with %#x!\n", ret); - return (void*)0; - } + + if (maxFd <= i6c_state[i].fileDesc) + maxFd = i6c_state[i].fileDesc; } i6c_venc_stat stat; i6c_venc_strm stream; + struct timeval timeout; + fd_set readFds; while (keepRunning) { - epollEvt = epoll_wait(epollFd, events, 5, 2000); - for (int e = 0; e < epollEvt; e++) { + FD_ZERO(&readFds); + for(int i = 0; i < I6C_VENC_CHN_NUM; i++) { + if (!i6c_state[i].enable) continue; + if (!i6c_state[i].mainLoop) continue; + FD_SET(i6c_state[i].fileDesc, &readFds); + } + + timeout.tv_sec = 2; + timeout.tv_usec = 0; + ret = select(maxFd + 1, &readFds, NULL, NULL, &timeout); + if (ret < 0) { + HAL_DANGER("i6c_venc", "Select operation failed!\n"); + break; + } else if (ret == 0) { + HAL_WARNING("i6c_venc", "Main stream loop timed out!\n"); + continue; + } else { for (int i = 0; i < I6C_VENC_CHN_NUM; i++) { if (!i6c_state[i].enable) continue; if (!i6c_state[i].mainLoop) continue; - if (i6c_state[i].fileDesc == events[e].data.fd) { + if (FD_ISSET(i6c_state[i].fileDesc, &readFds)) { memset(&stream, 0, sizeof(stream)); if (ret = i6c_venc.fnQuery(_i6c_venc_dev[i], i, &stat)) { @@ -963,15 +971,11 @@ void *i6c_video_thread(void) } free(stream.packet); stream.packet = NULL; - break; } } } } - if (close(epollFd)) - HAL_DANGER("i6c_venc", "Closing the polling descriptor failed!\n"); - HAL_INFO("i6c_venc", "Shutting down encoding thread...\n"); } diff --git a/src/hal/star/i6c_hal.h b/src/hal/star/i6c_hal.h index f684fa8..74cde20 100644 --- a/src/hal/star/i6c_hal.h +++ b/src/hal/star/i6c_hal.h @@ -10,7 +10,7 @@ #include "i6c_venc.h" #include "i6c_vif.h" -#include +#include #include extern char keepRunning; diff --git a/src/hal/star/i6f_hal.c b/src/hal/star/i6f_hal.c index 6855c86..8737830 100644 --- a/src/hal/star/i6f_hal.c +++ b/src/hal/star/i6f_hal.c @@ -690,9 +690,7 @@ void i6f_video_request_idr(char index) int i6f_video_snapshot_grab(char index, char quality, hal_jpegdata *jpeg) { - int ret, epollEvt; - struct epoll_event events[5], event = { .events = EPOLLIN|EPOLLET }; - int epollFd = epoll_create1(0); + int ret; if (ret = i6f_channel_bind(index, 1)) { HAL_DANGER("i6f_venc", "Binding the encoder channel " @@ -725,15 +723,21 @@ int i6f_video_snapshot_grab(char index, char quality, hal_jpegdata *jpeg) } int fd = i6f_venc.fnGetDescriptor(_i6f_venc_dev[index], index); - event.data.fd = fd; - if (ret = epoll_ctl(epollFd, EPOLL_CTL_ADD, fd, &event)) { - HAL_DANGER("i6f_venc", "Adding the encoder descriptor to " - "the polling set failed with %#x!\n", ret); + + struct timeval timeout = { .tv_sec = 2, .tv_usec = 0 }; + fd_set readFds; + FD_ZERO(&readFds); + FD_SET(fd, &readFds); + ret = select(fd + 1, &readFds, NULL, NULL, &timeout); + if (ret < 0) { + HAL_DANGER("i6f_venc", "Select operation failed!\n"); + goto abort; + } else if (ret == 0) { + HAL_DANGER("i6f_venc", "Capture stream timed out!\n"); goto abort; } - epollEvt = epoll_wait(epollFd, events, 5, 2000); - for (int e = 0; e < epollEvt; e++) { + if (FD_ISSET(fd, &readFds)) { i6f_venc_stat stat; if (ret = i6f_venc.fnQuery(_i6f_venc_dev[index], index, &stat)) { HAL_DANGER("i6f_venc", "Querying the encoder channel " @@ -784,9 +788,6 @@ int i6f_video_snapshot_grab(char index, char quality, hal_jpegdata *jpeg) i6f_venc.fnFreeStream(_i6f_venc_dev[index], index, &strm); } - if (close(epollFd)) - HAL_DANGER("i6f_venc", "Closing the polling descriptor failed!\n"); - i6f_venc.fnFreeDescriptor(_i6f_venc_dev[index], index); i6f_venc.fnStopReceiving(_i6f_venc_dev[index], index); @@ -798,14 +799,7 @@ int i6f_video_snapshot_grab(char index, char quality, hal_jpegdata *jpeg) void *i6f_video_thread(void) { - int ret, epollEvt; - struct epoll_event events[5], event = { .events = EPOLLIN|EPOLLET }; - int epollFd = epoll_create1(0); - - if (epollFd < 0) { - HAL_DANGER("i6f_venc", "Creating the polling descriptor failed!\n"); - return (void*)0; - } + int ret, maxFd = 0; for (int i = 0; i < I6F_VENC_CHN_NUM; i++) { if (!i6f_state[i].enable) continue; @@ -817,25 +811,38 @@ void *i6f_video_thread(void) return NULL; } i6f_state[i].fileDesc = ret; - event.data.fd = ret; - if (ret = epoll_ctl(epollFd, EPOLL_CTL_ADD, ret, &event)) { - HAL_DANGER("i6f_venc", "Adding the encoder descriptor to " - "the polling set failed with %#x!\n", ret); - return (void*)0; - } + + if (maxFd <= i6f_state[i].fileDesc) + maxFd = i6f_state[i].fileDesc; } i6f_venc_stat stat; i6f_venc_strm stream; + struct timeval timeout; + fd_set readFds; while (keepRunning) { - epollEvt = epoll_wait(epollFd, events, 5, 2000); - for (int e = 0; e < epollEvt; e++) { + FD_ZERO(&readFds); + for(int i = 0; i < I6F_VENC_CHN_NUM; i++) { + if (!i6f_state[i].enable) continue; + if (!i6f_state[i].mainLoop) continue; + FD_SET(i6f_state[i].fileDesc, &readFds); + } + + timeout.tv_sec = 2; + timeout.tv_usec = 0; + ret = select(maxFd + 1, &readFds, NULL, NULL, &timeout); + if (ret < 0) { + HAL_DANGER("i6f_venc", "Select operation failed!\n"); + break; + } else if (ret == 0) { + HAL_WARNING("i6f_venc", "Main stream loop timed out!\n"); + continue; + } else { for (int i = 0; i < I6F_VENC_CHN_NUM; i++) { if (!i6f_state[i].enable) continue; if (!i6f_state[i].mainLoop) continue; - if (i6f_state[i].fileDesc == events[e].data.fd) { - + if (FD_ISSET(i6f_state[i].fileDesc, &readFds)) { memset(&stream, 0, sizeof(stream)); if (ret = i6f_venc.fnQuery(_i6f_venc_dev[i], i, &stat)) { @@ -907,15 +914,11 @@ void *i6f_video_thread(void) "channel %d failed with %#x!\n", i, ret); free(stream.packet); stream.packet = NULL; - break; } } } } - if (close(epollFd)) - HAL_DANGER("i6f_venc", "Closing the polling descriptor failed!\n"); - HAL_INFO("i6f_venc", "Shutting down encoding thread...\n"); } diff --git a/src/hal/star/i6f_hal.h b/src/hal/star/i6f_hal.h index 917abfa..0ade29c 100644 --- a/src/hal/star/i6f_hal.h +++ b/src/hal/star/i6f_hal.h @@ -8,7 +8,7 @@ #include "i6f_venc.h" #include "i6f_vif.h" -#include +#include #include extern char keepRunning;