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.

160 lines
3.0 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. #include "stream.h"
  2. #include <algorithm>
  3. #include <chrono>
  4. #include "../codecs/h264/h264.h"
  5. namespace mtx {
  6. int64_t nowMs()
  7. {
  8. return std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::steady_clock::now().time_since_epoch()).count();
  9. }
  10. int64_t nowUs()
  11. {
  12. return std::chrono::duration_cast<std::chrono::microseconds>(std::chrono::system_clock::now().time_since_epoch()).count();
  13. }
  14. Stream::Stream(const StreamDesc & desc) : m_desc(desc) {}
  15. void Stream::addReader(StreamReader * r)
  16. {
  17. std::lock_guard<std::mutex> lock(m_mutex);
  18. m_readers.push_back(r);
  19. }
  20. void Stream::removeReader(StreamReader * r)
  21. {
  22. std::lock_guard<std::mutex> lock(m_mutex);
  23. m_readers.erase(std::remove(m_readers.begin(), m_readers.end(), r), m_readers.end());
  24. }
  25. size_t Stream::readerCount() const
  26. {
  27. std::lock_guard<std::mutex> lock(m_mutex);
  28. return m_readers.size();
  29. }
  30. StreamDesc Stream::desc() const
  31. {
  32. std::lock_guard<std::mutex> lock(m_mutex);
  33. return m_desc;
  34. }
  35. void Stream::writeUnit(const UnitPtr & unit)
  36. {
  37. m_nBytes += unit->byteSize();
  38. std::lock_guard<std::mutex> lock(m_mutex);
  39. for (const Bytes & n : unit->nalus)
  40. {
  41. int t = h264::naluType(n);
  42. if (t == h264::NALU_SPS && n != m_desc.sps)
  43. {
  44. m_desc.sps = n;
  45. h264::SpsInfo tInfo;
  46. if (h264::parseSps(n, tInfo))
  47. {
  48. m_desc.width = tInfo.width;
  49. m_desc.height = tInfo.height;
  50. }
  51. }
  52. else if (t == h264::NALU_PPS && n != m_desc.pps)
  53. {
  54. m_desc.pps = n;
  55. }
  56. }
  57. for (StreamReader * r : m_readers)
  58. {
  59. r->push(unit);
  60. }
  61. }
  62. AsyncReader::AsyncReader(const LogParent * parent, size_t queueSize) : m_pParent(parent), m_nQueueSize(queueSize) {}
  63. AsyncReader::~AsyncReader()
  64. {
  65. stop();
  66. }
  67. void AsyncReader::start(Handler handler)
  68. {
  69. m_handler = std::move(handler);
  70. m_bRun = true;
  71. m_thread = std::thread(&AsyncReader::run, this);
  72. }
  73. void AsyncReader::stop()
  74. {
  75. {
  76. std::lock_guard<std::mutex> lock(m_mutex);
  77. if (! m_bRun)
  78. {
  79. return;
  80. }
  81. m_bRun = false;
  82. }
  83. m_cond.notify_all();
  84. if (m_thread.joinable())
  85. {
  86. m_thread.join();
  87. }
  88. }
  89. void AsyncReader::push(const UnitPtr & unit)
  90. {
  91. {
  92. std::lock_guard<std::mutex> lock(m_mutex);
  93. if (! m_bRun)
  94. {
  95. return;
  96. }
  97. if (m_bWaitIdr)
  98. {
  99. if (! unit->idr)
  100. {
  101. return;
  102. }
  103. m_bWaitIdr = false;
  104. }
  105. if (m_queue.size() >= m_nQueueSize)
  106. {
  107. // Reader too slow: drop what is queued and resume at the next IDR
  108. m_nDropped += m_queue.size() + 1;
  109. m_queue.clear();
  110. m_bWaitIdr = true;
  111. int64_t tNow = nowMs();
  112. if (m_pParent != nullptr && tNow - m_tLastDropLog > 1000)
  113. {
  114. m_tLastDropLog = tNow;
  115. m_pParent->log(LogLevel::Warn, "reader is too slow, discarding %llu units", (unsigned long long) m_nDropped);
  116. m_nDropped = 0;
  117. }
  118. return;
  119. }
  120. m_queue.push_back(unit);
  121. }
  122. m_cond.notify_one();
  123. }
  124. void AsyncReader::run()
  125. {
  126. while (true)
  127. {
  128. UnitPtr tUnit;
  129. {
  130. std::unique_lock<std::mutex> lock(m_mutex);
  131. m_cond.wait(lock, [this] { return ! m_bRun || ! m_queue.empty(); });
  132. if (! m_bRun)
  133. {
  134. return;
  135. }
  136. tUnit = m_queue.front();
  137. m_queue.pop_front();
  138. }
  139. m_handler(tUnit);
  140. }
  141. }
  142. } // namespace mtx