Skip to content

Commit

Permalink
Reverting to selects for video threads
Browse files Browse the repository at this point in the history
- 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
  • Loading branch information
wberube committed Sep 14, 2024
1 parent fa91a56 commit 84dd22f
Show file tree
Hide file tree
Showing 20 changed files with 375 additions and 349 deletions.
70 changes: 37 additions & 33 deletions src/hal/hisi/v1_hal.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 "
Expand All @@ -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 "
Expand Down Expand Up @@ -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);
Expand All @@ -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;
Expand All @@ -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)) {
Expand Down Expand Up @@ -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");
}

Expand Down
2 changes: 1 addition & 1 deletion src/hal/hisi/v1_hal.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
#include "v1_vpss.h"

#include <fcntl.h>
#include <sys/epoll.h>
#include <sys/ioctl.h>
#include <sys/select.h>
#include <unistd.h>

extern char keepRunning;
Expand Down
69 changes: 36 additions & 33 deletions src/hal/hisi/v2_hal.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 "
Expand All @@ -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 "
Expand Down Expand Up @@ -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);
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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)) {
Expand Down Expand Up @@ -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");
}

Expand Down
2 changes: 1 addition & 1 deletion src/hal/hisi/v2_hal.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
#include "v2_vpss.h"

#include <fcntl.h>
#include <sys/epoll.h>
#include <sys/ioctl.h>
#include <sys/select.h>
#include <unistd.h>

extern char keepRunning;
Expand Down
Loading

0 comments on commit 84dd22f

Please sign in to comment.