WatchBotRecorder.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. #include "WatchBotRecorder.h"
  2. #include "Util/Util.h"
  3. #include <HttpClient.h>
  4. #include <JpegImageCollection.h>
  5. #include <Logging.h>
  6. #include <filesystem/path.h>
  7. #include <fstream>
  8. #include <iostream>
  9. #include <sstream>
  10. #include <vector>
  11. namespace CameraRecorder {
  12. namespace Recorder {
  13. WatchBotRecorder::WatchBotRecorder(Http::HttpClient* pHttpClient, const Settings& settings) :
  14. m_pHttpClient(pHttpClient),
  15. m_settings(settings)
  16. {
  17. }
  18. WatchBotRecorder::~WatchBotRecorder()
  19. {
  20. }
  21. std::string WatchBotRecorder::Snapshot(ctpl::thread_pool* pThreadPool, const std::string& ffmpeg)
  22. {
  23. Logging::Log(Logging::Severity::Debug, "WatchBotRecorder Snapshot");
  24. std::string folderName = GetFolderName(m_settings);
  25. if (!Util::FolderExists(folderName))
  26. {
  27. try
  28. {
  29. Util::CreateFolder(folderName);
  30. }
  31. catch (const std::exception& e)
  32. {
  33. std::cerr << "Exception caught" << std::endl;
  34. std::stringstream ss;
  35. ss << "Type : " << typeid(e).name() << std::endl;
  36. ss << "ERROR: " << e.what() << std::endl;
  37. Logging::Log(Logging::Severity::Error, ss.str());
  38. return std::string();
  39. }
  40. }
  41. std::string dateTimeString = Util::GetDateTimeString();
  42. std::string fileName = GetFileName(m_settings, "jpg", dateTimeString);
  43. Http::HttpClient* pHttpClient = m_pHttpClient;
  44. Settings settings = m_settings;
  45. WatchBotRecorder::Snapshots(pHttpClient, settings, dateTimeString, 1);
  46. return fileName;
  47. }
  48. std::string WatchBotRecorder::MultiSnapshot(ctpl::thread_pool* pThreadPool, const std::string& ffmpeg, int numberOfImages)
  49. {
  50. Logging::Log(Logging::Severity::Debug, "WatchBotRecorder MultiSnapshot");
  51. std::string folderName = GetFolderName(m_settings);
  52. if (!Util::FolderExists(folderName))
  53. {
  54. try
  55. {
  56. Util::CreateFolder(folderName);
  57. }
  58. catch (const std::exception& e)
  59. {
  60. std::cerr << "Exception caught" << std::endl;
  61. std::stringstream ss;
  62. ss << "Type : " << typeid(e).name() << std::endl;
  63. ss << "ERROR: " << e.what() << std::endl;
  64. Logging::Log(Logging::Severity::Error, ss.str());
  65. return std::string();
  66. }
  67. }
  68. std::string dateTimeString = Util::GetDateTimeString();
  69. std::string fileName = GetFileName(m_settings, "jpg", dateTimeString);
  70. Http::HttpClient* pHttpClient = m_pHttpClient;
  71. Settings settings = m_settings;
  72. pThreadPool->push( [pHttpClient, settings, dateTimeString, numberOfImages](int) { WatchBotRecorder::Snapshots(pHttpClient, settings, dateTimeString, numberOfImages); } );
  73. return fileName;
  74. }
  75. std::string WatchBotRecorder::Video(ctpl::thread_pool* pThreadPool, const std::string& ffmpeg)
  76. {
  77. Logging::Log(Logging::Severity::Debug, "WatchBotRecorder Video");
  78. std::stringstream url;
  79. url << "http://" << m_settings.IpAddress << ":" << m_settings.Port << "/videostream.asf?user=" << m_settings.Username << "&pwd=" << m_settings.Password;
  80. std::string folderName = GetFolderName(m_settings);
  81. if (!Util::FolderExists(folderName))
  82. {
  83. try
  84. {
  85. Util::CreateFolder(folderName);
  86. }
  87. catch (const std::exception& e)
  88. {
  89. std::cerr << "Exception caught" << std::endl;
  90. std::stringstream ss;
  91. ss << "Type : " << typeid(e).name() << std::endl;
  92. ss << "ERROR: " << e.what() << std::endl;
  93. Logging::Log(Logging::Severity::Error, ss.str());
  94. return std::string();
  95. }
  96. }
  97. std::string dateTimeString = Util::GetDateTimeString();
  98. std::string fileName = GetFileName(m_settings, "mp4", dateTimeString);
  99. std::stringstream command;
  100. 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 ; " << ffmpeg << " -i " << fileName << " -vf \"select=eq(n\\,0)\" -q:v 3 " << fileName << ".jpg > /dev/null 2>&1";
  101. std::string cmd(command.str());
  102. Logging::Log(Logging::Severity::Debug, cmd);
  103. pThreadPool->push( [cmd](int) { int retval = std::system(cmd.c_str()); } );
  104. return fileName;
  105. }
  106. std::string WatchBotRecorder::GetFolderName(const Settings& settings)
  107. {
  108. filesystem::path fileLocation(settings.Path);
  109. fileLocation = fileLocation / filesystem::path(settings.IpAddress);
  110. return fileLocation.str();
  111. }
  112. std::string WatchBotRecorder::GetFileName(const Settings& settings, const std::string& extension, const std::string& dateTimeString)
  113. {
  114. filesystem::path fileLocation(GetFolderName(settings));
  115. std::string fileName = std::string(dateTimeString).append(".").append(extension);
  116. filesystem::path fullFileName = fileLocation / filesystem::path(fileName);
  117. return fullFileName.str();
  118. }
  119. void WatchBotRecorder::Snapshots(Http::HttpClient* pHttpClient, const Settings& settings, std::string dateTimeString, int numberOfImages)
  120. {
  121. std::vector<Image::JpegImage> images;
  122. for (int i = 0; i < numberOfImages; ++i)
  123. images.push_back(WatchBotRecorder::Snapshot(pHttpClient, settings));
  124. Image::JpegImageCollection collection(images);
  125. collection.Save(WatchBotRecorder::GetFileName(settings, "jpg", dateTimeString));
  126. }
  127. Image::JpegImage WatchBotRecorder::Snapshot(Http::HttpClient* pHttpClient, const Settings& settings)
  128. {
  129. std::stringstream url;
  130. url << "http://" << settings.IpAddress << ":" << settings.Port << "/snapshot.cgi?user=" << settings.Username << "&pwd=" << settings.Password;
  131. Image::JpegImage image;
  132. try
  133. {
  134. Http::HttpRequest request(url.str());
  135. image.SetImageData(pHttpClient->Open(request));
  136. }
  137. catch (const std::exception& e)
  138. {
  139. std::stringstream ss;
  140. ss << "WatchBotRecorder::Snapshot() - Error: " << e.what() << std::endl;
  141. Logging::Log(Logging::Severity::Error, ss.str());
  142. }
  143. return image;
  144. }
  145. } // namespace Recorder
  146. } // namespace CameraRecorder