Files
CameraRecorder/Recorder/RTSPRecorder.cpp
T

171 lines
5.1 KiB
C++

#include "RTSPRecorder.h"
#include "Util/Util.h"
#include <HttpClient.h>
#include <Logging.h>
#include <iostream>
namespace CameraRecorder {
namespace Recorder {
RTSPRecorder::RTSPRecorder(Http::HttpClient* pHttpClient, const Settings& settings) :
m_pHttpClient(pHttpClient),
m_settings(settings)
{
}
RTSPRecorder::~RTSPRecorder()
{
}
std::string RTSPRecorder::Snapshot(ctpl::thread_pool* pThreadPool, const std::string& ffmpeg)
{
Logging::Log(Logging::Severity::Debug, "RTSPRecorder 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();
}
}
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);
return fileName;
}
std::string RTSPRecorder::MultiSnapshot(ctpl::thread_pool* pThreadPool, const std::string& ffmpeg, int numberOfImages)
{
Logging::Log(Logging::Severity::Debug, "RTSPRecorder 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();
}
}
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); } );
return fileName;
}
std::string RTSPRecorder::Video(ctpl::thread_pool* pThreadPool, const std::string& ffmpeg)
{
Logging::Log(Logging::Severity::Debug, "RTSPRecorder Video");
std::stringstream url;
url << "rtsp://" << m_settings.Username << ":" << m_settings.Password << "@" << m_settings.IpAddress << ":" << m_settings.Port << m_settings.URL;
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();
}
}
std::string dateTimeString = Util::GetDateTimeString();
std::string fileName = GetFileName(m_settings, "mp4", dateTimeString, 0);
std::stringstream command;
command << ffmpeg << " -rtsp_transport tcp -i \"" << url.str() << "\" -t 30 -timeout 60 -c:v copy -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);
pThreadPool->push( [cmd](int) { int retval = std::system(cmd.c_str()); } );
return fileName;
}
std::string RTSPRecorder::GetFolderName(const Settings& settings)
{
filesystem::path fileLocation(settings.Path);
fileLocation = fileLocation / filesystem::path(settings.IpAddress);
return fileLocation.str();
}
std::string RTSPRecorder::GetFileName(const Settings& settings, const std::string& extension, const std::string& dateTimeString, int imageIndex)
{
filesystem::path fileLocation(GetFolderName(settings));
std::string fileName = std::string(dateTimeString).append("_").append(std::to_string(imageIndex)).append(".").append(extension);
filesystem::path fullFileName = fileLocation / filesystem::path(fileName);
return fullFileName.str();
}
void RTSPRecorder::Snapshots(const std::string& ffmpeg, const Settings& settings, std::string dateTimeString, int numberOfImages)
{
for (int i = 0; i < numberOfImages; ++i)
RTSPRecorder::Snapshot(ffmpeg, settings, RTSPRecorder::GetFileName(settings, "jpg", dateTimeString, i));
}
void RTSPRecorder::Snapshot(const std::string& ffmpeg, const Settings& settings, const filesystem::path& fileName)
{
std::stringstream url;
url << "rtsp://" << settings.Username << ":" << settings.Password << "@" << settings.IpAddress << ":" << settings.Port << settings.URL;
std::stringstream command;
command << ffmpeg << " -rtsp_transport tcp -i \"" << url.str() << "\" -vframes 1 -qscale:v 2 " << fileName << " > /dev/null 2>&1";
std::string cmd(command.str());
Logging::Log(Logging::Severity::Debug, cmd);
int retval = std::system(cmd.c_str());
}
} // namespace Recorder
} // namespace CameraRecorder