| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222 |
- #include "FoscamRecorder.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 <thread>
- namespace CameraRecorder {
- namespace Recorder {
- FoscamRecorder::FoscamRecorder(Http::HttpClient* pHttpClient, const Settings& settings, Util::RecorderMutexPointer pRecorderMutex) :
- Recorder(pRecorderMutex),
- m_pHttpClient(pHttpClient),
- m_settings(settings)
- {
- }
- FoscamRecorder::~FoscamRecorder()
- {
- }
- std::string FoscamRecorder::Snapshot(ctpl::thread_pool* pThreadPool, const std::string& ffmpeg)
- {
- Logging::Log(Logging::Severity::Debug, "FoscamRecorder 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();
- std::string fileName = GetFileName(m_settings, "jpg", dateTimeString);
- 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);
- FoscamRecorder::Snapshots(pHttpClient, settings, dateTimeString, 1);
- // pRecorderMutex->UnregisterRecording(settings.IpAddress);
- //} );
- return fileName;
- }
- std::string FoscamRecorder::MultiSnapshot(ctpl::thread_pool* pThreadPool, const std::string& ffmpeg, int numberOfImages)
- {
- Logging::Log(Logging::Severity::Debug, "FoscamRecorder 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, "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;
- 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;
- }
- std::string FoscamRecorder::Video(ctpl::thread_pool* pThreadPool, const std::string& ffmpeg)
- {
- Logging::Log(Logging::Severity::Debug, "FoscamRecorder Video");
- int port = 65534;
- std::stringstream url;
- url << "rtsp://" << m_settings.Username << ":" << m_settings.Password << "@" << m_settings.IpAddress << ":" << port << "/videoSub";
- 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, "FoscamRecorder Video Cancelled, Recording in progress");
- 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 copy -an -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 FoscamRecorder::GetFolderName(const Settings& settings)
- {
- filesystem::path fileLocation(settings.Path);
- fileLocation = fileLocation / filesystem::path(settings.IpAddress);
- return fileLocation.str();
- }
- std::string FoscamRecorder::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 FoscamRecorder::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(FoscamRecorder::Snapshot(pHttpClient, settings));
- Image::JpegImageCollection collection(images);
- collection.Save(FoscamRecorder::GetFileName(settings, "jpg", dateTimeString));
- }
- Image::JpegImage FoscamRecorder::Snapshot(Http::HttpClient* pHttpClient, const Settings& settings)
- {
- std::stringstream url;
- url << "http://" << settings.IpAddress << ":" << settings.Port << "/cgi-bin/CGIProxy.fcgi?cmd=snapPicture2&usr=" << 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 << "FoscamRecorder::Snapshot() - Error: " << e.what() << std::endl;
- Logging::Log(Logging::Severity::Error, ss.str());
- }
- return image;
- }
- } // namespace Recorder
- } // namespace CameraRecorder
|