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

Producer: avoid unneeded key frames

上级 f2939643
...@@ -82,6 +82,7 @@ namespace RTC ...@@ -82,6 +82,7 @@ namespace RTC
RTC::RtpStreamRecv* GetRtpStream(RTC::RtpPacket* packet); RTC::RtpStreamRecv* GetRtpStream(RTC::RtpPacket* packet);
RTC::RtpStreamRecv* CreateRtpStream( RTC::RtpStreamRecv* CreateRtpStream(
RTC::RtpPacket* packet, const RTC::RtpCodecParameters& mediaCodec, size_t encodingIdx); RTC::RtpPacket* packet, const RTC::RtpCodecParameters& mediaCodec, size_t encodingIdx);
void NotifyNewRtpStream(RTC::RtpStreamRecv* rtpStream);
bool MangleRtpPacket(RTC::RtpPacket* packet, RTC::RtpStreamRecv* rtpStream) const; bool MangleRtpPacket(RTC::RtpPacket* packet, RTC::RtpStreamRecv* rtpStream) const;
void PostProcessRtpPacket(RTC::RtpPacket* packet); void PostProcessRtpPacket(RTC::RtpPacket* packet);
void EmitScore() const; void EmitScore() const;
...@@ -117,6 +118,7 @@ namespace RTC ...@@ -117,6 +118,7 @@ namespace RTC
std::map<uint32_t, uint32_t> mapMappedSsrcSsrc; std::map<uint32_t, uint32_t> mapMappedSsrcSsrc;
struct RTC::RtpHeaderExtensionIds rtpHeaderExtensionIds; struct RTC::RtpHeaderExtensionIds rtpHeaderExtensionIds;
bool paused{ false }; bool paused{ false };
RTC::RtpPacket* currentRtpPacket{ nullptr };
// Timestamp when last RTCP was sent. // Timestamp when last RTCP was sent.
uint64_t lastRtcpSentTime{ 0 }; uint64_t lastRtcpSentTime{ 0 };
uint16_t maxRtcpInterval{ 0 }; uint16_t maxRtcpInterval{ 0 };
......
...@@ -412,6 +412,12 @@ namespace RTC ...@@ -412,6 +412,12 @@ namespace RTC
{ {
MS_TRACE(); MS_TRACE();
// Reset current packet.
this->currentRtpPacket = nullptr;
// Count number of RTP streams.
auto numRtpStreamsBefore = this->mapSsrcRtpStream.size();
auto* rtpStream = GetRtpStream(packet); auto* rtpStream = GetRtpStream(packet);
if (rtpStream == nullptr) if (rtpStream == nullptr)
...@@ -426,7 +432,13 @@ namespace RTC ...@@ -426,7 +432,13 @@ namespace RTC
{ {
// Process the packet. // Process the packet.
if (!rtpStream->ReceivePacket(packet)) if (!rtpStream->ReceivePacket(packet))
{
// May have to announce a new RTP stream to the listener.
if (this->mapSsrcRtpStream.size() > numRtpStreamsBefore)
NotifyNewRtpStream(rtpStream);
return; return;
}
} }
// RTX packet. // RTX packet.
else if (packet->GetSsrc() == rtpStream->GetRtxSsrc()) else if (packet->GetSsrc() == rtpStream->GetRtxSsrc())
...@@ -443,6 +455,12 @@ namespace RTC ...@@ -443,6 +455,12 @@ namespace RTC
if (packet->IsKeyFrame()) if (packet->IsKeyFrame())
{ {
// TODO
MS_ERROR(
"key frame received [ssrc:%" PRIu32 ", seq:%" PRIu16 "]",
packet->GetSsrc(),
packet->GetSequenceNumber());
MS_DEBUG_TAG( MS_DEBUG_TAG(
rtp, rtp,
"key frame received [ssrc:%" PRIu32 ", seq:%" PRIu16 "]", "key frame received [ssrc:%" PRIu32 ", seq:%" PRIu16 "]",
...@@ -454,6 +472,23 @@ namespace RTC ...@@ -454,6 +472,23 @@ namespace RTC
this->keyFrameRequestManager->KeyFrameReceived(packet->GetSsrc()); 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 paused stop here.
if (this->paused) if (this->paused)
return; return;
...@@ -522,6 +557,26 @@ namespace RTC ...@@ -522,6 +557,26 @@ namespace RTC
uint32_t ssrc = it->second; 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); this->keyFrameRequestManager->KeyFrameNeeded(ssrc);
} }
...@@ -775,21 +830,23 @@ namespace RTC ...@@ -775,21 +830,23 @@ namespace RTC
if (this->paused) if (this->paused)
rtpStream->Pause(); 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. // Emit the first score event right now.
EmitScore(); EmitScore();
return rtpStream; 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 inline bool Producer::MangleRtpPacket(RTC::RtpPacket* packet, RTC::RtpStreamRecv* rtpStream) const
{ {
MS_TRACE(); MS_TRACE();
...@@ -1014,6 +1071,8 @@ namespace RTC ...@@ -1014,6 +1071,8 @@ namespace RTC
auto* rtpStream = it->second; auto* rtpStream = it->second;
MS_ERROR("----- sending PLI [ssrc:%" PRIu32 "]", rtpStream->GetSsrc());
rtpStream->RequestKeyFrame(); rtpStream->RequestKeyFrame();
} }
} // namespace RTC } // namespace RTC
Markdown 格式
0% 或
您添加了 0 人 到此讨论。请谨慎行事。
请先完成此评论的编辑!
请 注册 或者 后发表评论