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.

209 lines
4.7 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. #include "h264Feeder.h"
  2. #include <memory>
  3. #include "../codecs/h264/h264.h"
  4. namespace mtx {
  5. void H264Feeder::setParent(SourceParent * parent)
  6. {
  7. std::lock_guard<std::mutex> lock(m_mutex);
  8. if (parent == m_pParent)
  9. {
  10. return;
  11. }
  12. if (m_pParent != nullptr && m_bReady)
  13. {
  14. m_pParent->sourceSetNotReady();
  15. }
  16. m_pParent = parent;
  17. m_bReady = false;
  18. m_vNalus.clear();
  19. }
  20. void H264Feeder::setParameterSets(const std::vector<Bytes> & nalus)
  21. {
  22. std::lock_guard<std::mutex> lock(m_mutex);
  23. for (const Bytes & n : nalus)
  24. {
  25. if (h264::naluType(n) == h264::NALU_SPS)
  26. {
  27. m_sps = n;
  28. }
  29. else if (h264::naluType(n) == h264::NALU_PPS)
  30. {
  31. m_pps = n;
  32. }
  33. }
  34. }
  35. void H264Feeder::setExtradata(const uint8_t * data, size_t size)
  36. {
  37. int tLen = 0;
  38. std::vector<Bytes> tSets = h264::parameterSetsFromExtradata(data, size, &tLen);
  39. m_nAvccLengthSize = tLen;
  40. setParameterSets(tSets);
  41. }
  42. void H264Feeder::pushNalu(const uint8_t * data, size_t size, int64_t ptUs, bool marker, bool synced)
  43. {
  44. std::lock_guard<std::mutex> lock(m_mutex);
  45. // A new presentation time starts a new unit (a camera that does not set the marker bit). Small changes
  46. // are not a new unit: every RTCP sender report moves live555's mapping a little, also within a picture.
  47. if (! m_vNalus.empty() && (ptUs - m_nPtUs > 2000 || m_nPtUs - ptUs > 2000))
  48. {
  49. flushLocked();
  50. }
  51. if (data != nullptr && size > 0)
  52. {
  53. int tType = data[0] & 0x1f;
  54. if (tType != h264::NALU_AUD && tType != 0)
  55. {
  56. m_vNalus.emplace_back(data, data + size);
  57. }
  58. }
  59. if (m_vNalus.size() <= 1)
  60. {
  61. m_nPtUs = ptUs; // the time of the unit is the time of its first NAL unit
  62. m_bSynced = synced;
  63. }
  64. if (marker)
  65. {
  66. flushLocked();
  67. }
  68. }
  69. void H264Feeder::pushAnnexB(const uint8_t * data, size_t size, int64_t ptsUs)
  70. {
  71. std::vector<Bytes> tNalus = h264::splitAnnexB(data, size);
  72. std::lock_guard<std::mutex> lock(m_mutex);
  73. m_bSynced = false;
  74. unitLocked(std::move(tNalus), ptsUs >= 0 ? ptsUs : nowUs(), nowUs());
  75. }
  76. void H264Feeder::pushAvcc(const uint8_t * data, size_t size, int lengthSize, int64_t ptsUs)
  77. {
  78. std::vector<Bytes> tNalus = h264::splitAvcc(data, size, lengthSize);
  79. std::lock_guard<std::mutex> lock(m_mutex);
  80. m_bSynced = false;
  81. unitLocked(std::move(tNalus), ptsUs >= 0 ? ptsUs : nowUs(), nowUs());
  82. }
  83. void H264Feeder::flushLocked()
  84. {
  85. if (m_vNalus.empty())
  86. {
  87. return;
  88. }
  89. std::vector<Bytes> tNalus;
  90. tNalus.swap(m_vNalus);
  91. unitLocked(std::move(tNalus), m_nPtUs, m_nPtUs);
  92. }
  93. void H264Feeder::unitLocked(std::vector<Bytes> && nalus, int64_t ptUs, int64_t wallUs)
  94. {
  95. if (nalus.empty() || m_pParent == nullptr)
  96. {
  97. return;
  98. }
  99. bool tIdr = false;
  100. for (const Bytes & n : nalus)
  101. {
  102. int t = h264::naluType(n);
  103. if (t == h264::NALU_SPS)
  104. {
  105. m_sps = n;
  106. }
  107. else if (t == h264::NALU_PPS)
  108. {
  109. m_pps = n;
  110. }
  111. else if (t == h264::NALU_IDR)
  112. {
  113. tIdr = true;
  114. }
  115. }
  116. // 90 kHz pts starting at 0. A jump (RTCP synchronisation, a restarted source, a clock step) is replaced
  117. // by one frame interval: the readers need increasing time stamps.
  118. int64_t tRaw = ptUs * 9 / 100;
  119. if (! m_bHavePts)
  120. {
  121. m_bHavePts = true;
  122. m_bLastSynced = m_bSynced;
  123. m_nOffset = -tRaw;
  124. }
  125. else if (m_bSynced != m_bLastSynced)
  126. {
  127. m_bLastSynced = m_bSynced;
  128. m_nOffset = m_nLastPts + m_nLastDelta - tRaw;
  129. }
  130. int64_t tPts = tRaw + m_nOffset;
  131. if (tPts <= m_nLastPts && m_nUnits > 0)
  132. {
  133. if (tPts == m_nLastPts)
  134. {
  135. tPts = m_nLastPts + 1; // same time stamp (should not happen): keep increasing
  136. }
  137. else
  138. {
  139. m_nOffset = m_nLastPts + m_nLastDelta - tRaw;
  140. tPts = tRaw + m_nOffset;
  141. }
  142. }
  143. else if (tPts - m_nLastPts >= 90000 * 5 && m_nUnits > 0)
  144. {
  145. m_nOffset = m_nLastPts + m_nLastDelta - tRaw; // a gap of 5 s or more: a clock step, not a pause
  146. tPts = tRaw + m_nOffset;
  147. }
  148. else if (m_nUnits > 0 && tPts - m_nLastPts < 90000)
  149. {
  150. m_nLastDelta = tPts - m_nLastPts;
  151. }
  152. m_nLastPts = tPts;
  153. if (! m_bReady)
  154. {
  155. if (m_sps.empty() || m_pps.empty())
  156. {
  157. return; // a reader cannot use the stream without the codec parameters
  158. }
  159. StreamDesc tDesc;
  160. h264::SpsInfo tInfo;
  161. tDesc.sps = m_sps;
  162. tDesc.pps = m_pps;
  163. if (h264::parseSps(m_sps, tInfo))
  164. {
  165. tDesc.width = tInfo.width;
  166. tDesc.height = tInfo.height;
  167. }
  168. m_bReady = true;
  169. m_pParent->sourceSetReady(tDesc);
  170. }
  171. auto tUnit = std::make_shared<Unit>();
  172. tUnit->pts = tPts;
  173. tUnit->ntpUs = wallUs;
  174. tUnit->idr = tIdr;
  175. tUnit->nalus = std::move(nalus);
  176. m_nUnits++;
  177. m_pParent->sourceWriteUnit(tUnit);
  178. }
  179. void H264Feeder::reset()
  180. {
  181. std::lock_guard<std::mutex> lock(m_mutex);
  182. m_vNalus.clear();
  183. if (m_pParent != nullptr && m_bReady)
  184. {
  185. m_pParent->sourceSetNotReady();
  186. }
  187. m_bReady = false;
  188. }
  189. bool H264Feeder::ready() const
  190. {
  191. std::lock_guard<std::mutex> lock(m_mutex);
  192. return m_bReady;
  193. }
  194. } // namespace mtx