VStarCamRecorder.cpp 6.6 KB

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