提交 feb7fd40 authored 作者: Iñaki Baz Castillo's avatar Iñaki Baz Castillo

Producer: avoid unneeded key frames

上级 f2939643
......@@ -82,6 +82,7 @@ namespace RTC
RTC::RtpStreamRecv* GetRtpStream(RTC::RtpPacket* packet);
RTC::RtpStreamRecv* CreateRtpStream(
RTC::RtpPacket* packet, const RTC::RtpCodecParameters& mediaCodec, size_t encodingIdx);
void NotifyNewRtpStream(RTC::RtpStreamRecv* rtpStream);
bool MangleRtpPacket(RTC::RtpPacket* packet, RTC::RtpStreamRecv* rtpStream) const;
void PostProcessRtpPacket(RTC::RtpPacket* packet);
void EmitScore() const;
......@@ -117,6 +118,7 @@ namespace RTC
std::map<uint32_t, uint32_t> mapMappedSsrcSsrc;
struct RTC::RtpHeaderExtensionIds rtpHeaderExtensionIds;
bool paused{ false };
RTC::RtpPacket* currentRtpPacket{ nullptr };
// Timestamp when last RTCP was sent.
uint64_t lastRtcpSentTime{ 0 };
uint16_t maxRtcpInterval{ 0 };
......
......@@ -412,6 +412,12 @@ namespace RTC
{
MS_TRACE();
// Reset current packet.
this->currentRtpPacket = nullptr;
// Count number of RTP streams.
auto numRtpStreamsBefore = this->mapSsrcRtpStream.size();
auto* rtpStream = GetRtpStream(packet);
if (rtpStream == nullptr)
......@@ -426,7 +432,13 @@ namespace RTC
{
// Process the packet.
if (!rtpStream->ReceivePacket(packet))
{
// May have to announce a new RTP stream to the listener.
if (this->mapSsrcRtpStream.size() > numRtpStreamsBefore)
NotifyNewRtpStream(rtpStream);
return;
}
}
// RTX packet.
else if (packet->GetSsrc() == rtpStream->GetRtxSsrc())
......@@ -443,6 +455,12 @@ namespace RTC
if (packet->IsKeyFrame())
{
// TODO
MS_ERROR(
"key frame received [ssrc:%" PRIu32 ", seq:%" PRIu16 "]",
packet->GetSsrc(),
packet->GetSequenceNumber());
MS_DEBUG_TAG(
rtp,
"key frame received [ssrc:%" PRIu32 ", seq:%" PRIu16 "]",
......@@ -454,6 +472,23 @@ namespace RTC
this->keyFrameRequestManager->KeyFrameReceived(packet->GetSsrc());
}
// May have to announce a new RTP stream to the listener.
if (this->mapSsrcRtpStream.size() > numRtpStreamsBefore)
{
// Request a key frame for this stream since we may have lost the first packets
// (do not do it if this is a key frame).
if (this->keyFrameRequestManager && !this->paused && !packet->IsKeyFrame())
this->keyFrameRequestManager->ForceKeyFrameNeeded(packet->GetSsrc());
// Update current packet.
this->currentRtpPacket = packet;
NotifyNewRtpStream(rtpStream);
// Reset current packet.
this->currentRtpPacket = nullptr;
}
// If paused stop here.
if (this->paused)
return;
......@@ -522,6 +557,26 @@ namespace RTC
uint32_t ssrc = it->second;
// If the current RTP packet is a key frame for the given mapped SSRC do
// nothing since we are gonna provide Consumers with the requested key frame
// right now.
//
// NOTE: We know that this may only happen before calling MangleRtpPacket()
// so the SSRC of the packet is still the original one and not the mapped one.
//
// clang-format off
if (
this->currentRtpPacket &&
this->currentRtpPacket->GetSsrc() == ssrc &&
this->currentRtpPacket->IsKeyFrame()
)
// clang-format on
{
MS_ERROR("----- IGNORING BECAUSE CURRENT PACKET IS KEY FRAME [ssrc:%" PRIu32 "]", ssrc);
return;
}
this->keyFrameRequestManager->KeyFrameNeeded(ssrc);
}
......@@ -775,21 +830,23 @@ namespace RTC
if (this->paused)
rtpStream->Pause();
// Notify to the listener.
this->listener->OnProducerNewRtpStream(
this, static_cast<RTC::RtpStream*>(rtpStream), encodingMapping.mappedSsrc);
// Request a key frame for this stream since we may have lost the first packets
// (do not do it if this is a key frame).
if (this->keyFrameRequestManager && !this->paused && !packet->IsKeyFrame())
this->keyFrameRequestManager->ForceKeyFrameNeeded(ssrc);
// Emit the first score event right now.
EmitScore();
return rtpStream;
}
void Producer::NotifyNewRtpStream(RTC::RtpStreamRecv* rtpStream)
{
MS_TRACE();
auto mappedSsrc = this->mapRtpStreamMappedSsrc.at(rtpStream);
// Notify the listener.
this->listener->OnProducerNewRtpStream(
this, static_cast<RTC::RtpStream*>(rtpStream), mappedSsrc);
}
inline bool Producer::MangleRtpPacket(RTC::RtpPacket* packet, RTC::RtpStreamRecv* rtpStream) const
{
MS_TRACE();
......@@ -1014,6 +1071,8 @@ namespace RTC
auto* rtpStream = it->second;
MS_ERROR("----- sending PLI [ssrc:%" PRIu32 "]", rtpStream->GetSsrc());
rtpStream->RequestKeyFrame();
}
} // namespace RTC
Markdown 格式
0% 或
您添加了 0 人 到此讨论。请谨慎行事。
请先完成此评论的编辑!
请 注册 或者 后发表评论