VStarCamRecorder.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. #include "VStarCamRecorder.h"
  2. #include "Util.h"
  3. #include <HttpClient.h>
  4. #include <Logging.h>
  5. #include <fstream>
  6. #include <sstream>
  7. namespace CameraRecorder {
  8. namespace Recorder {
  9. VStarCamRecorder::VStarCamRecorder(Http::HttpClient* pHttpClient, const Settings& settings) :
  10. m_pHttpClient(pHttpClient),
  11. m_settings(settings)
  12. {
  13. }
  14. VStarCamRecorder::~VStarCamRecorder()
  15. {
  16. }
  17. std::string VStarCamRecorder::Snapshot(ctpl::thread_pool* pThreadPool)
  18. {
  19. Logging::Log(Logging::Severity::Debug, "VStarCamRecorder Snapshot");
  20. std::string dateTimeString = GetDateTimeString(true);
  21. std::string fileName = GetFileName(m_settings, "jpg", dateTimeString, 0);
  22. Http::HttpClient* pHttpClient = m_pHttpClient;
  23. Settings settings = m_settings;
  24. VStarCamRecorder::Snapshots(pHttpClient, settings, dateTimeString, 1);
  25. return fileName;
  26. }
  27. std::string VStarCamRecorder::MultiSnapshot(ctpl::thread_pool* pThreadPool, int numberOfImages)
  28. {
  29. Logging::Log(Logging::Severity::Debug, "VStarCamRecorder MultiSnapshot");
  30. std::string dateTimeString = GetDateTimeString(true);
  31. std::string fileName = GetFileName(m_settings, "jpg", dateTimeString, 0);
  32. Http::HttpClient* pHttpClient = m_pHttpClient;
  33. Settings settings = m_settings;
  34. pThreadPool->push( [pHttpClient, settings, dateTimeString, numberOfImages](int) { VStarCamRecorder::Snapshots(pHttpClient, settings, dateTimeString, numberOfImages); } );
  35. return fileName;
  36. }
  37. std::string VStarCamRecorder::Video(ctpl::thread_pool* pThreadPool, const std::string& ffmpeg)
  38. {
  39. Logging::Log(Logging::Severity::Debug, "VStarCamRecorder Video");
  40. int port = 10554;
  41. std::stringstream url;
  42. url << "rtsp://" << m_settings.Username << ":" << m_settings.Password << "@" << m_settings.IpAddress << ":" << port << "/udp/av0_0";
  43. std::string dateTimeString = GetDateTimeString(true);
  44. std::string fileName = GetFileName(m_settings, "mp4", dateTimeString, 0);
  45. std::stringstream command;
  46. command << ffmpeg << " -i \"" << url.str() << "\" -t 30 -c:v copy -c:a aac -strict experimental " << fileName << " > /dev/null 2>&1";
  47. std::string cmd(command.str());
  48. Logging::Log(Logging::Severity::Debug, cmd);
  49. pThreadPool->push( [cmd](int) { int retval = std::system(cmd.c_str()); } );
  50. return fileName;
  51. }
  52. std::string VStarCamRecorder::GetFileName(const Settings& settings, const std::string& extension, const std::string& dateTimeString, int imageIndex)
  53. {
  54. filesystem::path fileLocation(settings.Path);
  55. fileLocation = fileLocation / filesystem::path(settings.IpAddress);
  56. std::string fileName = std::string("VSTA000000XXXXX_0_").append(dateTimeString).append("_").append(std::to_string(imageIndex)).append(".").append(extension);
  57. filesystem::path fullFileName = fileLocation / filesystem::path(fileName);
  58. return fullFileName.str();
  59. }
  60. void VStarCamRecorder::Snapshots(Http::HttpClient* pHttpClient, const Settings& settings, std::string dateTimeString, int numberOfImages)
  61. {
  62. for (int i = 0; i < numberOfImages; ++i)
  63. VStarCamRecorder::Snapshot(pHttpClient, settings, VStarCamRecorder::GetFileName(settings, "jpg", dateTimeString, i));
  64. }
  65. void VStarCamRecorder::Snapshot(Http::HttpClient* pHttpClient, const Settings& settings, const filesystem::path& fileName)
  66. {
  67. std::stringstream url;
  68. url << "http://" << settings.Username << ":" << settings.Password << "@" << settings.IpAddress << ":" << settings.Port << "/img/snapshot.cgi?res=0";
  69. try
  70. {
  71. std::ofstream file(fileName.str());
  72. file << pHttpClient->GetUrlContents(url.str());
  73. file.close();
  74. }
  75. catch (const std::exception& e)
  76. {
  77. std::stringstream ss;
  78. ss << "VStarCamRecorder::Snapshot() - Error: " << e.what() << std::endl;
  79. Logging::Log(Logging::Severity::Error, ss.str());
  80. }
  81. }
  82. } // namespace Recorder
  83. } // namespace CameraRecorder