RTSPRecorder.cpp 5.1 KB

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