Skip to content

Commit

Permalink
Tidying up the code
Browse files Browse the repository at this point in the history
  • Loading branch information
wberube committed Jun 6, 2024
1 parent 64ae7fd commit aff6ff3
Show file tree
Hide file tree
Showing 15 changed files with 38 additions and 50 deletions.
3 changes: 1 addition & 2 deletions src/hal/hisi/v3_hal.c
Original file line number Diff line number Diff line change
Expand Up @@ -581,8 +581,7 @@ int v3_video_destroy_all(void)
return EXIT_SUCCESS;
}

int v3_video_snapshot_grab(char index, short width, short height,
char quality, hal_jpegdata *jpeg)
int v3_video_snapshot_grab(char index, hal_jpegdata *jpeg)
{
int ret;

Expand Down
3 changes: 1 addition & 2 deletions src/hal/hisi/v3_hal.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,7 @@ int v3_sensor_init(char *name, char *obj);
int v3_video_create(char index, hal_vidconfig *config);
int v3_video_destroy(char index);
int v3_video_destroy_all(void);
int v3_video_snapshot_grab(char index, short width, short height,
char quality, hal_jpegdata *jpeg);
int v3_video_snapshot_grab(char index, hal_jpegdata *jpeg);
void *v3_video_thread(void);

int v3_system_calculate_block(short width, short height, v3_common_pixfmt pixFmt,
Expand Down
3 changes: 1 addition & 2 deletions src/hal/hisi/v4_hal.c
Original file line number Diff line number Diff line change
Expand Up @@ -636,8 +636,7 @@ int v4_video_destroy_all(void)
return EXIT_SUCCESS;
}

int v4_video_snapshot_grab(char index, short width, short height,
char quality, hal_jpegdata *jpeg)
int v4_video_snapshot_grab(char index, hal_jpegdata *jpeg)
{
int ret;

Expand Down
3 changes: 1 addition & 2 deletions src/hal/hisi/v4_hal.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,7 @@ int v4_sensor_init(char *name, char *obj);
int v4_video_create(char index, hal_vidconfig *config);
int v4_video_destroy(char index);
int v4_video_destroy_all(void);
int v4_video_snapshot_grab(char index, short width, short height,
char quality, hal_jpegdata *jpeg);
int v4_video_snapshot_grab(char index, hal_jpegdata *jpeg);
void *v4_video_thread(void);

int v4_system_calculate_block(short width, short height, v4_common_pixfmt pixFmt,
Expand Down
3 changes: 1 addition & 2 deletions src/hal/inge/t31_hal.c
Original file line number Diff line number Diff line change
Expand Up @@ -409,8 +409,7 @@ int t31_video_destroy_all(void)
return EXIT_SUCCESS;
}

int t31_video_snapshot_grab(char index, short width, short height,
char quality, hal_jpegdata *jpeg)
int t31_video_snapshot_grab(char index, hal_jpegdata *jpeg)
{
int ret, fd;
char mjpeg = 0;
Expand Down
3 changes: 1 addition & 2 deletions src/hal/inge/t31_hal.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,7 @@ int t31_region_setbitmap(int *handle, hal_bitmap *bitmap);
int t31_video_create(char index, hal_vidconfig *config);
int t31_video_destroy(char index);
int t31_video_destroy_all(void);
int t31_video_snapshot_grab(char index, short width, short height,
char quality, hal_jpegdata *jpeg);
int t31_video_snapshot_grab(char index, hal_jpegdata *jpeg);
void *t31_video_thread(void);

void t31_system_deinit(void);
Expand Down
3 changes: 1 addition & 2 deletions src/hal/sstar/i6_hal.c
Original file line number Diff line number Diff line change
Expand Up @@ -571,8 +571,7 @@ int i6_video_destroy_all(void)
return EXIT_SUCCESS;
}

int i6_video_snapshot_grab(char index, short width, short height,
char quality, char grayscale, hal_jpegdata *jpeg)
int i6_video_snapshot_grab(char index, char quality, char grayscale, hal_jpegdata *jpeg)
{
int ret;

Expand Down
3 changes: 1 addition & 2 deletions src/hal/sstar/i6_hal.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,7 @@ int i6_region_setbitmap(int handle, hal_bitmap *bitmap);
int i6_video_create(char index, hal_vidconfig *config);
int i6_video_destroy(char index);
int i6_video_destroy_all(void);
int i6_video_snapshot_grab(char index, short width, short height,
char quality, char grayscale, hal_jpegdata *jpeg);
int i6_video_snapshot_grab(char index, char quality, char grayscale, hal_jpegdata *jpeg);
void *i6_video_thread(void);

void i6_system_deinit(void);
Expand Down
3 changes: 1 addition & 2 deletions src/hal/sstar/i6c_hal.c
Original file line number Diff line number Diff line change
Expand Up @@ -676,8 +676,7 @@ int i6c_video_destroy_all(void)
return EXIT_SUCCESS;
}

int i6c_video_snapshot_grab(char index, short width, short height,
char quality, char grayscale, hal_jpegdata *jpeg)
int i6c_video_snapshot_grab(char index, char quality, char grayscale, hal_jpegdata *jpeg)
{
int ret;
char device =
Expand Down
3 changes: 1 addition & 2 deletions src/hal/sstar/i6c_hal.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,7 @@ int i6c_region_setbitmap(int handle, hal_bitmap *bitmap);
int i6c_video_create(char index, hal_vidconfig *config);
int i6c_video_destroy(char index, char jpeg);
int i6c_video_destroy_all(void);
int i6c_video_snapshot_grab(char index, short width, short height,
char quality, char grayscale, hal_jpegdata *jpeg);
int i6c_video_snapshot_grab(char index, char quality, char grayscale, hal_jpegdata *jpeg);
void *i6c_video_thread(void);

void i6c_system_deinit(void);
Expand Down
3 changes: 1 addition & 2 deletions src/hal/sstar/i6f_hal.c
Original file line number Diff line number Diff line change
Expand Up @@ -633,8 +633,7 @@ int i6f_video_destroy_all(void)
return EXIT_SUCCESS;
}

int i6f_video_snapshot_grab(char index, short width, short height,
char quality, char grayscale, hal_jpegdata *jpeg)
int i6f_video_snapshot_grab(char index, char quality, char grayscale, hal_jpegdata *jpeg)
{
int ret;
char device =
Expand Down
3 changes: 1 addition & 2 deletions src/hal/sstar/i6f_hal.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,7 @@ int i6f_region_setbitmap(int handle, hal_bitmap *bitmap);
int i6f_video_create(char index, hal_vidconfig *config);
int i6f_video_destroy(char index, char jpeg);
int i6f_video_destroy_all(void);
int i6f_video_snapshot_grab(char index, short width, short height,
char quality, char grayscale, hal_jpegdata *jpeg);
int i6f_video_snapshot_grab(char index, char quality, char grayscale, hal_jpegdata *jpeg);
void *i6f_video_thread(void);

void i6f_system_deinit(void);
Expand Down
20 changes: 9 additions & 11 deletions src/jpeg.c
Original file line number Diff line number Diff line change
Expand Up @@ -109,18 +109,16 @@ int jpeg_get(short width, short height, char quality, char grayscale,
int ret;

switch (plat) {
case HAL_PLATFORM_I6: ret = i6_video_snapshot_grab(jpeg_index, width, height,
quality, grayscale, jpeg); break;
case HAL_PLATFORM_I6C: ret = i6c_video_snapshot_grab(jpeg_index, width, height,
quality, grayscale, jpeg); break;
case HAL_PLATFORM_I6F: ret = i6f_video_snapshot_grab(jpeg_index, width, height,
quality, grayscale, jpeg); break;
case HAL_PLATFORM_I6: ret = i6_video_snapshot_grab(jpeg_index, quality,
grayscale, jpeg); break;
case HAL_PLATFORM_I6C: ret = i6c_video_snapshot_grab(jpeg_index, quality,
grayscale, jpeg); break;
case HAL_PLATFORM_I6F: ret = i6f_video_snapshot_grab(jpeg_index, quality,
grayscale, jpeg); break;
case HAL_PLATFORM_T31: ret = t31_video_snapshot_grab(app_config.mjpeg_enable ?
-1 : jpeg_index, width, height, quality, jpeg); break;
case HAL_PLATFORM_V3: ret = v3_video_snapshot_grab(jpeg_index, width, height,
quality, jpeg); break;
case HAL_PLATFORM_V4: ret = v4_video_snapshot_grab(jpeg_index, width, height,
quality, jpeg); break;
-1 : jpeg_index, jpeg); break;
case HAL_PLATFORM_V3: ret = v3_video_snapshot_grab(jpeg_index, jpeg); break;
case HAL_PLATFORM_V4: ret = v4_video_snapshot_grab(jpeg_index, jpeg); break;
}
if (ret) {
if (jpeg->data)
Expand Down
2 changes: 1 addition & 1 deletion src/night.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#include <sys/ioctl.h>
#include <unistd.h>

#define tag "[night]: "
#define tag "[night] "

static bool night_mode = false;
pthread_t nightPid = 0;
Expand Down
30 changes: 16 additions & 14 deletions src/server.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#include "server.h"

#define tag "[server] "

char keepRunning = 1;

enum StreamType { STREAM_H26X, STREAM_JPEG, STREAM_MJPEG, STREAM_MP4 };
Expand Down Expand Up @@ -251,7 +253,7 @@ void *send_jpeg_thread(void *vargp) {
struct jpegtask task = *((struct jpegtask *)vargp);
hal_jpegdata jpeg = {0};
printf(
"Requesting a JPEG snapshot (%ux%u, qfactor %u, color2Gray %d)...\n",
tag "Requesting a JPEG snapshot (%ux%u, qfactor %u, color2Gray %d)...\n",
task.width, task.height, task.qfactor, task.color2Gray);
int ret =
jpeg_get(task.width, task.height, task.qfactor, task.color2Gray, &jpeg);
Expand All @@ -266,7 +268,7 @@ void *send_jpeg_thread(void *vargp) {
close_socket_fd(task.client_fd);
return NULL;
}
printf("JPEG snapshot has been received!\n");
printf(tag "JPEG snapshot has been received!\n");
char buf[1024];
int buf_len = sprintf(
buf,
Expand All @@ -278,7 +280,7 @@ void *send_jpeg_thread(void *vargp) {
send_to_fd(task.client_fd, "\r\n", 2);
close_socket_fd(task.client_fd);
free(jpeg.data);
printf("JPEG snapshot has been sent!\n");
printf(tag "JPEG snapshot has been sent!\n");
return NULL;
}

Expand Down Expand Up @@ -432,7 +434,7 @@ void parse_request(char *request) {
uri = strtok(NULL, " \t");
prot = strtok(NULL, " \t\r\n");

fprintf(stderr, "[server]\x1b[32m New request: (%s) %s\x1b[0m\n", method, uri);
fprintf(stderr, tag "\x1b[32m New request: (%s) %s\x1b[0m\n", method, uri);

if (query = strchr(uri, '?'))
*query++ = '\0';
Expand Down Expand Up @@ -473,7 +475,7 @@ void *server_thread(void *vargp) {
int enable = 1;
if (setsockopt(server_fd, SOL_SOCKET, SO_REUSEADDR, &enable, sizeof(int)) <
0) {
printf("Web server error: setsockopt(SO_REUSEADDR) failed");
printf(tag "setsockopt(SO_REUSEADDR) failed");
fflush(stdout);
}
struct sockaddr_in server;
Expand All @@ -482,7 +484,7 @@ void *server_thread(void *vargp) {
server.sin_addr.s_addr = htonl(INADDR_ANY);
int res = bind(server_fd, (struct sockaddr *)&server, sizeof(server));
if (res != 0) {
printf("Web server error: %s (%d)\n", strerror(errno), errno);
printf(tag "%s (%d)\n", strerror(errno), errno);
keepRunning = 0;
close_socket_fd(server_fd);
return NULL;
Expand Down Expand Up @@ -594,7 +596,7 @@ void *server_thread(void *vargp) {
task.width = app_config.jpeg_width;
task.height = app_config.jpeg_height;
task.qfactor = app_config.jpeg_qfactor;
task.color2Gray = 3;
task.color2Gray = 0;

if (!empty(query)) {
char *remain;
Expand Down Expand Up @@ -633,12 +635,12 @@ void *server_thread(void *vargp) {
pthread_attr_getstacksize(&thread_attr, &stacksize);
size_t new_stacksize = 16 * 1024;
if (pthread_attr_setstacksize(&thread_attr, new_stacksize)) {
printf("Error: Can't set stack size %ld\n", new_stacksize);
printf("[jpeg] Can't set stack size %ld\n", new_stacksize);
}
pthread_create(
&thread_id, &thread_attr, send_jpeg_thread, (void *)&task);
if (pthread_attr_setstacksize(&thread_attr, stacksize)) {
printf("Error: Can't set stack size %ld\n", stacksize);
printf("[jpeg] Can't set stack size %ld\n", stacksize);
}
pthread_attr_destroy(&thread_attr);
}
Expand Down Expand Up @@ -714,12 +716,12 @@ void *server_thread(void *vargp) {
close_socket_fd(client_fd);
}
close_socket_fd(server_fd);
printf("Shutdown server thread\n");
printf(tag "Thread has exited\n");
return NULL;
}

void sig_handler(int signo) {
printf("Graceful shutdown...\n");
printf(tag "Graceful shutdown...\n");
keepRunning = 0;
}
void epipe_handler(int signo) { printf("EPIPE\n"); }
Expand Down Expand Up @@ -752,12 +754,12 @@ int start_server() {
pthread_attr_getstacksize(&thread_attr, &stacksize);
size_t new_stacksize = app_config.web_server_thread_stack_size;
if (pthread_attr_setstacksize(&thread_attr, new_stacksize)) {
printf("Can't set stack size %zu\n", new_stacksize);
printf(tag "Can't set stack size %zu\n", new_stacksize);
}
pthread_create(
&server_thread_id, &thread_attr, server_thread, (void *)&server_fd);
if (pthread_attr_setstacksize(&thread_attr, stacksize)) {
printf("Can't set stack size %zu\n", stacksize);
printf(tag "Can't set stack size %zu\n", stacksize);
}
pthread_attr_destroy(&thread_attr);
}
Expand All @@ -773,6 +775,6 @@ int stop_server() {
pthread_join(server_thread_id, NULL);

pthread_mutex_destroy(&client_fds_mutex);
printf("Shutting down server...\n");
printf(tag "Shutting down server...\n");
return EXIT_SUCCESS;
}

0 comments on commit aff6ff3

Please sign in to comment.