RTSPRecorder.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. #include "RTSPRecorder.h"
  2. #include "Util.h"
  3. #include <HttpClient.h>
  4. #include <Logging.h>
  5. namespace CameraRecorder {
  6. namespace Recorder {
  7. RTSPRecorder::RTSPRecorder(Http::HttpClient* pHttpClient, const Settings& settings) :
  8. m_pHttpClient(pHttpClient),
  9. m_settings(settings)
  10. {
  11. }
  12. RTSPRecorder::~RTSPRecorder()
  13. {
  14. }
  15. std::string RTSPRecorder::Snapshot(ctpl::thread_pool* pThreadPool, const std::string& ffmpeg)
  16. {
  17. Logging::Log(Logging::Severity::Debug, "RTSPRecorder Snapshot");
  18. std::string dateTimeString = GetDateTimeString();
  19. std::string fileName = GetFileName(m_settings, "jpg", dateTimeString, 0);
  20. Http::HttpClient* pHttpClient = m_pHttpClient;
  21. Settings settings = m_settings;
  22. RTSPRecorder::Snapshots(ffmpeg, settings, dateTimeString, 1);
  23. return fileName;
  24. }
  25. std::string RTSPRecorder::MultiSnapshot(ctpl::thread_pool* pThreadPool, const std::string& ffmpeg, int numberOfImages)
  26. {
  27. Logging::Log(Logging::Severity::Debug, "RTSPRecorder MultiSnapshot");
  28. std::string dateTimeString = GetDateTimeString();
  29. std::string fileName = GetFileName(m_settings, "jpg", dateTimeString, 0);
  30. Http::HttpClient* pHttpClient = m_pHttpClient;
  31. Settings settings = m_settings;
  32. pThreadPool->push( [ffmpeg, settings, dateTimeString, numberOfImages](int) { RTSPRecorder::Snapshots(ffmpeg, settings, dateTimeString, numberOfImages); } );
  33. return fileName;
  34. }
  35. std::string RTSPRecorder::Video(ctpl::thread_pool* pThreadPool, const std::string& ffmpeg)
  36. {
  37. Logging::Log(Logging::Severity::Debug, "RTSPRecorder Video");
  38. std::stringstream url;
  39. url << "rtsp://" << m_settings.Username << ":" << m_settings.Password << "@" << m_settings.IpAddress << ":" << m_settings.Port << m_settings.URL;
  40. std::string dateTimeString = GetDateTimeString();
  41. std::string fileName = GetFileName(m_settings, "mp4", dateTimeString, 0);
  42. std::stringstream command;
  43. 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";
  44. std::string cmd(command.str());
  45. Logging::Log(Logging::Severity::Debug, cmd);
  46. pThreadPool->push( [cmd](int) { int retval = std::system(cmd.c_str()); } );
  47. return fileName;
  48. }
  49. std::string RTSPRecorder::GetFileName(const Settings& settings, const std::string& extension, const std::string& dateTimeString, int imageIndex)
  50. {
  51. filesystem::path fileLocation(settings.Path);
  52. fileLocation = fileLocation / filesystem::path(settings.IpAddress);
  53. std::string fileName = std::string(dateTimeString).append(".").append(extension);
  54. filesystem::path fullFileName = fileLocation / filesystem::path(fileName);
  55. return fullFileName.str();
  56. }
  57. void RTSPRecorder::Snapshots(const std::string& ffmpeg, const Settings& settings, std::string dateTimeString, int numberOfImages)
  58. {
  59. for (int i = 0; i < numberOfImages; ++i)
  60. RTSPRecorder::Snapshot(ffmpeg, settings, RTSPRecorder::GetFileName(settings, "jpg", dateTimeString, i));
  61. }
  62. void RTSPRecorder::Snapshot(const std::string& ffmpeg, const Settings& settings, const filesystem::path& fileName)
  63. {
  64. std::stringstream url;
  65. url << "rtsp://" << settings.Username << ":" << settings.Password << "@" << settings.IpAddress << ":" << settings.Port << settings.URL;
  66. std::stringstream command;
  67. command << ffmpeg << " -rtsp_transport tcp -i \"" << url.str() << "\" -vframes 1 -qscale:v 2 " << fileName << " > /dev/null 2>&1";
  68. std::string cmd(command.str());
  69. Logging::Log(Logging::Severity::Debug, cmd);
  70. int retval = std::system(cmd.c_str());
  71. }
  72. } // namespace Recorder
  73. } // namespace CameraRecorder