RTSPRecorder.cpp 6.3 KB

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