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

#include "h264Feeder.h"
#include <memory>
#include "../codecs/h264/h264.h"
namespace mtx {
void H264Feeder::setParent(SourceParent * parent)
{
std::lock_guard<std::mutex> lock(m_mutex);
if (parent == m_pParent)
{
return;
}
if (m_pParent != nullptr && m_bReady)
{
m_pParent->sourceSetNotReady();
}
m_pParent = parent;
m_bReady = false;
m_vNalus.clear();
}
void H264Feeder::setParameterSets(const std::vector<Bytes> & nalus)
{
std::lock_guard<std::mutex> lock(m_mutex);
for (const Bytes & n : nalus)
{
if (h264::naluType(n) == h264::NALU_SPS)
{
m_sps = n;
}
else if (h264::naluType(n) == h264::NALU_PPS)
{
m_pps = n;
}
}
}
void H264Feeder::setExtradata(const uint8_t * data, size_t size)
{
int tLen = 0;
std::vector<Bytes> tSets = h264::parameterSetsFromExtradata(data, size, &tLen);
m_nAvccLengthSize = tLen;
setParameterSets(tSets);
}
void H264Feeder::pushNalu(const uint8_t * data, size_t size, int64_t ptUs, bool marker, bool synced)
{
std::lock_guard<std::mutex> lock(m_mutex);
// A new presentation time starts a new unit (a camera that does not set the marker bit). Small changes
// are not a new unit: every RTCP sender report moves live555's mapping a little, also within a picture.
if (! m_vNalus.empty() && (ptUs - m_nPtUs > 2000 || m_nPtUs - ptUs > 2000))
{
flushLocked();
}
if (data != nullptr && size > 0)
{
int tType = data[0] & 0x1f;
if (tType != h264::NALU_AUD && tType != 0)
{
m_vNalus.emplace_back(data, data + size);
}
}
if (m_vNalus.size() <= 1)
{
m_nPtUs = ptUs; // the time of the unit is the time of its first NAL unit
m_bSynced = synced;
}
if (marker)
{
flushLocked();
}
}
void H264Feeder::pushAnnexB(const uint8_t * data, size_t size, int64_t ptsUs)
{
std::vector<Bytes> tNalus = h264::splitAnnexB(data, size);
std::lock_guard<std::mutex> lock(m_mutex);
m_bSynced = false;
unitLocked(std::move(tNalus), ptsUs >= 0 ? ptsUs : nowUs(), nowUs());
}
void H264Feeder::pushAvcc(const uint8_t * data, size_t size, int lengthSize, int64_t ptsUs)
{
std::vector<Bytes> tNalus = h264::splitAvcc(data, size, lengthSize);
std::lock_guard<std::mutex> lock(m_mutex);
m_bSynced = false;
unitLocked(std::move(tNalus), ptsUs >= 0 ? ptsUs : nowUs(), nowUs());
}
void H264Feeder::flushLocked()
{
if (m_vNalus.empty())
{
return;
}
std::vector<Bytes> tNalus;
tNalus.swap(m_vNalus);
unitLocked(std::move(tNalus), m_nPtUs, m_nPtUs);
}
void H264Feeder::unitLocked(std::vector<Bytes> && nalus, int64_t ptUs, int64_t wallUs)
{
if (nalus.empty() || m_pParent == nullptr)
{
return;
}
bool tIdr = false;
for (const Bytes & n : nalus)
{
int t = h264::naluType(n);
if (t == h264::NALU_SPS)
{
m_sps = n;
}
else if (t == h264::NALU_PPS)
{
m_pps = n;
}
else if (t == h264::NALU_IDR)
{
tIdr = true;
}
}
// 90 kHz pts starting at 0. A jump (RTCP synchronisation, a restarted source, a clock step) is replaced
// by one frame interval: the readers need increasing time stamps.
int64_t tRaw = ptUs * 9 / 100;
if (! m_bHavePts)
{
m_bHavePts = true;
m_bLastSynced = m_bSynced;
m_nOffset = -tRaw;
}
else if (m_bSynced != m_bLastSynced)
{
m_bLastSynced = m_bSynced;
m_nOffset = m_nLastPts + m_nLastDelta - tRaw;
}
int64_t tPts = tRaw + m_nOffset;
if (tPts <= m_nLastPts && m_nUnits > 0)
{
if (tPts == m_nLastPts)
{
tPts = m_nLastPts + 1; // same time stamp (should not happen): keep increasing
}
else
{
m_nOffset = m_nLastPts + m_nLastDelta - tRaw;
tPts = tRaw + m_nOffset;
}
}
else if (tPts - m_nLastPts >= 90000 * 5 && m_nUnits > 0)
{
m_nOffset = m_nLastPts + m_nLastDelta - tRaw; // a gap of 5 s or more: a clock step, not a pause
tPts = tRaw + m_nOffset;
}
else if (m_nUnits > 0 && tPts - m_nLastPts < 90000)
{
m_nLastDelta = tPts - m_nLastPts;
}
m_nLastPts = tPts;
if (! m_bReady)
{
if (m_sps.empty() || m_pps.empty())
{
return; // a reader cannot use the stream without the codec parameters
}
StreamDesc tDesc;
h264::SpsInfo tInfo;
tDesc.sps = m_sps;
tDesc.pps = m_pps;
if (h264::parseSps(m_sps, tInfo))
{
tDesc.width = tInfo.width;
tDesc.height = tInfo.height;
}
m_bReady = true;
m_pParent->sourceSetReady(tDesc);
}
auto tUnit = std::make_shared<Unit>();
tUnit->pts = tPts;
tUnit->ntpUs = wallUs;
tUnit->idr = tIdr;
tUnit->nalus = std::move(nalus);
m_nUnits++;
m_pParent->sourceWriteUnit(tUnit);
}
void H264Feeder::reset()
{
std::lock_guard<std::mutex> lock(m_mutex);
m_vNalus.clear();
if (m_pParent != nullptr && m_bReady)
{
m_pParent->sourceSetNotReady();
}
m_bReady = false;
}
bool H264Feeder::ready() const
{
std::lock_guard<std::mutex> lock(m_mutex);
return m_bReady;
}
} // namespace mtx