From 398bf85d303c3465e2041e5ee7289faf8ce50d97 Mon Sep 17 00:00:00 2001 From: Mr-Dave Date: Sun, 22 May 2022 11:43:03 -0600 Subject: [PATCH] Add interrupt to movie --- src/motionplus.cpp | 8 +++-- src/movie.cpp | 82 +++++++++++++++++++++++++++++----------------- src/movie.hpp | 4 +++ 3 files changed, 62 insertions(+), 32 deletions(-) diff --git a/src/motionplus.cpp b/src/motionplus.cpp index 15d43f6e..8132f9d2 100644 --- a/src/motionplus.cpp +++ b/src/motionplus.cpp @@ -505,11 +505,15 @@ static void motion_watchdog(struct ctx_motapp *motapp, int indx) , motapp->cam_list[indx]->threadnr); if ((motapp->cam_list[indx]->camera_type == CAMERA_TYPE_NETCAM) && (motapp->cam_list[indx]->netcam != NULL)) { - pthread_cancel(motapp->cam_list[indx]->netcam->thread_id); + if (motapp->cam_list[indx]->netcam->handler_finished == false) { + pthread_cancel(motapp->cam_list[indx]->netcam->thread_id); + } } if ((motapp->cam_list[indx]->camera_type == CAMERA_TYPE_NETCAM) && (motapp->cam_list[indx]->netcam_high != NULL)) { - pthread_cancel(motapp->cam_list[indx]->netcam_high->thread_id); + if (motapp->cam_list[indx]->netcam_high->handler_finished == false) { + pthread_cancel(motapp->cam_list[indx]->netcam_high->thread_id); + } } pthread_cancel(motapp->cam_list[indx]->thread_id); } diff --git a/src/movie.cpp b/src/movie.cpp index 15f43838..547b7eb1 100644 --- a/src/movie.cpp +++ b/src/movie.cpp @@ -785,9 +785,23 @@ static int movie_set_picture(struct ctx_movie *movie) } +static int movie_interrupt(void *ctx) +{ + struct ctx_movie *movie = (struct ctx_movie *)ctx; + + clock_gettime(CLOCK_MONOTONIC, &movie->cb_cr_ts); + if ((movie->cb_cr_ts.tv_sec - movie->cb_st_ts.tv_sec ) > movie->cb_dur) { + MOTION_LOG(INF, TYPE_ENCODER, NO_ERRNO,_("Movie timed out")); + return 1; + } else{ + return 0; + } + return 0; +} + + static int movie_set_outputfile(struct ctx_movie *movie) { - int retcd; char errstr[128]; @@ -802,38 +816,38 @@ static int movie_set_outputfile(struct ctx_movie *movie) /* Open the output file, if needed. */ if ((movie_timelapse_exists(movie->filename) == 0) || (movie->tlapse != TIMELAPSE_APPEND)) { - if (!(movie->oc->oformat->flags & AVFMT_NOFILE)) { - if (avio_open(&movie->oc->pb, movie->filename, MY_FLAG_WRITE) < 0) { - if (errno == ENOENT) { - if (mycreate_path(movie->filename) == -1) { - movie_free_context(movie); - return -1; - } - if (avio_open(&movie->oc->pb, movie->filename, MY_FLAG_WRITE) < 0) { - MOTION_LOG(ERR, TYPE_ENCODER, SHOW_ERRNO - ,_("error opening file %s"), movie->filename); - movie_free_context(movie); - return -1; - } - /* Permission denied */ - } else if (errno == EACCES) { - MOTION_LOG(ERR, TYPE_ENCODER, SHOW_ERRNO - ,_("Permission denied. %s"),movie->filename); - movie_free_context(movie); - return -1; - } else { - MOTION_LOG(ERR, TYPE_ENCODER, SHOW_ERRNO - ,_("Error opening file %s"), movie->filename); + clock_gettime(CLOCK_MONOTONIC, &movie->cb_st_ts); + retcd = avio_open(&movie->oc->pb, movie->filename, MY_FLAG_WRITE); + if (retcd < 0) { + av_strerror(retcd, errstr, sizeof(errstr)); + MOTION_LOG(ERR, TYPE_ENCODER, SHOW_ERRNO + ,_("avio_open: %s File %s") + , errstr, movie->filename); + if (errno == ENOENT) { + if (mycreate_path(movie->filename) == -1) { movie_free_context(movie); return -1; } + clock_gettime(CLOCK_MONOTONIC, &movie->cb_st_ts); + retcd = avio_open(&movie->oc->pb, movie->filename, MY_FLAG_WRITE); + if (retcd < 0) { + av_strerror(retcd, errstr, sizeof(errstr)); + MOTION_LOG(ERR, TYPE_ENCODER, SHOW_ERRNO + ,_("error %s opening file %s") + , errstr, movie->filename); + movie_free_context(movie); + return -1; + } + } else { + MOTION_LOG(ERR, TYPE_ENCODER, SHOW_ERRNO + ,_("Error opening file %s") + , movie->filename); + movie_free_context(movie); + return -1; } } - /* Write the stream header, For the TIMELAPSE_APPEND - * we write the data via standard file I/O so we close the - * items here - */ + clock_gettime(CLOCK_MONOTONIC, &movie->cb_st_ts); retcd = avformat_write_header(movie->oc, NULL); if (retcd < 0) { av_strerror(retcd, errstr, sizeof(errstr)); @@ -842,6 +856,7 @@ static int movie_set_outputfile(struct ctx_movie *movie) movie_free_context(movie); return -1; } + /* TIMELAPSE_APPEND uses standard file IO so we close it */ if (movie->tlapse == TIMELAPSE_APPEND) { av_write_trailer(movie->oc); avio_close(movie->oc->pb); @@ -855,7 +870,6 @@ static int movie_set_outputfile(struct ctx_movie *movie) static int movie_flush_codec(struct ctx_movie *movie) { - #if (MYFFVER >= 57041) //ffmpeg version 3.1 and after @@ -1051,7 +1065,6 @@ static void movie_passthru_write(struct ctx_movie *movie, int indx) static void movie_passthru_minpts(struct ctx_movie *movie) { - int indx, indx_audio, indx_video; movie->pass_audio_base = 0; @@ -1313,6 +1326,9 @@ static int movie_passthru_open(struct ctx_movie *movie) movie_free_context(movie); return -1; } + movie->oc->interrupt_callback.callback = movie_interrupt; + movie->oc->interrupt_callback.opaque = movie; + movie->cb_dur = 3; if (mystrne(movie->container_name, "mp4") && mystrne(movie->container_name, "mkv")) { MOTION_LOG(NTC, TYPE_ENCODER, NO_ERRNO @@ -1486,6 +1502,10 @@ int movie_open(struct ctx_movie *movie) movie_free_context(movie); return -1; } + clock_gettime(CLOCK_MONOTONIC, &movie->cb_st_ts); + movie->cb_dur = 3; + movie->oc->interrupt_callback.callback = movie_interrupt; + movie->oc->interrupt_callback.opaque = movie; retcd = movie_get_oformat(movie); if (retcd < 0 ) { @@ -1534,6 +1554,7 @@ void movie_close(struct ctx_movie *movie) { if (movie != NULL) { + clock_gettime(CLOCK_MONOTONIC, &movie->cb_st_ts); if (movie_flush_codec(movie) < 0) { MOTION_LOG(ERR, TYPE_ENCODER, NO_ERRNO, _("Error flushing codec")); @@ -1557,10 +1578,11 @@ void movie_close(struct ctx_movie *movie) int movie_put_image(struct ctx_movie *movie, struct ctx_image_data *img_data, const struct timespec *ts1) { - int retcd = 0; int cnt = 0; + clock_gettime(CLOCK_MONOTONIC, &movie->cb_st_ts); + if (movie->passthrough) { retcd = movie_passthru_put(movie, img_data); return retcd; diff --git a/src/movie.hpp b/src/movie.hpp index e12875b0..b02fbf49 100644 --- a/src/movie.hpp +++ b/src/movie.hpp @@ -76,6 +76,10 @@ struct ctx_movie { enum USER_CODEC preferred_codec; char *nal_info; int nal_info_len; + struct timespec cb_st_ts; /* The time set before calling the av functions */ + struct timespec cb_cr_ts; /* Time during the interrupt to determine duration since start*/ + int cb_dur; /* Seconds permitted before triggering a interrupt */ + };