From 05fff066ef8714870f8d97a52603d3be115ec572 Mon Sep 17 00:00:00 2001 From: Sergiotarxz Date: Sun, 19 Mar 2023 20:03:24 +0100 Subject: [PATCH] Calling mCoreThreadJoin to avoid crash. --- include/msgba/client_connection_data.h | 3 +++ src/client_connection_data.c | 4 ++++ src/core_controller.c | 6 ++---- src/main.c | 5 +++-- src/packet.c | 2 +- src/packet/hello.c | 26 ++++++++++++++++++++++++-- 6 files changed, 37 insertions(+), 9 deletions(-) diff --git a/include/msgba/client_connection_data.h b/include/msgba/client_connection_data.h index f4d0ff2..7bcbbb5 100644 --- a/include/msgba/client_connection_data.h +++ b/include/msgba/client_connection_data.h @@ -15,6 +15,7 @@ struct msClientConnectionData { int clientFd; struct msCoreController *coreController; pthread_mutex_t *mutexSendPacket; + struct timespec *lastFrameDate; }; /** @@ -28,4 +29,6 @@ msClientConnectionDataNew(size_t numberOfThread, int clientFd); void msClientConnectionDataDestroy(struct msClientConnectionData **data); + +extern pthread_mutex_t mutexClientConnection; #endif diff --git a/src/client_connection_data.c b/src/client_connection_data.c index 2e7f807..2a1628b 100644 --- a/src/client_connection_data.c +++ b/src/client_connection_data.c @@ -2,6 +2,9 @@ #include #include + +pthread_mutex_t mutexClientConnection = PTHREAD_MUTEX_INITIALIZER; + struct msClientConnectionData * msClientConnectionDataNew(size_t numberOfThread, int clientFd) { struct msClientConnectionData *data = malloc (sizeof *data); @@ -9,6 +12,7 @@ msClientConnectionDataNew(size_t numberOfThread, int clientFd) { data->clientFd = clientFd; data->coreController = NULL; data->mutexSendPacket = malloc(sizeof *data->mutexSendPacket); + data->lastFrameDate = calloc(sizeof *data->lastFrameDate, 1); pthread_mutex_init(data->mutexSendPacket, NULL); return data; } diff --git a/src/core_controller.c b/src/core_controller.c index e86f1b6..bd0aedd 100644 --- a/src/core_controller.c +++ b/src/core_controller.c @@ -76,10 +76,8 @@ msCoreControllerDestroy(struct msCoreController **controller_ptr) { if (controller_ptr && *controller_ptr) { struct msCoreController *controller = *controller_ptr; - if (mCoreThreadHasStarted(&controller->threadContext) - && !mCoreThreadHasExited(&controller->threadContext)) { - mCoreThreadEnd(&controller->threadContext); - } + mCoreThreadEnd(&controller->threadContext); + mCoreThreadJoin(&controller->threadContext); if (controller->threadContext.core) { struct mCore *core = controller->threadContext.core; core->deinit(core); diff --git a/src/main.c b/src/main.c index 99f91a3..3b52d35 100644 --- a/src/main.c +++ b/src/main.c @@ -96,10 +96,11 @@ int main(int argc, char **argv) { while (connected_clients == MAX_NUMBER_OF_THREADS) { pthread_cond_wait(&cond_client_number_decreased, &mutex_number_connections); } + connected_clients++; + pthread_mutex_unlock(&mutex_number_connections); struct msClientConnectionData *data = msClientConnectionDataNew(number_of_threads, client_fd); - pthread_create(&pthread_client_connections[connected_clients++], &attributes, + pthread_create(&pthread_client_connections[connected_clients], &attributes, &handleClientConnection, (void *)data); - pthread_mutex_unlock(&mutex_number_connections); } } diff --git a/src/packet.c b/src/packet.c index eff68db..a20ba1b 100644 --- a/src/packet.c +++ b/src/packet.c @@ -39,7 +39,7 @@ return_ms_packet_write: bool msPacketSend(const struct msPacket *const packet, struct msClientConnectionData *const data) { - if (!data->mutexSendPacket) { + if (!data) { return false; } pthread_mutex_lock(data->mutexSendPacket); diff --git a/src/packet/hello.c b/src/packet/hello.c index ff08d21..e0b9874 100644 --- a/src/packet/hello.c +++ b/src/packet/hello.c @@ -1,3 +1,6 @@ +#include +#include + #include #include @@ -28,14 +31,33 @@ msThreadCallbackStart(struct mCoreThread *threadContext) { } void msThreadCallbackSetFrame(struct mCoreThread *threadContext) { +#define SEC_TO_NANO(sec) \ + (((uint64_t)sec) * 1000 * 1000 * 1000) struct msClientConnectionData *data = (struct msClientConnectionData *)threadContext->userData; - unsigned int stride = data->coreController->stride; - color_t *outputBuffer = data->coreController->outputBuffer; + unsigned int stride; + color_t *outputBuffer; unsigned width, height; unsigned char *raw_data = NULL; size_t raw_data_len = 0; struct msPacketSendFrame *send_frame = NULL; struct msPacket *packet = NULL; + struct timespec current_date; + timespec_get(¤t_date, TIME_UTC); + uint64_t nano_current = SEC_TO_NANO(current_date.tv_sec % 1000) + current_date.tv_nsec; + uint64_t nano_last = SEC_TO_NANO(data->lastFrameDate->tv_sec % 1000) + data->lastFrameDate->tv_nsec + (SEC_TO_NANO(1) / 60); + + if (!data || !data->coreController) { + goto return_ms_thread_callback_set_frame; + } + stride = data->coreController->stride; + outputBuffer = data->coreController->outputBuffer; + + while (nano_current < nano_last && nano_last != 0 && nano_last - nano_current < SEC_TO_NANO(10)) { + usleep(500); + timespec_get(¤t_date, TIME_UTC); + nano_current = SEC_TO_NANO(current_date.tv_sec % 1000) + current_date.tv_nsec; + } + *data->lastFrameDate = current_date; data->coreController->threadContext.core->desiredVideoDimensions(data->coreController->threadContext.core, &width, &height);