From 59f4409ab4d6c3f0603d1244556c2dbc98d58bfb Mon Sep 17 00:00:00 2001 From: JDierkse Date: Thu, 16 Mar 2023 13:40:38 +0100 Subject: [PATCH] Add RecorderMutex --- API/WebAPI.cpp | 14 ++++++------ API/WebAPI.h | 3 ++- Application/CameraRecorder.cc | 4 +++- Recorder/DigooRecorder.cpp | 3 ++- Recorder/DigooRecorder.h | 2 +- Recorder/FoscamRecorder.cpp | 38 +++++++++++++++++++++++++++++---- Recorder/FoscamRecorder.h | 2 +- Recorder/RTSPRecorder.cpp | 40 +++++++++++++++++++++++++++++++---- Recorder/RTSPRecorder.h | 2 +- Recorder/Recorder.cpp | 3 ++- Recorder/Recorder.h | 6 +++++- Recorder/VStarCamRecorder.cpp | 40 +++++++++++++++++++++++++++++++---- Recorder/VStarCamRecorder.h | 2 +- Recorder/WatchBotRecorder.cpp | 40 +++++++++++++++++++++++++++++++---- Recorder/WatchBotRecorder.h | 2 +- Recorder/ZModoRecorder.cpp | 3 ++- Recorder/ZModoRecorder.h | 2 +- Util/RecorderMutex.cpp | 31 +++++++++++++++++++++++++++ Util/RecorderMutex.h | 31 +++++++++++++++++++++++++++ 19 files changed, 233 insertions(+), 35 deletions(-) create mode 100644 Util/RecorderMutex.cpp create mode 100644 Util/RecorderMutex.h diff --git a/API/WebAPI.cpp b/API/WebAPI.cpp index 3ee2e00..38e81e7 100644 --- a/API/WebAPI.cpp +++ b/API/WebAPI.cpp @@ -30,7 +30,7 @@ WebAPI::~WebAPI() { } -Http::HttpServer::HttpReply WebAPI::ProcessQuery(ctpl::thread_pool* pThreadPool, Http::HttpClient* pHttpClient, const std::string& path, const std::string& ffmpeg, const std::string& uri, const std::vector& postData) +Http::HttpServer::HttpReply WebAPI::ProcessQuery(ctpl::thread_pool* pThreadPool, Http::HttpClient* pHttpClient, Util::RecorderMutexPointer pRecorderMutex, const std::string& path, const std::string& ffmpeg, const std::string& uri, const std::vector& postData) { if (!StringAlgorithm::iequals(uri, "/api") && !StringAlgorithm::istarts_with(uri, "/api/")) { @@ -69,7 +69,7 @@ Http::HttpServer::HttpReply WebAPI::ProcessQuery(ctpl::thread_pool* pThreadPool, reply.status = Http::HttpServer::HttpReply::Status::Ok; if (StringAlgorithm::iequals("Digoo", brand)) { - auto recorder = Recorder::DigooRecorder(pHttpClient, settings); + auto recorder = Recorder::DigooRecorder(pHttpClient, settings, pRecorderMutex); if (StringAlgorithm::iequals("Snapshot", action)) reply.content = recorder.Snapshot(pThreadPool, ffmpeg); else if (StringAlgorithm::iequals("MultiSnapshot", action)) @@ -79,7 +79,7 @@ Http::HttpServer::HttpReply WebAPI::ProcessQuery(ctpl::thread_pool* pThreadPool, } else if (StringAlgorithm::iequals("Foscam", brand)) { - auto recorder = Recorder::FoscamRecorder(pHttpClient, settings); + auto recorder = Recorder::FoscamRecorder(pHttpClient, settings, pRecorderMutex); if (StringAlgorithm::iequals("Snapshot", action)) reply.content = recorder.Snapshot(pThreadPool, ffmpeg); else if (StringAlgorithm::iequals("MultiSnapshot", action)) @@ -89,7 +89,7 @@ Http::HttpServer::HttpReply WebAPI::ProcessQuery(ctpl::thread_pool* pThreadPool, } else if (StringAlgorithm::iequals("RTSP", brand)) { - auto recorder = Recorder::RTSPRecorder(pHttpClient, settings); + auto recorder = Recorder::RTSPRecorder(pHttpClient, settings, pRecorderMutex); if (StringAlgorithm::iequals("Snapshot", action)) reply.content = recorder.Snapshot(pThreadPool, ffmpeg); else if (StringAlgorithm::iequals("MultiSnapshot", action)) @@ -99,7 +99,7 @@ Http::HttpServer::HttpReply WebAPI::ProcessQuery(ctpl::thread_pool* pThreadPool, } else if (StringAlgorithm::iequals("VStarCam", brand)) { - auto recorder = Recorder::VStarCamRecorder(pHttpClient, settings); + auto recorder = Recorder::VStarCamRecorder(pHttpClient, settings, pRecorderMutex); if (StringAlgorithm::iequals("Snapshot", action)) reply.content = recorder.Snapshot(pThreadPool, ffmpeg); else if (StringAlgorithm::iequals("MultiSnapshot", action)) @@ -109,7 +109,7 @@ Http::HttpServer::HttpReply WebAPI::ProcessQuery(ctpl::thread_pool* pThreadPool, } else if (StringAlgorithm::iequals("WatchBot", brand)) { - auto recorder = Recorder::WatchBotRecorder(pHttpClient, settings); + auto recorder = Recorder::WatchBotRecorder(pHttpClient, settings, pRecorderMutex); if (StringAlgorithm::iequals("Snapshot", action)) reply.content = recorder.Snapshot(pThreadPool, ffmpeg); else if (StringAlgorithm::iequals("MultiSnapshot", action)) @@ -119,7 +119,7 @@ Http::HttpServer::HttpReply WebAPI::ProcessQuery(ctpl::thread_pool* pThreadPool, } else if (StringAlgorithm::iequals("ZModo", brand)) { - auto recorder = Recorder::ZModoRecorder(pHttpClient, settings); + auto recorder = Recorder::ZModoRecorder(pHttpClient, settings, pRecorderMutex); if (StringAlgorithm::iequals("Snapshot", action)) reply.content = recorder.Snapshot(pThreadPool, ffmpeg); else if (StringAlgorithm::iequals("MultiSnapshot", action)) diff --git a/API/WebAPI.h b/API/WebAPI.h index 48005c5..6de72e9 100644 --- a/API/WebAPI.h +++ b/API/WebAPI.h @@ -1,6 +1,7 @@ #ifndef API_WEBAPI_H #define API_WEBAPI_H +#include "Util/RecorderMutex.h" #include #include #include @@ -23,7 +24,7 @@ public: WebAPI(); ~WebAPI(); - static Http::HttpServer::HttpReply ProcessQuery(ctpl::thread_pool* pThreadPool, Http::HttpClient* pHttpClient, const std::string& path, const std::string& ffmpeg, const std::string& uri, const std::vector& postData); + static Http::HttpServer::HttpReply ProcessQuery(ctpl::thread_pool* pThreadPool, Http::HttpClient* pHttpClient, Util::RecorderMutexPointer pRecorderMutex, const std::string& path, const std::string& ffmpeg, const std::string& uri, const std::vector& postData); }; } // namespace API diff --git a/Application/CameraRecorder.cc b/Application/CameraRecorder.cc index 4fe1881..d1bca59 100644 --- a/Application/CameraRecorder.cc +++ b/Application/CameraRecorder.cc @@ -1,4 +1,5 @@ #include "API/WebAPI.h" +#include "Util/RecorderMutex.h" #include #include #include @@ -41,7 +42,8 @@ int main(int argc, char** argv) ctpl::thread_pool threadPool(6); Http::HttpClient httpClient; - Http::HttpServer::CallbackMethod callback = std::bind(&CameraRecorder::API::WebAPI::ProcessQuery, &threadPool, &httpClient, path.str(), ffmpeg, std::placeholders::_1, std::placeholders::_2); + CameraRecorder::Util::RecorderMutex recorderMutex; + Http::HttpServer::CallbackMethod callback = std::bind(&CameraRecorder::API::WebAPI::ProcessQuery, &threadPool, &httpClient, &recorderMutex, path.str(), ffmpeg, std::placeholders::_1, std::placeholders::_2); Http::HttpServer server(port, callback); Logging::Log(Logging::Severity::Info, "Startup Complete"); diff --git a/Recorder/DigooRecorder.cpp b/Recorder/DigooRecorder.cpp index 57cb07e..07f363d 100644 --- a/Recorder/DigooRecorder.cpp +++ b/Recorder/DigooRecorder.cpp @@ -6,7 +6,8 @@ namespace CameraRecorder { namespace Recorder { -DigooRecorder::DigooRecorder(Http::HttpClient* pHttpClient, const Settings& settings) : +DigooRecorder::DigooRecorder(Http::HttpClient* pHttpClient, const Settings& settings, Util::RecorderMutexPointer pRecorderMutex) : + Recorder(pRecorderMutex), m_pHttpClient(pHttpClient), m_settings(settings) { diff --git a/Recorder/DigooRecorder.h b/Recorder/DigooRecorder.h index 4b75470..8b4cf8b 100644 --- a/Recorder/DigooRecorder.h +++ b/Recorder/DigooRecorder.h @@ -17,7 +17,7 @@ namespace Recorder { class DigooRecorder : public virtual Recorder { public: - DigooRecorder(Http::HttpClient* pHttpClient, const Settings& settings); + DigooRecorder(Http::HttpClient* pHttpClient, const Settings& settings, Util::RecorderMutexPointer pRecorderMutex); ~DigooRecorder(); virtual std::string Snapshot(ctpl::thread_pool* pThreadPool, const std::string& ffmpeg) override; diff --git a/Recorder/FoscamRecorder.cpp b/Recorder/FoscamRecorder.cpp index d43c569..ba3e098 100644 --- a/Recorder/FoscamRecorder.cpp +++ b/Recorder/FoscamRecorder.cpp @@ -13,7 +13,8 @@ namespace CameraRecorder { namespace Recorder { -FoscamRecorder::FoscamRecorder(Http::HttpClient* pHttpClient, const Settings& settings) : +FoscamRecorder::FoscamRecorder(Http::HttpClient* pHttpClient, const Settings& settings, Util::RecorderMutexPointer pRecorderMutex) : + Recorder(pRecorderMutex), m_pHttpClient(pHttpClient), m_settings(settings) { @@ -56,7 +57,12 @@ std::string FoscamRecorder::Snapshot(ctpl::thread_pool* pThreadPool, const std:: Http::HttpClient* pHttpClient = m_pHttpClient; Settings settings = m_settings; - FoscamRecorder::Snapshots(pHttpClient, settings, dateTimeString, 1); + //Util::RecorderMutexPointer pRecorderMutex = m_pRecorderMutex; + //pThreadPool->push( [pHttpClient, settings, dateTimeString, pRecorderMutex](int) { + // pRecorderMutex->RegisterRecording(settings.IpAddress); + FoscamRecorder::Snapshots(pHttpClient, settings, dateTimeString, 1); + // pRecorderMutex->UnregisterRecording(settings.IpAddress); + //} ); return fileName; } @@ -85,12 +91,24 @@ std::string FoscamRecorder::MultiSnapshot(ctpl::thread_pool* pThreadPool, const return std::string(); } } + + if (m_pRecorderMutex->IsRecording(m_settings.IpAddress)) + { + Logging::Log(Logging::Severity::Debug, "FoscamRecorder MultiSnapshot Cancelled, Recording in progress"); + return std::string(); + } + std::string dateTimeString = Util::GetDateTimeString(); std::string fileName = GetFileName(m_settings, "jpg", dateTimeString); Http::HttpClient* pHttpClient = m_pHttpClient; Settings settings = m_settings; - pThreadPool->push( [pHttpClient, settings, dateTimeString, numberOfImages](int) { FoscamRecorder::Snapshots(pHttpClient, settings, dateTimeString, numberOfImages); } ); + Util::RecorderMutexPointer pRecorderMutex = m_pRecorderMutex; + pThreadPool->push( [pHttpClient, settings, dateTimeString, numberOfImages, pRecorderMutex](int) { + pRecorderMutex->RegisterRecording(settings.IpAddress); + FoscamRecorder::Snapshots(pHttpClient, settings, dateTimeString, numberOfImages); + pRecorderMutex->UnregisterRecording(settings.IpAddress); + } ); return fileName; } @@ -124,6 +142,12 @@ std::string FoscamRecorder::Video(ctpl::thread_pool* pThreadPool, const std::str } } + if (m_pRecorderMutex->IsRecording(m_settings.IpAddress)) + { + Logging::Log(Logging::Severity::Debug, "FoscamRecorder Video Cancelled, Recording in progress"); + return std::string(); + } + std::string dateTimeString = Util::GetDateTimeString(); std::string fileName = GetFileName(m_settings, "mp4", dateTimeString); @@ -132,7 +156,13 @@ std::string FoscamRecorder::Video(ctpl::thread_pool* pThreadPool, const std::str std::string cmd(command.str()); Logging::Log(Logging::Severity::Debug, cmd); - pThreadPool->push( [cmd](int) { int retval = std::system(cmd.c_str()); } ); + Settings settings = m_settings; + Util::RecorderMutexPointer pRecorderMutex = m_pRecorderMutex; + pThreadPool->push( [cmd, settings, pRecorderMutex](int) { + pRecorderMutex->RegisterRecording(settings.IpAddress); + int retval = std::system(cmd.c_str()); + pRecorderMutex->UnregisterRecording(settings.IpAddress); + } ); return fileName; } diff --git a/Recorder/FoscamRecorder.h b/Recorder/FoscamRecorder.h index 7f91da7..7fc52ae 100644 --- a/Recorder/FoscamRecorder.h +++ b/Recorder/FoscamRecorder.h @@ -18,7 +18,7 @@ namespace Recorder { class FoscamRecorder : public virtual Recorder { public: - FoscamRecorder(Http::HttpClient* pHttpClient, const Settings& settings); + FoscamRecorder(Http::HttpClient* pHttpClient, const Settings& settings, Util::RecorderMutexPointer pRecorderMutex); ~FoscamRecorder(); virtual std::string Snapshot(ctpl::thread_pool* pThreadPool, const std::string& ffmpeg) override; diff --git a/Recorder/RTSPRecorder.cpp b/Recorder/RTSPRecorder.cpp index b361659..30f04f2 100644 --- a/Recorder/RTSPRecorder.cpp +++ b/Recorder/RTSPRecorder.cpp @@ -8,7 +8,8 @@ namespace CameraRecorder { namespace Recorder { -RTSPRecorder::RTSPRecorder(Http::HttpClient* pHttpClient, const Settings& settings) : +RTSPRecorder::RTSPRecorder(Http::HttpClient* pHttpClient, const Settings& settings, Util::RecorderMutexPointer pRecorderMutex) : + Recorder(pRecorderMutex), m_pHttpClient(pHttpClient), m_settings(settings) { @@ -43,12 +44,20 @@ std::string RTSPRecorder::Snapshot(ctpl::thread_pool* pThreadPool, const std::st } } + //if (m_pRecorderMutex->IsRecording(m_settings.IpAddress)) + // return std::string(); + std::string dateTimeString = Util::GetDateTimeString(); std::string fileName = GetFileName(m_settings, "jpg", dateTimeString, 0); Http::HttpClient* pHttpClient = m_pHttpClient; Settings settings = m_settings; - RTSPRecorder::Snapshots(ffmpeg, settings, dateTimeString, 1); + //Util::RecorderMutexPointer pRecorderMutex = m_pRecorderMutex; + //pThreadPool->push( [ffmpeg, settings, dateTimeString, pRecorderMutex](int) { + // pRecorderMutex->RegisterRecording(settings.IpAddress); + RTSPRecorder::Snapshots(ffmpeg, settings, dateTimeString, 1); + // pRecorderMutex->UnregisterRecording(settings.IpAddress); + //} ); return fileName; } @@ -78,12 +87,23 @@ std::string RTSPRecorder::MultiSnapshot(ctpl::thread_pool* pThreadPool, const st } } + if (m_pRecorderMutex->IsRecording(m_settings.IpAddress)) + { + Logging::Log(Logging::Severity::Debug, "RTSPRecorder MultiSnapshot Cancelled, Recording in progress"); + return std::string(); + } + std::string dateTimeString = Util::GetDateTimeString(); std::string fileName = GetFileName(m_settings, "jpg", dateTimeString, 0); Http::HttpClient* pHttpClient = m_pHttpClient; Settings settings = m_settings; - pThreadPool->push( [ffmpeg, settings, dateTimeString, numberOfImages](int) { RTSPRecorder::Snapshots(ffmpeg, settings, dateTimeString, numberOfImages); } ); + Util::RecorderMutexPointer pRecorderMutex = m_pRecorderMutex; + pThreadPool->push( [ffmpeg, settings, dateTimeString, numberOfImages, pRecorderMutex](int) { + pRecorderMutex->RegisterRecording(settings.IpAddress); + RTSPRecorder::Snapshots(ffmpeg, settings, dateTimeString, numberOfImages); + pRecorderMutex->UnregisterRecording(settings.IpAddress); + } ); return fileName; } @@ -116,6 +136,12 @@ std::string RTSPRecorder::Video(ctpl::thread_pool* pThreadPool, const std::strin } } + if (m_pRecorderMutex->IsRecording(m_settings.IpAddress)) + { + Logging::Log(Logging::Severity::Debug, "RTSPRecorder Video Cancelled, Recording in progress"); + return std::string(); + } + std::string dateTimeString = Util::GetDateTimeString(); std::string fileName = GetFileName(m_settings, "mp4", dateTimeString, 0); @@ -124,7 +150,13 @@ std::string RTSPRecorder::Video(ctpl::thread_pool* pThreadPool, const std::strin std::string cmd(command.str()); Logging::Log(Logging::Severity::Debug, cmd); - pThreadPool->push( [cmd](int) { int retval = std::system(cmd.c_str()); } ); + Settings settings = m_settings; + Util::RecorderMutexPointer pRecorderMutex = m_pRecorderMutex; + pThreadPool->push( [cmd, settings, pRecorderMutex](int) { + pRecorderMutex->RegisterRecording(settings.IpAddress); + int retval = std::system(cmd.c_str()); + pRecorderMutex->UnregisterRecording(settings.IpAddress); + } ); return fileName; } diff --git a/Recorder/RTSPRecorder.h b/Recorder/RTSPRecorder.h index f836080..250bc8e 100644 --- a/Recorder/RTSPRecorder.h +++ b/Recorder/RTSPRecorder.h @@ -18,7 +18,7 @@ namespace Recorder { class RTSPRecorder : public virtual Recorder { public: - RTSPRecorder(Http::HttpClient* pHttpClient, const Settings& settings); + RTSPRecorder(Http::HttpClient* pHttpClient, const Settings& settings, Util::RecorderMutexPointer pRecorderMutex); ~RTSPRecorder(); virtual std::string Snapshot(ctpl::thread_pool* pThreadPool, const std::string& ffmpeg) override; diff --git a/Recorder/Recorder.cpp b/Recorder/Recorder.cpp index e532dd5..3caecfb 100644 --- a/Recorder/Recorder.cpp +++ b/Recorder/Recorder.cpp @@ -4,7 +4,8 @@ namespace CameraRecorder { namespace Recorder { -Recorder::Recorder() +Recorder::Recorder(Util::RecorderMutexPointer pRecorderMutex) : + m_pRecorderMutex(pRecorderMutex) { } diff --git a/Recorder/Recorder.h b/Recorder/Recorder.h index ba0554e..7bf59f1 100644 --- a/Recorder/Recorder.h +++ b/Recorder/Recorder.h @@ -1,6 +1,7 @@ #ifndef RECORDER_RECORDER_H #define RECORDER_RECORDER_H +#include "Util/RecorderMutex.h" #include #include @@ -11,12 +12,15 @@ namespace Recorder { class Recorder { public: - Recorder(); + Recorder(Util::RecorderMutexPointer pRecorderMutex); ~Recorder(); virtual std::string Snapshot(ctpl::thread_pool* pThreadPool, const std::string& ffmpeg) = 0; virtual std::string MultiSnapshot(ctpl::thread_pool* pThreadPool, const std::string& ffmpeg, int numberOfImages) = 0; virtual std::string Video(ctpl::thread_pool* pThreadPool, const std::string& ffmpeg) = 0; + +protected: + Util::RecorderMutexPointer m_pRecorderMutex; }; } // namespace Recorder diff --git a/Recorder/VStarCamRecorder.cpp b/Recorder/VStarCamRecorder.cpp index 2cf940a..7afacd4 100644 --- a/Recorder/VStarCamRecorder.cpp +++ b/Recorder/VStarCamRecorder.cpp @@ -10,7 +10,8 @@ namespace CameraRecorder { namespace Recorder { -VStarCamRecorder::VStarCamRecorder(Http::HttpClient* pHttpClient, const Settings& settings) : +VStarCamRecorder::VStarCamRecorder(Http::HttpClient* pHttpClient, const Settings& settings, Util::RecorderMutexPointer pRecorderMutex) : + Recorder(pRecorderMutex), m_pHttpClient(pHttpClient), m_settings(settings) { @@ -45,12 +46,20 @@ std::string VStarCamRecorder::Snapshot(ctpl::thread_pool* pThreadPool, const std } } + //if (m_pRecorderMutex->IsRecording(m_settings.IpAddress)) + // return std::string(); + std::string dateTimeString = Util::GetDateTimeString(true); std::string fileName = GetFileName(m_settings, "jpg", dateTimeString, 0); Http::HttpClient* pHttpClient = m_pHttpClient; Settings settings = m_settings; - VStarCamRecorder::Snapshots(pHttpClient, settings, dateTimeString, 1); + //Util::RecorderMutexPointer pRecorderMutex = m_pRecorderMutex; + //pThreadPool->push( [pHttpClient, settings, dateTimeString, pRecorderMutex](int) { + // pRecorderMutex->RegisterRecording(settings.IpAddress); + VStarCamRecorder::Snapshots(pHttpClient, settings, dateTimeString, 1); + // pRecorderMutex->UnregisterRecording(settings.IpAddress); + //} ); return fileName; } @@ -80,12 +89,23 @@ std::string VStarCamRecorder::MultiSnapshot(ctpl::thread_pool* pThreadPool, cons } } + if (m_pRecorderMutex->IsRecording(m_settings.IpAddress)) + { + Logging::Log(Logging::Severity::Debug, "VStarCamRecorder MultiSnapshot Cancelled, Recording in progress"); + return std::string(); + } + std::string dateTimeString = Util::GetDateTimeString(true); std::string fileName = GetFileName(m_settings, "jpg", dateTimeString, 0); Http::HttpClient* pHttpClient = m_pHttpClient; Settings settings = m_settings; - pThreadPool->push( [pHttpClient, settings, dateTimeString, numberOfImages](int) { VStarCamRecorder::Snapshots(pHttpClient, settings, dateTimeString, numberOfImages); } ); + Util::RecorderMutexPointer pRecorderMutex = m_pRecorderMutex; + pThreadPool->push( [pHttpClient, settings, dateTimeString, numberOfImages, pRecorderMutex](int) { + pRecorderMutex->RegisterRecording(settings.IpAddress); + VStarCamRecorder::Snapshots(pHttpClient, settings, dateTimeString, numberOfImages); + pRecorderMutex->UnregisterRecording(settings.IpAddress); + } ); return fileName; } @@ -119,6 +139,12 @@ std::string VStarCamRecorder::Video(ctpl::thread_pool* pThreadPool, const std::s } } + if (m_pRecorderMutex->IsRecording(m_settings.IpAddress)) + { + Logging::Log(Logging::Severity::Debug, "VStarCamRecorder Video Cancelled, Recording in progress"); + return std::string(); + } + std::string dateTimeString = Util::GetDateTimeString(true); std::string fileName = GetFileName(m_settings, "mp4", dateTimeString, 0); @@ -127,7 +153,13 @@ std::string VStarCamRecorder::Video(ctpl::thread_pool* pThreadPool, const std::s std::string cmd(command.str()); Logging::Log(Logging::Severity::Debug, cmd); - pThreadPool->push( [cmd](int) { int retval = std::system(cmd.c_str()); } ); + Settings settings = m_settings; + Util::RecorderMutexPointer pRecorderMutex = m_pRecorderMutex; + pThreadPool->push( [cmd, settings, pRecorderMutex](int) { + pRecorderMutex->RegisterRecording(settings.IpAddress); + int retval = std::system(cmd.c_str()); + pRecorderMutex->UnregisterRecording(settings.IpAddress); + } ); return fileName; } diff --git a/Recorder/VStarCamRecorder.h b/Recorder/VStarCamRecorder.h index 5932186..50ee292 100644 --- a/Recorder/VStarCamRecorder.h +++ b/Recorder/VStarCamRecorder.h @@ -18,7 +18,7 @@ namespace Recorder { class VStarCamRecorder : public virtual Recorder { public: - VStarCamRecorder(Http::HttpClient* pHttpClient, const Settings& settings); + VStarCamRecorder(Http::HttpClient* pHttpClient, const Settings& settings, Util::RecorderMutexPointer pRecorderMutex); ~VStarCamRecorder(); virtual std::string Snapshot(ctpl::thread_pool* pThreadPool, const std::string& ffmpeg) override; diff --git a/Recorder/WatchBotRecorder.cpp b/Recorder/WatchBotRecorder.cpp index 8e4f13d..346761f 100644 --- a/Recorder/WatchBotRecorder.cpp +++ b/Recorder/WatchBotRecorder.cpp @@ -13,7 +13,8 @@ namespace CameraRecorder { namespace Recorder { -WatchBotRecorder::WatchBotRecorder(Http::HttpClient* pHttpClient, const Settings& settings) : +WatchBotRecorder::WatchBotRecorder(Http::HttpClient* pHttpClient, const Settings& settings, Util::RecorderMutexPointer pRecorderMutex) : + Recorder(pRecorderMutex), m_pHttpClient(pHttpClient), m_settings(settings) { @@ -48,12 +49,20 @@ std::string WatchBotRecorder::Snapshot(ctpl::thread_pool* pThreadPool, const std } } + //if (m_pRecorderMutex->IsRecording(m_settings.IpAddress)) + // return std::string(); + std::string dateTimeString = Util::GetDateTimeString(); std::string fileName = GetFileName(m_settings, "jpg", dateTimeString); Http::HttpClient* pHttpClient = m_pHttpClient; Settings settings = m_settings; - WatchBotRecorder::Snapshots(pHttpClient, settings, dateTimeString, 1); + //Util::RecorderMutexPointer pRecorderMutex = m_pRecorderMutex; + //pThreadPool->push( [pHttpClient, settings, dateTimeString, pRecorderMutex](int) { + // pRecorderMutex->RegisterRecording(settings.IpAddress); + WatchBotRecorder::Snapshots(pHttpClient, settings, dateTimeString, 1); + // pRecorderMutex->UnregisterRecording(settings.IpAddress); + //} ); return fileName; } @@ -83,12 +92,23 @@ std::string WatchBotRecorder::MultiSnapshot(ctpl::thread_pool* pThreadPool, cons } } + if (m_pRecorderMutex->IsRecording(m_settings.IpAddress)) + { + Logging::Log(Logging::Severity::Debug, "WatchBotRecorder MultiSnapshot Cancelled, Recording in progress"); + return std::string(); + } + std::string dateTimeString = Util::GetDateTimeString(); std::string fileName = GetFileName(m_settings, "jpg", dateTimeString); Http::HttpClient* pHttpClient = m_pHttpClient; Settings settings = m_settings; - pThreadPool->push( [pHttpClient, settings, dateTimeString, numberOfImages](int) { WatchBotRecorder::Snapshots(pHttpClient, settings, dateTimeString, numberOfImages); } ); + Util::RecorderMutexPointer pRecorderMutex = m_pRecorderMutex; + pThreadPool->push( [pHttpClient, settings, dateTimeString, numberOfImages, pRecorderMutex](int) { + pRecorderMutex->RegisterRecording(settings.IpAddress); + WatchBotRecorder::Snapshots(pHttpClient, settings, dateTimeString, numberOfImages); + pRecorderMutex->UnregisterRecording(settings.IpAddress); + } ); return fileName; } @@ -121,6 +141,12 @@ std::string WatchBotRecorder::Video(ctpl::thread_pool* pThreadPool, const std::s } } + if (m_pRecorderMutex->IsRecording(m_settings.IpAddress)) + { + Logging::Log(Logging::Severity::Debug, "WatchBotRecorder MultiSnapshot Cancelled, Recording in progress"); + return std::string(); + } + std::string dateTimeString = Util::GetDateTimeString(); std::string fileName = GetFileName(m_settings, "mp4", dateTimeString); @@ -129,7 +155,13 @@ std::string WatchBotRecorder::Video(ctpl::thread_pool* pThreadPool, const std::s std::string cmd(command.str()); Logging::Log(Logging::Severity::Debug, cmd); - pThreadPool->push( [cmd](int) { int retval = std::system(cmd.c_str()); } ); + Settings settings = m_settings; + Util::RecorderMutexPointer pRecorderMutex = m_pRecorderMutex; + pThreadPool->push( [cmd, settings, pRecorderMutex](int) { + pRecorderMutex->RegisterRecording(settings.IpAddress); + int retval = std::system(cmd.c_str()); + pRecorderMutex->UnregisterRecording(settings.IpAddress); + } ); return fileName; } diff --git a/Recorder/WatchBotRecorder.h b/Recorder/WatchBotRecorder.h index 9b709a2..698d1db 100644 --- a/Recorder/WatchBotRecorder.h +++ b/Recorder/WatchBotRecorder.h @@ -18,7 +18,7 @@ namespace Recorder { class WatchBotRecorder : public virtual Recorder { public: - WatchBotRecorder(Http::HttpClient* pHttpClient, const Settings& settings); + WatchBotRecorder(Http::HttpClient* pHttpClient, const Settings& settings, Util::RecorderMutexPointer pRecorderMutex); ~WatchBotRecorder(); virtual std::string Snapshot(ctpl::thread_pool* pThreadPool, const std::string& ffmpeg) override; diff --git a/Recorder/ZModoRecorder.cpp b/Recorder/ZModoRecorder.cpp index 7846834..add7c7e 100644 --- a/Recorder/ZModoRecorder.cpp +++ b/Recorder/ZModoRecorder.cpp @@ -6,7 +6,8 @@ namespace CameraRecorder { namespace Recorder { -ZModoRecorder::ZModoRecorder(Http::HttpClient* pHttpClient, const Settings& settings) : +ZModoRecorder::ZModoRecorder(Http::HttpClient* pHttpClient, const Settings& settings, Util::RecorderMutexPointer pRecorderMutex) : + Recorder(pRecorderMutex), m_pHttpClient(pHttpClient), m_settings(settings) { diff --git a/Recorder/ZModoRecorder.h b/Recorder/ZModoRecorder.h index ff73687..7595c49 100644 --- a/Recorder/ZModoRecorder.h +++ b/Recorder/ZModoRecorder.h @@ -17,7 +17,7 @@ namespace Recorder { class ZModoRecorder : public virtual Recorder { public: - ZModoRecorder(Http::HttpClient* pHttpClient, const Settings& settings); + ZModoRecorder(Http::HttpClient* pHttpClient, const Settings& settings, Util::RecorderMutexPointer pRecorderMutex); ~ZModoRecorder(); virtual std::string Snapshot(ctpl::thread_pool* pThreadPool, const std::string& ffmpeg) override; diff --git a/Util/RecorderMutex.cpp b/Util/RecorderMutex.cpp new file mode 100644 index 0000000..1d94d3f --- /dev/null +++ b/Util/RecorderMutex.cpp @@ -0,0 +1,31 @@ +#include "RecorderMutex.h" +#include + + +namespace CameraRecorder { +namespace Util { + +RecorderMutex::RecorderMutex() +{ +} + +void RecorderMutex::RegisterRecording(const std::string& ipAddress) +{ + std::unique_lock lock(m_mutex); + m_activeRecordings.push_back(ipAddress); +} + +void RecorderMutex::UnregisterRecording(const std::string& ipAddress) +{ + std::unique_lock lock(m_mutex); + m_activeRecordings.erase(std::remove(m_activeRecordings.begin(), m_activeRecordings.end(), ipAddress), m_activeRecordings.end()); +} + +bool RecorderMutex::IsRecording(const std::string& ipAddress) +{ + std::unique_lock lock(m_mutex); + return std::find(m_activeRecordings.begin(), m_activeRecordings.end(), ipAddress) != m_activeRecordings.end(); +} + +} // namespace Util +} // namespace CameraRecorder diff --git a/Util/RecorderMutex.h b/Util/RecorderMutex.h new file mode 100644 index 0000000..5fd5e54 --- /dev/null +++ b/Util/RecorderMutex.h @@ -0,0 +1,31 @@ +#ifndef UTIL_RECORDERMUTEX_H +#define UTIL_RECORDERMUTEX_H + +#include +#include +#include + + +namespace CameraRecorder { +namespace Util { + +class RecorderMutex +{ +public: + RecorderMutex(); + + void RegisterRecording(const std::string& ipAddress); + void UnregisterRecording(const std::string& ipAddress); + bool IsRecording(const std::string& ipAddress); + +private: + std::mutex m_mutex; + std::vector m_activeRecordings; +}; + +typedef RecorderMutex* const RecorderMutexPointer; + +} // namespace Util +} // namespace CameraRecorder + +#endif // UTIL_RECORDERMUTEX_H