From 162d5940f06e97d460116408345ec1cb2a7d14bb Mon Sep 17 00:00:00 2001 From: Nicolas Date: Tue, 23 Jun 2026 13:43:11 +0200 Subject: [PATCH 1/2] add more log for debug info + last frame keeping --- source/webcam.cpp | 24 +++++++++++++++++------- source/webcam.h | 1 + 2 files changed, 18 insertions(+), 7 deletions(-) diff --git a/source/webcam.cpp b/source/webcam.cpp index 6b9f8df..28516ce 100644 --- a/source/webcam.cpp +++ b/source/webcam.cpp @@ -9,6 +9,8 @@ #include "opencv2\opencv.hpp" #include #include +#include + using namespace GS; using namespace GS::Script; @@ -36,10 +38,12 @@ void ProcessWebcamThread::Execute() { bool temp_not_finish = true; bool temp_pause = true; bool temp_record_frame = false; - + current_frame_clock = 0.0; //construction LUT pour synchro avec la clock squirrel std::ofstream lutFile(outputVideoName+"_lut.bin", std::ios::binary); __LOG_CAM__ << "NEW RECORDING THREAD "<read(*im); - outputVideo->write(*im); + InputVideo->read(*im); + + __LOG_CAM__ << "Record frame clock: " << (float)current_frame_clock << "at index: "<< frame_index<<"\n"; + outputVideo->write(*im); + frame_index++; lutFile.write(reinterpret_cast(¤t_frame_clock),sizeof(float)); } } @@ -317,6 +324,8 @@ void Webcam::Close() { uchar *Webcam::SetFrame(float clock, GS::String record_name , int record_index) { //create buffer from lut + bool is_the_last_record = record_index == index_save_video - 1 ; + if(video_times.size() == 0){ //charger lut dans video_times __LOG_CAM__ << "Chargement LUT: "<(sizeof(float))); lutFile.read(reinterpret_cast(&end_time), sizeof(float)); + __LOG_CAM__ <<"end: "<<(float)end_time<<" asked clock: "<(&start_time), sizeof(float)); - + if(clock < start_time){ __LOG_CAM__ << "clock < start_time\n"; lutFile.close(); return NULL; } - else if(clock > end_time){ + else if(clock > end_time && !is_the_last_record){ __LOG_CAM__ << "clock > end_time\n"; lutFile.close(); return SetFrame(clock,record_name,record_index+1); //recursive search forward @@ -383,7 +392,8 @@ uchar *Webcam::SetFrame(float clock, GS::String record_name , int record_index) return NULL; //force reset if clock not in range - if(clock < video_times[0] || clock > video_times[video_times.size() -1]){ + if(clock < video_times[0] || (clock > video_times[video_times.size() -1] && !is_the_last_record)){ + __LOG_CAM__ << "CLEAR \n"; video_times.clear(); return SetFrame(clock, record_name , 0); } diff --git a/source/webcam.h b/source/webcam.h index a38b8a5..ac617ed 100644 --- a/source/webcam.h +++ b/source/webcam.h @@ -71,6 +71,7 @@ private: int index_save_video; int index_replay_video; + std::vector video_times; cv::VideoCapture *ReplayInputVideo; cv::Mat* ReplayIm; From 5131a4b09cb282aab6ec6dcc232a5e7671b84a74 Mon Sep 17 00:00:00 2001 From: Nicolas Date: Wed, 24 Jun 2026 11:27:16 +0200 Subject: [PATCH 2/2] changing codec for loss less cause xvid lost some frame at end causing crashes --- source/webcam.cpp | 43 ++++++++++++++++++++++++++----------------- 1 file changed, 26 insertions(+), 17 deletions(-) diff --git a/source/webcam.cpp b/source/webcam.cpp index 28516ce..4b19d34 100644 --- a/source/webcam.cpp +++ b/source/webcam.cpp @@ -63,11 +63,14 @@ void ProcessWebcamThread::Execute() { else{ InputVideo->read(*im); - - __LOG_CAM__ << "Record frame clock: " << (float)current_frame_clock << "at index: "<< frame_index<<"\n"; - outputVideo->write(*im); - frame_index++; - lutFile.write(reinterpret_cast(¤t_frame_clock),sizeof(float)); + mutex_pause.Lock(); + temp_pause = pause; + mutex_pause.Unlock(); + if(!temp_pause){ + outputVideo->write(*im); + frame_index++; + lutFile.write(reinterpret_cast(¤t_frame_clock),sizeof(float)); + } } } __LOG_CAM__ << "THREAD STOPPED "<release(); lutFile.close(); + Platform::Get().Sleep(50); // Laisser le fichier se finaliser + delete im; } //********************************************************* @@ -238,8 +243,7 @@ void Webcam::ResetVideo() { std::stringstream video_name; video_name << "./webcam_record_" << index_save_video << ".avi"; // set the codec fourcc - unsigned int ex = outputVideo->fourcc('X', 'V', 'I', 'D'); - outputVideo->set(VIDEOWRITER_PROP_QUALITY, 70); + unsigned int ex = outputVideo->fourcc('M', 'J', 'P', 'G'); outputVideo->open(video_name.str(), ex, webcam_fps, Size(width, height), true); if (!outputVideo->isOpened()) @@ -275,10 +279,14 @@ void Webcam::setPause(bool pause) { bool thread_pause = thread_process->pause; thread_process->mutex_pause.Unlock(); + thread_process->mutex_pause.Lock(); + thread_process->pause = pause; + thread_process->mutex_pause.Unlock(); if (!thread_pause && pause) { __LOG_CAM__ <<"B\n"; Close(); + Platform::Get().Sleep(50); // Laisser le fichier se finaliser __LOG_CAM__ << "REPLAY INPUT RELEASE ./webcam_record_" << index_replay_video << ".avi\n "; ReplayInputVideo->release(); index_replay_video = index_save_video - 1; @@ -319,7 +327,8 @@ void Webcam::Close() { thread_process = nullptr; } - ReplayInputVideo->release(); + if(ReplayInputVideo) + ReplayInputVideo->release(); } uchar *Webcam::SetFrame(float clock, GS::String record_name , int record_index) { @@ -368,11 +377,11 @@ uchar *Webcam::SetFrame(float clock, GS::String record_name , int record_index) } if (record_index != index_replay_video) { - __LOG_H__ << "Changing video\n"; + __LOG_CAM__ << "Changing video\n"; index_replay_video = record_index; std::stringstream video_name; video_name << record_name << "_" << index_replay_video << ".avi"; - __LOG_H__ << record_name << "_" << index_replay_video << ".avi"; + __LOG_CAM__ << record_name << "_" << index_replay_video << ".avi\n"; ReplayInputVideo->release(); ReplayInputVideo->open(video_name.str()); } @@ -402,26 +411,26 @@ uchar *Webcam::SetFrame(float clock, GS::String record_name , int record_index) if (it == video_times.begin()) { - // clock before first frame __LOG_CAM__ << "clock before first frame\n"; - //SetFrame(clock,record_name-1) return nullptr; } size_t index = std::distance(video_times.begin(), it) - 1; - + if (!ReplayInputVideo || !ReplayInputVideo->isOpened()) { __LOG_CAM__ << "ReplayInputVideo not opened\n"; return NULL; } - __LOG_CAM__ << "Index found "<get(cv::CAP_PROP_FRAME_COUNT) - 1; ReplayInputVideo->set(cv::CAP_PROP_POS_FRAMES, index); + if (!ReplayInputVideo->read(*ReplayImframe)) - { - __LOG_CAM__ << "Failed to read frame at index " << index << "\n"; - return NULL; + { + __LOG_CAM__ << "Failed to read frame at index " << index << "\n"; + return NULL; } cv::cvtColor(*ReplayImframe, *ReplayIm, CV_BGR2RGBA, 4);