FoscamRecorder.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. #include "FoscamRecorder.h"
  2. #include "Util.h"
  3. #include <HttpClient.h>
  4. #include <JpegImageCollection.h>
  5. #include <Logging.h>
  6. #include <filesystem/path.h>
  7. #include <fstream>
  8. #include <sstream>
  9. #include <thread>
  10. namespace CameraRecorder {
  11. namespace Recorder {
  12. FoscamRecorder::FoscamRecorder(Http::HttpClient* pHttpClient, const Settings& settings) :
  13. m_pHttpClient(pHttpClient),
  14. m_settings(settings)
  15. {
  16. }
  17. FoscamRecorder::~FoscamRecorder()
  18. {
  19. }
  20. std::string FoscamRecorder::Snapshot(ctpl::thread_pool* pThreadPool, const std::string& ffmpeg)
  21. {
  22. Logging::Log(Logging::Severity::Debug, "FoscamRecorder Snapshot");
  23. std::string dateTimeString = GetDateTimeString();
  24. std::string fileName = GetFileName(m_settings, "jpg", dateTimeString);
  25. Http::HttpClient* pHttpClient = m_pHttpClient;
  26. Settings settings = m_settings;
  27. FoscamRecorder::Snapshots(pHttpClient, settings, dateTimeString, 1);
  28. return fileName;
  29. }
  30. std::string FoscamRecorder::MultiSnapshot(ctpl::thread_pool* pThreadPool, const std::string& ffmpeg, int numberOfImages)
  31. {
  32. Logging::Log(Logging::Severity::Debug, "FoscamRecorder MultiSnapshot");
  33. std::string dateTimeString = GetDateTimeString();
  34. std::string fileName = GetFileName(m_settings, "jpg", dateTimeString);
  35. Http::HttpClient* pHttpClient = m_pHttpClient;
  36. Settings settings = m_settings;
  37. pThreadPool->push( [pHttpClient, settings, dateTimeString, numberOfImages](int) { FoscamRecorder::Snapshots(pHttpClient, settings, dateTimeString, numberOfImages); } );
  38. return fileName;
  39. }
  40. std::string FoscamRecorder::Video(ctpl::thread_pool* pThreadPool, const std::string& ffmpeg)
  41. {
  42. Logging::Log(Logging::Severity::Debug, "FoscamRecorder Video");
  43. int port = 65534;
  44. std::stringstream url;
  45. url << "rtsp://" << m_settings.Username << ":" << m_settings.Password << "@" << m_settings.IpAddress << ":" << port << "/videoSub";
  46. std::string dateTimeString = GetDateTimeString();
  47. std::string fileName = GetFileName(m_settings, "mp4", dateTimeString);
  48. std::stringstream command;
  49. command << ffmpeg << " -i \"" << url.str() << "\" -t 30 -c:v copy -an -strict experimental " << fileName << " > /dev/null 2>&1";
  50. std::string cmd(command.str());
  51. Logging::Log(Logging::Severity::Debug, cmd);
  52. pThreadPool->push( [cmd](int) { int retval = std::system(cmd.c_str()); } );
  53. return fileName;
  54. }
  55. std::string FoscamRecorder::GetFileName(const Settings& settings, const std::string& extension, const std::string& dateTimeString)
  56. {
  57. filesystem::path fileLocation(settings.Path);
  58. fileLocation = fileLocation / filesystem::path(settings.IpAddress);
  59. std::string fileName = std::string(dateTimeString).append(".").append(extension);
  60. filesystem::path fullFileName = fileLocation / filesystem::path(fileName);
  61. return fullFileName.str();
  62. }
  63. void FoscamRecorder::Snapshots(Http::HttpClient* pHttpClient, const Settings& settings, std::string dateTimeString, int numberOfImages)
  64. {
  65. std::vector<Image::JpegImage> images;
  66. for (int i = 0; i < numberOfImages; ++i)
  67. images.push_back(FoscamRecorder::Snapshot(pHttpClient, settings));
  68. Image::JpegImageCollection collection(images);
  69. collection.Save(FoscamRecorder::GetFileName(settings, "jpg", dateTimeString));
  70. }
  71. Image::JpegImage FoscamRecorder::Snapshot(Http::HttpClient* pHttpClient, const Settings& settings)
  72. {
  73. std::stringstream url;
  74. url << "http://" << settings.IpAddress << ":" << settings.Port << "/cgi-bin/CGIProxy.fcgi?cmd=snapPicture2&usr=" << settings.Username << "&pwd=" << settings.Password;
  75. Image::JpegImage image;
  76. try
  77. {
  78. Http::HttpRequest request(url.str());
  79. image.SetImageData(pHttpClient->Open(request));
  80. }
  81. catch (const std::exception& e)
  82. {
  83. std::stringstream ss;
  84. ss << "FoscamRecorder::Snapshot() - Error: " << e.what() << std::endl;
  85. Logging::Log(Logging::Severity::Error, ss.str());
  86. }
  87. return image;
  88. }
  89. } // namespace Recorder
  90. } // namespace CameraRecorder