VStarCamRecorder.cpp 5.3 KB

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