From 6febe12f3b147d3adad6091d5c5d804e1392d816 Mon Sep 17 00:00:00 2001 From: MrDave Date: Sun, 2 May 2021 12:14:47 -0600 Subject: [PATCH] Resolve processing when no camera specified --- src/alg_sec.cpp | 4 ++++ src/motion_loop.cpp | 18 ++++++++---------- src/motionplus.cpp | 20 +++++++++----------- src/rotate.cpp | 4 ++++ src/video_v4l2.cpp | 6 ++++++ 5 files changed, 31 insertions(+), 21 deletions(-) diff --git a/src/alg_sec.cpp b/src/alg_sec.cpp index d3a9cfd3..b998f526 100644 --- a/src/alg_sec.cpp +++ b/src/alg_sec.cpp @@ -551,6 +551,10 @@ void algsec_deinit(ctx_cam *cam) #ifdef HAVE_OPENCV int waitcnt = 0; + if (cam->algsec == NULL) { + return; + } + algsec_params_deinit(cam->algsec->models); if (!cam->algsec->closing) { diff --git a/src/motion_loop.cpp b/src/motion_loop.cpp index 40c9dd35..7808e966 100644 --- a/src/motion_loop.cpp +++ b/src/motion_loop.cpp @@ -641,12 +641,7 @@ static int mlp_init_cam_start(struct ctx_cam *cam) if (cam->video_dev == -1) { MOTION_LOG(WRN, TYPE_ALL, NO_ERRNO ,_("Could not fetch initial image from camera ")); - MOTION_LOG(WRN, TYPE_ALL, NO_ERRNO - ,_("Motion continues using width and height from config file(s)")); - cam->imgs.width = cam->conf->width; - cam->imgs.height = cam->conf->height; - cam->imgs.size_norm = cam->conf->width * cam->conf->height * 3 / 2; - cam->imgs.motionsize = cam->conf->width * cam->conf->height; + return -1; } else if (cam->video_dev == -2) { MOTION_LOG(ERR, TYPE_ALL, NO_ERRNO ,_("Could not fetch initial image from camera ")); @@ -1651,10 +1646,14 @@ void *motion_loop(void *arg) { struct ctx_cam *cam =(struct ctx_cam *) arg; - cam->restart_cam = true; - cam->watchdog = cam->conf->watchdog_tmo; cam->running_cam = true; + pthread_mutex_lock(&cam->motapp->global_lock); + cam->motapp->threads_running++; + pthread_mutex_unlock(&cam->motapp->global_lock); + + cam->watchdog = cam->conf->watchdog_tmo; + if (mlp_init(cam) == 0) { while (!cam->finish_cam || cam->event_stop) { mlp_prepare(cam); @@ -1689,8 +1688,7 @@ void *motion_loop(void *arg) cam->motapp->threads_running--; pthread_mutex_unlock(&cam->motapp->global_lock); - cam->running_cam = 0; - cam->finish_cam = 0; + cam->running_cam = false; pthread_exit(NULL); } diff --git a/src/motionplus.cpp b/src/motionplus.cpp index e5cd2234..2ce2f380 100644 --- a/src/motionplus.cpp +++ b/src/motionplus.cpp @@ -420,23 +420,21 @@ static void motion_startup(struct ctx_motapp *motapp, int daemonize, int argc, c /** Start a camera thread */ static void motion_start_thread(struct ctx_motapp *motapp, int indx) { - pthread_attr_t thread_attr; + int retcd; - pthread_mutex_lock(&motapp->global_lock); - motapp->threads_running++; - pthread_mutex_unlock(&motapp->global_lock); + pthread_attr_t thread_attr; pthread_attr_init(&thread_attr); pthread_attr_setdetachstate(&thread_attr, PTHREAD_CREATE_DETACHED); - if (pthread_create(&motapp->cam_list[indx]->thread_id - , &thread_attr, &motion_loop, motapp->cam_list[indx])) { - /* thread create failed, undo running state */ - motapp->cam_list[indx]->running_cam = false; - pthread_mutex_lock(&motapp->global_lock); - motapp->threads_running--; - pthread_mutex_unlock(&motapp->global_lock); + motapp->cam_list[indx]->restart_cam = true; + + retcd = pthread_create(&motapp->cam_list[indx]->thread_id + , &thread_attr, &motion_loop, motapp->cam_list[indx]); + if (retcd != 0) { + MOTION_LOG(WRN, TYPE_ALL, NO_ERRNO,_("Unable to start thread for motion loop.")); } + pthread_attr_destroy(&thread_attr); } diff --git a/src/rotate.cpp b/src/rotate.cpp index 2d9b05e6..708c5902 100644 --- a/src/rotate.cpp +++ b/src/rotate.cpp @@ -275,6 +275,10 @@ void rotate_init(struct ctx_cam *cam) void rotate_deinit(struct ctx_cam *cam) { + if (cam->rotate_data == NULL) { + return; + } + if (cam->rotate_data->buffer_norm) { free(cam->rotate_data->buffer_norm); } diff --git a/src/video_v4l2.cpp b/src/video_v4l2.cpp index 8b6236ab..b2b80ee0 100644 --- a/src/video_v4l2.cpp +++ b/src/video_v4l2.cpp @@ -1188,6 +1188,12 @@ void v4l2_cleanup(ctx_cam *cam) cam->v4l2cam->devctrl_count=0; util_parms_free(cam->v4l2cam->params); + + if (cam->v4l2cam->params != NULL) { + free(cam->v4l2cam->params); + cam->v4l2cam->params = NULL; + } + if (cam->v4l2cam != NULL) { free(cam->v4l2cam); }