Files
CameraRecorder/Recorder/WatchBotRecorder.cpp
T

190 lines
5.4 KiB
C++

#include "WatchBotRecorder.h"
#include "Util/Util.h"
#include <HttpClient.h>
#include <JpegImageCollection.h>
#include <Logging.h>
#include <filesystem/path.h>
#include <fstream>
#include <iostream>
#include <sstream>
#include <vector>
namespace CameraRecorder {
namespace Recorder {
WatchBotRecorder::WatchBotRecorder(Http::HttpClient* pHttpClient, const Settings& settings) :
m_pHttpClient(pHttpClient),
m_settings(settings)
{
}
WatchBotRecorder::~WatchBotRecorder()
{
}
std::string WatchBotRecorder::Snapshot(ctpl::thread_pool* pThreadPool, const std::string& ffmpeg)
{
Logging::Log(Logging::Severity::Debug, "WatchBotRecorder 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);
Http::HttpClient* pHttpClient = m_pHttpClient;
Settings settings = m_settings;
WatchBotRecorder::Snapshots(pHttpClient, settings, dateTimeString, 1);
return fileName;
}
std::string WatchBotRecorder::MultiSnapshot(ctpl::thread_pool* pThreadPool, const std::string& ffmpeg, int numberOfImages)
{
Logging::Log(Logging::Severity::Debug, "WatchBotRecorder 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);
Http::HttpClient* pHttpClient = m_pHttpClient;
Settings settings = m_settings;
pThreadPool->push( [pHttpClient, settings, dateTimeString, numberOfImages](int) { WatchBotRecorder::Snapshots(pHttpClient, settings, dateTimeString, numberOfImages); } );
return fileName;
}
std::string WatchBotRecorder::Video(ctpl::thread_pool* pThreadPool, const std::string& ffmpeg)
{
Logging::Log(Logging::Severity::Debug, "WatchBotRecorder Video");
std::stringstream url;
url << "http://" << m_settings.IpAddress << ":" << m_settings.Port << "/videostream.asf?user=" << m_settings.Username << "&pwd=" << m_settings.Password;
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);
std::stringstream command;
command << ffmpeg << " -i \"" << url.str() << "\" -t 30 -timeout 60 -c:v libx264 -pix_fmt yuv420p -profile:v main -level:v 3.1 -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);
pThreadPool->push( [cmd](int) { int retval = std::system(cmd.c_str()); } );
return fileName;
}
std::string WatchBotRecorder::GetFolderName(const Settings& settings)
{
filesystem::path fileLocation(settings.Path);
fileLocation = fileLocation / filesystem::path(settings.IpAddress);
return fileLocation.str();
}
std::string WatchBotRecorder::GetFileName(const Settings& settings, const std::string& extension, const std::string& dateTimeString)
{
filesystem::path fileLocation(GetFolderName(settings));
std::string fileName = std::string(dateTimeString).append(".").append(extension);
filesystem::path fullFileName = fileLocation / filesystem::path(fileName);
return fullFileName.str();
}
void WatchBotRecorder::Snapshots(Http::HttpClient* pHttpClient, const Settings& settings, std::string dateTimeString, int numberOfImages)
{
std::vector<Image::JpegImage> images;
for (int i = 0; i < numberOfImages; ++i)
images.push_back(WatchBotRecorder::Snapshot(pHttpClient, settings));
Image::JpegImageCollection collection(images);
collection.Save(WatchBotRecorder::GetFileName(settings, "jpg", dateTimeString));
}
Image::JpegImage WatchBotRecorder::Snapshot(Http::HttpClient* pHttpClient, const Settings& settings)
{
std::stringstream url;
url << "http://" << settings.IpAddress << ":" << settings.Port << "/snapshot.cgi?user=" << settings.Username << "&pwd=" << settings.Password;
Image::JpegImage image;
try
{
Http::HttpRequest request(url.str());
image.SetImageData(pHttpClient->Open(request));
}
catch (const std::exception& e)
{
std::stringstream ss;
ss << "WatchBotRecorder::Snapshot() - Error: " << e.what() << std::endl;
Logging::Log(Logging::Severity::Error, ss.str());
}
return image;
}
} // namespace Recorder
} // namespace CameraRecorder