Files
CameraRecorder/Recorder/VStarCamRecorder.cpp
2023-03-16 13:41:53 +01:00

213 lines
6.6 KiB
C++

#include "VStarCamRecorder.h"
#include "Util/Util.h"
#include <HttpClient.h>
#include <Logging.h>
#include <fstream>
#include <iostream>
#include <sstream>
namespace CameraRecorder {
namespace Recorder {
VStarCamRecorder::VStarCamRecorder(Http::HttpClient* pHttpClient, const Settings& settings, Util::RecorderMutexPointer pRecorderMutex) :
Recorder(pRecorderMutex),
m_pHttpClient(pHttpClient),
m_settings(settings)
{
}
VStarCamRecorder::~VStarCamRecorder()
{
}
std::string VStarCamRecorder::Snapshot(ctpl::thread_pool* pThreadPool, const std::string& ffmpeg)
{
Logging::Log(Logging::Severity::Debug, "VStarCamRecorder Snapshot");
std::string folderName = GetFolderName(m_settings);
if (!Util::FolderExists(folderName))
{
try
{
Util::CreateFolder(folderName);
}
catch (const std::exception& e)
{
std::cerr << "Exception caught" << std::endl;
std::stringstream ss;
ss << "Type : " << typeid(e).name() << std::endl;
ss << "ERROR: " << e.what() << std::endl;
Logging::Log(Logging::Severity::Error, ss.str());
return std::string();
}
}
//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;
//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;
}
std::string VStarCamRecorder::MultiSnapshot(ctpl::thread_pool* pThreadPool, const std::string& ffmpeg, int numberOfImages)
{
Logging::Log(Logging::Severity::Debug, "VStarCamRecorder MultiSnapshot");
std::string folderName = GetFolderName(m_settings);
if (!Util::FolderExists(folderName))
{
try
{
Util::CreateFolder(folderName);
}
catch (const std::exception& e)
{
std::cerr << "Exception caught" << std::endl;
std::stringstream ss;
ss << "Type : " << typeid(e).name() << std::endl;
ss << "ERROR: " << e.what() << std::endl;
Logging::Log(Logging::Severity::Error, ss.str());
return std::string();
}
}
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;
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;
}
std::string VStarCamRecorder::Video(ctpl::thread_pool* pThreadPool, const std::string& ffmpeg)
{
Logging::Log(Logging::Severity::Debug, "VStarCamRecorder Video");
int port = 10554;
std::stringstream url;
url << "rtsp://" << m_settings.Username << ":" << m_settings.Password << "@" << m_settings.IpAddress << ":" << port << "/udp/av0_0";
std::string folderName = GetFolderName(m_settings);
if (!Util::FolderExists(folderName))
{
try
{
Util::CreateFolder(folderName);
}
catch (const std::exception& e)
{
std::cerr << "Exception caught" << std::endl;
std::stringstream ss;
ss << "Type : " << typeid(e).name() << std::endl;
ss << "ERROR: " << e.what() << std::endl;
Logging::Log(Logging::Severity::Error, ss.str());
return std::string();
}
}
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);
std::stringstream command;
command << ffmpeg << " -i \"" << url.str() << "\" -t 30 -timeout 60 -c:v copy -c:a aac -strict experimental " << fileName << " > /dev/null 2>&1 ; " << ffmpeg << " -i " << fileName << " -vf \"select=eq(n\\,0)\" -q:v 3 " << fileName << ".jpg > /dev/null 2>&1";
std::string cmd(command.str());
Logging::Log(Logging::Severity::Debug, cmd);
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;
}
std::string VStarCamRecorder::GetFolderName(const Settings& settings)
{
filesystem::path fileLocation(settings.Path);
fileLocation = fileLocation / filesystem::path(settings.IpAddress);
return fileLocation.str();
}
std::string VStarCamRecorder::GetFileName(const Settings& settings, const std::string& extension, const std::string& dateTimeString, int imageIndex)
{
filesystem::path fileLocation(GetFolderName(settings));
std::string fileName = std::string("VSTA000000XXXXX_0_").append(dateTimeString).append("_").append(std::to_string(imageIndex)).append(".").append(extension);
filesystem::path fullFileName = fileLocation / filesystem::path(fileName);
return fullFileName.str();
}
void VStarCamRecorder::Snapshots(Http::HttpClient* pHttpClient, const Settings& settings, std::string dateTimeString, int numberOfImages)
{
for (int i = 0; i < numberOfImages; ++i)
VStarCamRecorder::Snapshot(pHttpClient, settings, VStarCamRecorder::GetFileName(settings, "jpg", dateTimeString, i));
}
void VStarCamRecorder::Snapshot(Http::HttpClient* pHttpClient, const Settings& settings, const filesystem::path& fileName)
{
std::stringstream url;
url << "http://" << settings.Username << ":" << settings.Password << "@" << settings.IpAddress << ":" << settings.Port << "/img/snapshot.cgi?res=0";
try
{
std::ofstream file(fileName.str());
Http::HttpRequest request(url.str());
file << pHttpClient->Open(request);
file.close();
}
catch (const std::exception& e)
{
std::stringstream ss;
ss << "VStarCamRecorder::Snapshot() - Error: " << e.what() << std::endl;
Logging::Log(Logging::Severity::Error, ss.str());
}
}
} // namespace Recorder
} // namespace CameraRecorder