Skip to content

Commit

Permalink
Improve netcam shutdown process
Browse files Browse the repository at this point in the history
  • Loading branch information
Mr-Dave committed Oct 28, 2024
1 parent 6fc386c commit 34ea87e
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 46 deletions.
6 changes: 6 additions & 0 deletions src/camera.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -411,9 +411,11 @@ void cls_camera::cam_start()
libcam = new cls_libcam(this);
} else if (camera_type == CAMERA_TYPE_NETCAM) {
netcam = new cls_netcam(this, false);
netcam->netcam_start();
if (cfg->netcam_high_url != "") {
watchdog = cfg->watchdog_tmo;
netcam_high = new cls_netcam(this, true);
netcam_high->netcam_start();
}
} else if (camera_type == CAMERA_TYPE_V4L2) {
v4l2cam = new cls_v4l2cam(this);
Expand Down Expand Up @@ -504,6 +506,10 @@ void cls_camera::init_firstimage()
indx = 0;
}

if (finish == true) {
return;
}

if ((indx >= 5) || (device_status != STATUS_OPENED)) {
if (device_status != STATUS_OPENED) {
MOTPLS_LOG(ERR, TYPE_ALL, NO_ERRNO, "%s", "Unable to open camera");
Expand Down
27 changes: 18 additions & 9 deletions src/motionplus.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -142,24 +142,33 @@ void cls_motapp::signal_process()
break;
case MOTPLS_SIGNAL_SIGTERM: /* Quit application */
webu->finish = true;
for (indx=0; indx<cam_cnt; indx++) {
cam_list[indx]->event_stop = true;
cam_list[indx]->restart = false;
cam_list[indx]->handler_stop = true;
}
for (indx=0; indx<snd_cnt; indx++) {
snd_list[indx]->restart = false;
snd_list[indx]->handler_stop = true;
}
for (indx=0; indx<cam_cnt; indx++) {
cam_list[indx]->handler_shutdown();
cam_list[indx]->finish = true;
}
for (indx=0; indx<snd_cnt; indx++) {
snd_list[indx]->handler_shutdown();
snd_list[indx]->finish = true;
}

for (indx=0; indx<cam_cnt; indx++) {
cam_list[indx]->event_stop = true;
cam_list[indx]->restart = false;
cam_list[indx]->handler_stop = true;
cam_list[indx]->finish = true;
if (cam_list[indx]->camera_type == CAMERA_TYPE_NETCAM) {
if (cam_list[indx]->netcam != nullptr) {
cam_list[indx]->netcam->idur = 0;
}
if (cam_list[indx]->netcam_high != nullptr) {
cam_list[indx]->netcam_high->idur = 0;
}
}
}
for (indx=0; indx<cam_cnt; indx++) {
cam_list[indx]->handler_shutdown();
}

default:
break;
}
Expand Down
67 changes: 37 additions & 30 deletions src/netcam.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,11 @@ int netcam_interrupt(void *ctx)
clock_gettime(CLOCK_MONOTONIC, &netcam->icur_tm);
if ((netcam->icur_tm.tv_sec -
netcam->ist_tm.tv_sec ) > netcam->idur){
MOTPLS_LOG(INF, TYPE_NETCAM, NO_ERRNO
,_("%s:Camera reading (%s) timed out")
, netcam->cameratype.c_str(), netcam->camera_name.c_str());
if (netcam->cam->finish == false) {
MOTPLS_LOG(INF, TYPE_NETCAM, NO_ERRNO
,_("%s:Camera reading (%s) timed out")
, netcam->cameratype.c_str(), netcam->camera_name.c_str());
}
netcam->interrupted = true;
return true;
} else{
Expand All @@ -104,9 +106,11 @@ int netcam_interrupt(void *ctx)
*/
clock_gettime(CLOCK_MONOTONIC, &netcam->icur_tm);
if ((netcam->icur_tm.tv_sec - netcam->ist_tm.tv_sec ) > netcam->idur){
MOTPLS_LOG(INF, TYPE_NETCAM, NO_ERRNO
,_("%s:Camera (%s) timed out")
, netcam->cameratype.c_str(), netcam->camera_name.c_str());
if (netcam->cam->finish == false) {
MOTPLS_LOG(INF, TYPE_NETCAM, NO_ERRNO
,_("%s:Camera (%s) timed out")
, netcam->cameratype.c_str(), netcam->camera_name.c_str());
}
netcam->interrupted = true;
return true;
} else{
Expand All @@ -123,12 +127,6 @@ bool netcam_filelist_cmp(const ctx_filelist_item &a, const ctx_filelist_item &b)
return a.filenm < b.filenm;
}







void cls_netcam::filelist_load()
{
DIR *d;
Expand Down Expand Up @@ -1785,11 +1783,18 @@ int cls_netcam::open_context()
, path.c_str(), nullptr, &opts);
if ((retcd < 0) || (interrupted) || (handler_stop) ) {
if (status == NETCAM_NOTCONNECTED) {
av_strerror(retcd, errstr, sizeof(errstr));
MOTPLS_LOG(NTC, TYPE_NETCAM, NO_ERRNO
,_("%s:Unable to open camera(%s):%s")
, cameratype.c_str()
, camera_name.c_str(), errstr);
if (retcd < 0) {
av_strerror(retcd, errstr, sizeof(errstr));
MOTPLS_LOG(NTC, TYPE_NETCAM, NO_ERRNO
,_("%s:Unable to open camera(%s):%s")
, cameratype.c_str()
, camera_name.c_str(), errstr);
} else if (interrupted) {
MOTPLS_LOG(NTC, TYPE_NETCAM, NO_ERRNO
,_("%s:Unable to open camera(%s):timeout")
, cameratype.c_str()
, camera_name.c_str());
}
}
av_dict_free(&opts);
context_close();
Expand All @@ -1806,10 +1811,16 @@ int cls_netcam::open_context()
retcd = avformat_find_stream_info(format_context, nullptr);
if ((retcd < 0) || (interrupted) || (handler_stop) ) {
if (status == NETCAM_NOTCONNECTED) {
av_strerror(retcd, errstr, sizeof(errstr));
MOTPLS_LOG(ERR, TYPE_NETCAM, NO_ERRNO
,_("%s:Unable to find stream info:%s")
,cameratype.c_str(), errstr);
if (retcd < 0) {
av_strerror(retcd, errstr, sizeof(errstr));
MOTPLS_LOG(ERR, TYPE_NETCAM, NO_ERRNO
,_("%s:Unable to find stream info:%s")
,cameratype.c_str(), errstr);
} else if (interrupted) {
MOTPLS_LOG(ERR, TYPE_NETCAM, NO_ERRNO
,_("%s:Unable to find stream info:%s")
,cameratype.c_str(), errstr);
}
}
context_close();
return -1;
Expand All @@ -1825,13 +1836,12 @@ int cls_netcam::open_context()
retcd = open_codec();
mythreadname_set(nullptr, 0, threadname.c_str());
if ((retcd < 0) || (interrupted) || (handler_stop) ) {
av_strerror(retcd, errstr, sizeof(errstr));
if (status == NETCAM_NOTCONNECTED) {
av_strerror(retcd, errstr, sizeof(errstr));
MOTPLS_LOG(ERR, TYPE_NETCAM, NO_ERRNO
,_("%s:Unable to open codec context:%s")
,cameratype.c_str(), errstr);
} else {
av_strerror(retcd, errstr, sizeof(errstr));
MOTPLS_LOG(ERR, TYPE_NETCAM, NO_ERRNO
,_("%s:Connected and unable to open codec context:%s")
,cameratype.c_str(), errstr);
Expand Down Expand Up @@ -2158,14 +2168,13 @@ void cls_netcam::handler_shutdown()
{
int waitcnt;

MOTPLS_LOG(INF, TYPE_NETCAM, NO_ERRNO
,_("%s:Shutting down network camera.")
,cameratype.c_str());

idur = 0;
handler_stop = true;

if (handler_running == true) {
MOTPLS_LOG(INF, TYPE_NETCAM, NO_ERRNO
,_("%s:Shutting down network camera.")
,cameratype.c_str());
waitcnt = 0;
while ((handler_running == true) && (waitcnt < cam->cfg->watchdog_tmo)){
SLEEP(1,0)
Expand Down Expand Up @@ -2359,13 +2368,11 @@ cls_netcam::cls_netcam(cls_camera *p_cam, bool p_is_high)
pthread_mutex_init(&mutex_pktarray, nullptr);
pthread_mutex_init(&mutex_transfer, nullptr);

netcam_start();

}

cls_netcam::~cls_netcam()
{
netcam_stop();
handler_shutdown();

pthread_mutex_destroy(&mutex);
pthread_mutex_destroy(&mutex_pktarray);
Expand Down
13 changes: 6 additions & 7 deletions src/netcam.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,8 @@ class cls_netcam {
public:
cls_netcam(cls_camera *p_cam, bool p_is_high);
~cls_netcam();
int next(ctx_image_data *img_data);
void noimage();

cls_camera *cam;
bool interrupted; /* Boolean for whether interrupt has been tripped */
enum NETCAM_STATUS status; /* Status of whether the camera is connecting, closed, etc*/
struct timespec ist_tm; /* The time set before calling the av functions */
Expand All @@ -100,9 +99,12 @@ class cls_netcam {
pthread_t handler_thread;
void handler();

private:
cls_camera *cam;
int next(ctx_image_data *img_data);
void noimage();
void netcam_start();
void netcam_stop();

private:
AVFormatContext *format_context; /* Main format context for the camera */
AVCodecContext *codec_context; /* Codec being sent from the camera */
AVStream *strm;
Expand Down Expand Up @@ -203,9 +205,6 @@ class cls_netcam {
void handler_startup();
void handler_shutdown();

void netcam_start();
void netcam_stop();

};

#endif /* _INCLUDE_NETCAM_HPP_ */

0 comments on commit 34ea87e

Please sign in to comment.