You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

119 lines
2.4 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. #include "path.h"
  2. #include "../recorder/recorder.h"
  3. namespace mtx {
  4. Path::Path(const PathConf & conf, const Conf & global) : LogParent("[path " + conf.name + "] "), m_conf(conf), m_global(global) {}
  5. Path::~Path()
  6. {
  7. stop();
  8. }
  9. void Path::start()
  10. {
  11. m_pSource.reset(new RtspSource(m_conf, m_global, this));
  12. m_pSource->start();
  13. }
  14. void Path::stop()
  15. {
  16. if (m_pSource)
  17. {
  18. m_pSource->stop(); // calls sourceSetNotReady() if it was ready
  19. m_pSource.reset();
  20. }
  21. }
  22. PathInfo Path::info() const
  23. {
  24. std::lock_guard<std::mutex> lock(m_mutex);
  25. PathInfo tI;
  26. tI.name = m_conf.name;
  27. tI.source = m_conf.source;
  28. tI.record = m_conf.record;
  29. tI.ready = (m_pStream != nullptr);
  30. tI.readySinceUs = m_tReadySinceUs;
  31. tI.readers = m_readers.size();
  32. if (m_pStream)
  33. {
  34. tI.desc = m_pStream->desc();
  35. tI.bytesReceived = m_pStream->bytesReceived();
  36. }
  37. return tI;
  38. }
  39. bool Path::addReader(PathReader * r, StreamDesc & desc, std::string & err)
  40. {
  41. std::lock_guard<std::mutex> lock(m_mutex);
  42. if (! m_pStream)
  43. {
  44. err = "no stream is available on path '" + m_conf.name + "'";
  45. return false;
  46. }
  47. desc = m_pStream->desc();
  48. m_readers.insert(r);
  49. m_pStream->addReader(r);
  50. return true;
  51. }
  52. void Path::removeReader(PathReader * r)
  53. {
  54. std::lock_guard<std::mutex> lock(m_mutex);
  55. if (m_readers.erase(r) > 0 && m_pStream)
  56. {
  57. m_pStream->removeReader(r);
  58. }
  59. }
  60. void Path::sourceSetReady(const StreamDesc & desc)
  61. {
  62. std::lock_guard<std::mutex> lock(m_mutex);
  63. m_pStream = std::make_shared<Stream>(desc);
  64. m_tReadySinceUs = nowUs();
  65. log(LogLevel::Info, "stream is available and online, 1 track (H264 %dx%d)", desc.width, desc.height);
  66. if (m_conf.record)
  67. {
  68. m_pRecorder.reset(new Recorder(m_conf.name, m_global, m_pStream, this));
  69. m_pRecorder->start();
  70. }
  71. }
  72. void Path::sourceSetNotReady()
  73. {
  74. std::unique_ptr<Recorder> tRec;
  75. {
  76. std::lock_guard<std::mutex> lock(m_mutex);
  77. if (! m_pStream)
  78. {
  79. return;
  80. }
  81. for (PathReader * r : m_readers)
  82. {
  83. m_pStream->removeReader(r);
  84. r->pathNotReady();
  85. }
  86. m_readers.clear();
  87. tRec = std::move(m_pRecorder);
  88. m_pStream.reset();
  89. m_tReadySinceUs = 0;
  90. }
  91. tRec.reset(); // closes the segment
  92. log(LogLevel::Info, "stream is not available anymore");
  93. }
  94. void Path::sourceWriteUnit(const UnitPtr & unit)
  95. {
  96. std::shared_ptr<Stream> tStream;
  97. {
  98. std::lock_guard<std::mutex> lock(m_mutex);
  99. tStream = m_pStream;
  100. }
  101. if (tStream)
  102. {
  103. tStream->writeUnit(unit);
  104. }
  105. }
  106. } // namespace mtx