FoscamRecorder.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. #include "FoscamRecorder.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 <thread>
  11. namespace CameraRecorder {
  12. namespace Recorder {
  13. FoscamRecorder::FoscamRecorder(Http::HttpClient* pHttpClient, const Settings& settings) :
  14. m_pHttpClient(pHttpClient),
  15. m_settings(settings)
  16. {
  17. }
  18. FoscamRecorder::~FoscamRecorder()
  19. {
  20. }
  21. std::string FoscamRecorder::Snapshot(ctpl::thread_pool* pThreadPool, const std::string& ffmpeg)
  22. {
  23. Logging::Log(Logging::Severity::Debug, "FoscamRecorder 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. //if (m_pRecorderMutex->IsRecording(m_settings.IpAddress))
  42. // return std::string();
  43. std::string dateTimeString = Util::GetDateTimeString();
  44. std::string fileName = GetFileName(m_settings, "jpg", dateTimeString);
  45. Http::HttpClient* pHttpClient = m_pHttpClient;
  46. Settings settings = m_settings;
  47. FoscamRecorder::Snapshots(pHttpClient, settings, dateTimeString, 1);
  48. return fileName;
  49. }
  50. std::string FoscamRecorder::MultiSnapshot(ctpl::thread_pool* pThreadPool, const std::string& ffmpeg, int numberOfImages)
  51. {
  52. Logging::Log(Logging::Severity::Debug, "FoscamRecorder MultiSnapshot");
  53. std::string folderName = GetFolderName(m_settings);
  54. if (!Util::FolderExists(folderName))
  55. {
  56. try
  57. {
  58. Util::CreateFolder(folderName);
  59. }
  60. catch (const std::exception& e)
  61. {
  62. std::cerr << "Exception caught" << std::endl;
  63. std::stringstream ss;
  64. ss << "Type : " << typeid(e).name() << std::endl;
  65. ss << "ERROR: " << e.what() << std::endl;
  66. Logging::Log(Logging::Severity::Error, ss.str());
  67. return std::string();
  68. }
  69. }
  70. std::string dateTimeString = Util::GetDateTimeString();
  71. std::string fileName = GetFileName(m_settings, "jpg", dateTimeString);
  72. Http::HttpClient* pHttpClient = m_pHttpClient;
  73. Settings settings = m_settings;
  74. pThreadPool->push( [pHttpClient, settings, dateTimeString, numberOfImages](int) { FoscamRecorder::Snapshots(pHttpClient, settings, dateTimeString, numberOfImages); } );
  75. return fileName;
  76. }
  77. std::string FoscamRecorder::Video(ctpl::thread_pool* pThreadPool, const std::string& ffmpeg)
  78. {
  79. Logging::Log(Logging::Severity::Debug, "FoscamRecorder Video");
  80. int port = 65534;
  81. std::stringstream url;
  82. url << "rtsp://" << m_settings.Username << ":" << m_settings.Password << "@" << m_settings.IpAddress << ":" << port << "/videoSub";
  83. std::string folderName = GetFolderName(m_settings);
  84. if (!Util::FolderExists(folderName))
  85. {
  86. try
  87. {
  88. Util::CreateFolder(folderName);
  89. }
  90. catch (const std::exception& e)
  91. {
  92. std::cerr << "Exception caught" << std::endl;
  93. std::stringstream ss;
  94. ss << "Type : " << typeid(e).name() << std::endl;
  95. ss << "ERROR: " << e.what() << std::endl;
  96. Logging::Log(Logging::Severity::Error, ss.str());
  97. return std::string();
  98. }
  99. }
  100. std::string dateTimeString = Util::GetDateTimeString();
  101. std::string fileName = GetFileName(m_settings, "mp4", dateTimeString);
  102. std::stringstream command;
  103. 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";
  104. std::string cmd(command.str());
  105. Logging::Log(Logging::Severity::Debug, cmd);
  106. pThreadPool->push( [cmd](int) { int retval = std::system(cmd.c_str()); } );
  107. return fileName;
  108. }
  109. std::string FoscamRecorder::GetFolderName(const Settings& settings)
  110. {
  111. filesystem::path fileLocation(settings.Path);
  112. fileLocation = fileLocation / filesystem::path(settings.IpAddress);
  113. return fileLocation.str();
  114. }
  115. std::string FoscamRecorder::GetFileName(const Settings& settings, const std::string& extension, const std::string& dateTimeString)
  116. {
  117. filesystem::path fileLocation(GetFolderName(settings));
  118. std::string fileName = std::string(dateTimeString).append(".").append(extension);
  119. filesystem::path fullFileName = fileLocation / filesystem::path(fileName);
  120. return fullFileName.str();
  121. }
  122. void FoscamRecorder::Snapshots(Http::HttpClient* pHttpClient, const Settings& settings, std::string dateTimeString, int numberOfImages)
  123. {
  124. std::vector<Image::JpegImage> images;
  125. for (int i = 0; i < numberOfImages; ++i)
  126. images.push_back(FoscamRecorder::Snapshot(pHttpClient, settings));
  127. Image::JpegImageCollection collection(images);
  128. collection.Save(FoscamRecorder::GetFileName(settings, "jpg", dateTimeString));
  129. }
  130. Image::JpegImage FoscamRecorder::Snapshot(Http::HttpClient* pHttpClient, const Settings& settings)
  131. {
  132. std::stringstream url;
  133. url << "http://" << settings.IpAddress << ":" << settings.Port << "/cgi-bin/CGIProxy.fcgi?cmd=snapPicture2&usr=" << settings.Username << "&pwd=" << settings.Password;
  134. Image::JpegImage image;
  135. try
  136. {
  137. Http::HttpRequest request(url.str());
  138. image.SetImageData(pHttpClient->Open(request));
  139. }
  140. catch (const std::exception& e)
  141. {
  142. std::stringstream ss;
  143. ss << "FoscamRecorder::Snapshot() - Error: " << e.what() << std::endl;
  144. Logging::Log(Logging::Severity::Error, ss.str());
  145. }
  146. return image;
  147. }
  148. } // namespace Recorder
  149. } // namespace CameraRecorder