WatchBotRecorder.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. #include "WatchBotRecorder.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 <vector>
  10. namespace CameraRecorder {
  11. namespace Recorder {
  12. WatchBotRecorder::WatchBotRecorder(Http::HttpClient* pHttpClient, const Settings& settings) :
  13. m_pHttpClient(pHttpClient),
  14. m_settings(settings)
  15. {
  16. }
  17. WatchBotRecorder::~WatchBotRecorder()
  18. {
  19. }
  20. std::string WatchBotRecorder::Snapshot(ctpl::thread_pool* pThreadPool)
  21. {
  22. Logging::Log(Logging::Severity::Debug, "WatchBotRecorder 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. WatchBotRecorder::Snapshots(pHttpClient, settings, dateTimeString, 1);
  28. return fileName;
  29. }
  30. std::string WatchBotRecorder::MultiSnapshot(ctpl::thread_pool* pThreadPool, int numberOfImages)
  31. {
  32. Logging::Log(Logging::Severity::Debug, "WatchBotRecorder 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) { WatchBotRecorder::Snapshots(pHttpClient, settings, dateTimeString, numberOfImages); } );
  38. return fileName;
  39. }
  40. std::string WatchBotRecorder::Video(ctpl::thread_pool* pThreadPool, const std::string& ffmpeg)
  41. {
  42. Logging::Log(Logging::Severity::Debug, "WatchBotRecorder Video");
  43. std::stringstream url;
  44. url << "http://" << m_settings.IpAddress << ":" << m_settings.Port << "/videostream.asf?user=" << m_settings.Username << "&pwd=" << m_settings.Password;
  45. std::string dateTimeString = GetDateTimeString();
  46. std::string fileName = GetFileName(m_settings, "mp4", dateTimeString);
  47. std::stringstream command;
  48. command << ffmpeg << " -i \"" << url.str() << "\" -t 30 -c:v libx264 -pix_fmt yuv420p -profile:v main -level:v 3.1 -c:a aac -strict experimental " << fileName << " > /dev/null 2>&1";
  49. std::string cmd(command.str());
  50. Logging::Log(Logging::Severity::Debug, cmd);
  51. pThreadPool->push( [cmd](int) { int retval = std::system(cmd.c_str()); } );
  52. return fileName;
  53. }
  54. std::string WatchBotRecorder::GetFileName(const Settings& settings, const std::string& extension, const std::string& dateTimeString)
  55. {
  56. filesystem::path fileLocation(settings.Path);
  57. fileLocation = fileLocation / filesystem::path(settings.IpAddress);
  58. std::string fileName = std::string(dateTimeString).append(".").append(extension);
  59. filesystem::path fullFileName = fileLocation / filesystem::path(fileName);
  60. return fullFileName.str();
  61. }
  62. void WatchBotRecorder::Snapshots(Http::HttpClient* pHttpClient, const Settings& settings, std::string dateTimeString, int numberOfImages)
  63. {
  64. std::vector<Image::JpegImage> images;
  65. for (int i = 0; i < numberOfImages; ++i)
  66. images.push_back(WatchBotRecorder::Snapshot(pHttpClient, settings));
  67. Image::JpegImageCollection collection(images);
  68. collection.Save(WatchBotRecorder::GetFileName(settings, "jpg", dateTimeString));
  69. }
  70. Image::JpegImage WatchBotRecorder::Snapshot(Http::HttpClient* pHttpClient, const Settings& settings)
  71. {
  72. std::stringstream url;
  73. url << "http://" << settings.IpAddress << ":" << settings.Port << "/snapshot.cgi?user=" << settings.Username << "&pwd=" << settings.Password;
  74. Image::JpegImage image;
  75. try
  76. {
  77. Http::HttpRequest request(url.str());
  78. image.SetImageData(pHttpClient->Open(request));
  79. }
  80. catch (const std::exception& e)
  81. {
  82. std::stringstream ss;
  83. ss << "WatchBotRecorder::Snapshot() - Error: " << e.what() << std::endl;
  84. Logging::Log(Logging::Severity::Error, ss.str());
  85. }
  86. return image;
  87. }
  88. } // namespace Recorder
  89. } // namespace CameraRecorder