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

- Refactor RTC/Codecs so we have RtpPacket::IsKeyFrame().

- s/fullframe/keyframe/g
上级 3df732bb
......@@ -3,15 +3,31 @@
#include "common.hpp"
#include "RTC/Codecs/VP8.hpp"
#include "RTC/RtpDictionaries.hpp"
#include "RTC/RtpPacket.hpp"
#include "RTC/RtpDictionaries.hpp"
namespace RTC
{
namespace Codecs
{
bool IsKnown(const RTC::RtpCodecMimeType& mimeType);
bool IsKeyFrame(const RTC::RtpCodecMimeType& mimeType, const RTC::RtpPacket* packet);
bool CanBeKeyFrame(const RTC::RtpCodecMimeType& mimeType);
void ProcessRtpPacket(RTC::RtpPacket* packet, const RTC::RtpCodecMimeType& mimeType);
// Inline namespace methods.
inline bool CanBeKeyFrame(const RTC::RtpCodecMimeType& mimeType)
{
if (mimeType.type != RTC::RtpCodecMimeType::Type::VIDEO)
return false;
switch (mimeType.subtype)
{
case RTC::RtpCodecMimeType::Subtype::VP8:
return true;
default:
return false;
}
}
} // namespace Codecs
} // namespace RTC
......
......@@ -2,6 +2,8 @@
#define MS_RTC_CODECS_VP8_HPP
#include "common.hpp"
#include "RTC/RtpPacket.hpp"
#include "RTC/RtpDictionaries.hpp"
namespace RTC
{
......@@ -10,7 +12,7 @@ namespace RTC
class VP8
{
public:
static bool IsKeyFrame(const uint8_t* data, size_t len);
static void ProcessRtpPacket(RTC::RtpPacket* packet);
};
} // namespace Codecs
} // namespace RTC
......
......@@ -56,7 +56,7 @@ namespace RTC
void ReceiveNack(RTC::RTCP::FeedbackRtpNackPacket* nackPacket);
void ReceiveRtcpReceiverReport(RTC::RTCP::ReceiverReport* report);
uint32_t GetTransmissionRate(uint64_t now);
void RequestFullFrame();
void RequestKeyFrame();
private:
void FillSupportedCodecPayloadTypes();
......
......@@ -13,7 +13,7 @@ namespace RTC
{
public:
virtual void OnConsumerClosed(RTC::Consumer* consumer) = 0;
virtual void OnConsumerFullFrameRequired(RTC::Consumer* consumer) = 0;
virtual void OnConsumerKeyFrameRequired(RTC::Consumer* consumer) = 0;
};
} // namespace RTC
......
......@@ -16,7 +16,7 @@ namespace RTC
{
public:
virtual void OnNackGeneratorNackRequired(const std::vector<uint16_t>& seqNumbers) = 0;
virtual void OnNackGeneratorFullFrameRequired() = 0;
virtual void OnNackGeneratorKeyFrameRequired() = 0;
};
private:
......
......@@ -72,7 +72,7 @@ namespace RTC
void GetRtcp(RTC::RTCP::CompoundPacket* packet, uint64_t now);
void ReceiveRtcpFeedback(RTC::RTCP::FeedbackPsPacket* packet) const;
void ReceiveRtcpFeedback(RTC::RTCP::FeedbackRtpPacket* packet) const;
void RequestFullFrame(bool force = false);
void RequestKeyFrame(bool force = false);
const std::set<RTC::RtpEncodingParameters::Profile> GetProfiles() const;
private:
......@@ -111,7 +111,7 @@ namespace RTC
std::map<uint32_t, RTC::RtpStreamRecv*> rtpStreams;
std::map<uint32_t, RTC::RtpStreamRecv*> mapRtxStreams;
std::map<RTC::RtpStreamRecv*, std::set<RTC::RtpEncodingParameters::Profile>> profiles;
Timer* fullFrameRequestBlockTimer{ nullptr };
Timer* keyFrameRequestBlockTimer{ nullptr };
// Others.
std::vector<RtpEncodingParameters> outputEncodings;
struct RTC::Transport::HeaderExtensionIds transportHeaderExtensionIds;
......@@ -119,7 +119,7 @@ namespace RTC
bool paused{ false };
bool rtpRawEventEnabled{ false };
bool rtpObjectEventEnabled{ false };
bool isFullFrameRequested{ false };
bool isKeyFrameRequested{ false };
// Timestamp when last RTCP was sent.
uint64_t lastRtcpSentTime{ 0 };
uint16_t maxRtcpInterval{ 0 };
......
......@@ -79,7 +79,7 @@ namespace RTC
/* Pure virtual methods inherited from RTC::ConsumerListener. */
public:
void OnConsumerClosed(RTC::Consumer* consumer) override;
void OnConsumerFullFrameRequired(RTC::Consumer* consumer) override;
void OnConsumerKeyFrameRequired(RTC::Consumer* consumer) override;
/* Pure virtual methods inherited from Timer::Listener. */
public:
......
......@@ -117,6 +117,8 @@ namespace RTC
bool ReadRid(const uint8_t** data, size_t* len) const;
uint8_t* GetPayload() const;
size_t GetPayloadLength() const;
void SetKeyFrame(bool flag);
bool IsKeyFrame() const;
void Serialize(uint8_t* buffer);
RtpPacket* Clone(uint8_t* buffer) const;
void RtxEncode(uint8_t payloadType, uint32_t ssrc, uint16_t seq);
......@@ -138,6 +140,7 @@ namespace RTC
uint8_t payloadPadding{ 0 };
size_t size{ 0 }; // Full size of the packet in bytes.
uint32_t seq32{ 0 }; // Extended seq number.
bool isKeyFrame{ false }; // Whether this packet contains a key frame.
};
/* Inline static methods. */
......@@ -362,6 +365,16 @@ namespace RTC
{
return this->payloadLength;
}
inline void RtpPacket::SetKeyFrame(bool flag)
{
this->isKeyFrame = flag;
}
inline bool RtpPacket::IsKeyFrame() const
{
return this->isKeyFrame;
}
} // namespace RTC
#endif
......@@ -30,7 +30,7 @@ namespace RTC
RTC::RTCP::ReceiverReport* GetRtcpReceiverReport();
void ReceiveRtcpSenderReport(RTC::RTCP::SenderReport* report);
void SetRtx(uint8_t payloadType, uint32_t ssrc);
void RequestFullFrame();
void RequestKeyFrame();
uint32_t GetBitRate();
private:
......@@ -44,7 +44,7 @@ namespace RTC
/* Pure virtual methods inherited from RTC::NackGenerator. */
protected:
void OnNackGeneratorNackRequired(const std::vector<uint16_t>& seqNumbers) override;
void OnNackGeneratorFullFrameRequired() override;
void OnNackGeneratorKeyFrameRequired() override;
private:
// Passed by argument.
......
......@@ -176,7 +176,7 @@ namespace RTC
/* Pure virtual methods inherited from RTC::ConsumerListener. */
public:
void OnConsumerClosed(RTC::Consumer* consumer) override;
void OnConsumerFullFrameRequired(RTC::Consumer* consumer) override;
void OnConsumerKeyFrameRequired(RTC::Consumer* consumer) override;
/* Pure virtual methods inherited from Timer::Listener. */
public:
......
#define MS_CLASS "RTC::Codecs"
#define MS_LOG_DEV
// #define MS_LOG_DEV
#include "RTC/Codecs/Codecs.hpp"
#include "Logger.hpp"
......@@ -9,41 +9,21 @@ namespace RTC
{
namespace Codecs
{
bool IsKnown(const RTC::RtpCodecMimeType& mimeType)
void ProcessRtpPacket(RTC::RtpPacket* packet, const RTC::RtpCodecMimeType& mimeType)
{
MS_TRACE();
if (mimeType.type != RTC::RtpCodecMimeType::Type::VIDEO)
return false;
switch (mimeType.subtype)
{
case RTC::RtpCodecMimeType::Subtype::VP8:
return true;
default:
return false;
}
}
bool IsKeyFrame(const RTC::RtpCodecMimeType& mimeType, const RTC::RtpPacket* packet)
{
MS_TRACE();
if (!IsKnown(mimeType))
return false;
auto data = packet->GetPayload();
auto len = packet->GetPayloadLength();
{
VP8::ProcessRtpPacket(packet);
switch (mimeType.subtype)
{
case RTC::RtpCodecMimeType::Subtype::VP8:
return VP8::IsKeyFrame(data, len);
break;
}
default:
MS_DEBUG_TAG(
rtp, "requested key frame for an unsupported codec: %s", mimeType.GetName().c_str());
return false;
;
}
}
} // namespace Codecs
......
#define MS_CLASS "RTC::Codecs::VP8"
#define MS_LOG_DEV
// #define MS_LOG_DEV
#include "RTC/Codecs/VP8.hpp"
#include "Logger.hpp"
......@@ -9,10 +9,15 @@ namespace RTC
{
namespace Codecs
{
bool VP8::IsKeyFrame(const uint8_t* data, size_t len)
void VP8::ProcessRtpPacket(RTC::RtpPacket* packet)
{
MS_TRACE();
auto data = packet->GetPayload();
auto len = packet->GetPayloadLength();
if (len < 1)
return false;
return;
size_t offset = 0;
uint8_t byte = data[offset++];
......@@ -28,10 +33,8 @@ namespace RTC
if (x)
{
{
if (len < offset + 1)
return false;
}
if (len < offset + 1)
return;
byte = data[offset++];
......@@ -44,34 +47,29 @@ namespace RTC
if (i)
{
if (len < offset++)
{
return false;
}
return;
if ((byte >> 7) & 0x1)
{
if (len < offset++)
return false;
return;
}
}
if (l)
{
if (len < offset++)
{
return false;
}
return;
}
if (t || k)
{
if (len < offset++)
{
return false;
}
return;
}
return start && pid == 0 && (!(data[offset] & 0x1));
if (start && pid == 0 && (!(data[offset] & 0x1)))
packet->SetKeyFrame(true);
}
} // namespace Codecs
} // namespace RTC
......@@ -175,7 +175,7 @@ namespace RTC
MS_DEBUG_DEV("Consumer resumed [consumerId:%" PRIu32 "]", this->consumerId);
if (IsEnabled() && !this->sourcePaused)
RequestFullFrame();
RequestKeyFrame();
}
void Consumer::SourcePause()
......@@ -209,7 +209,7 @@ namespace RTC
this->notifier->Emit(this->consumerId, "sourceresumed");
if (IsEnabled() && !this->paused)
RequestFullFrame();
RequestKeyFrame();
}
void Consumer::SourceRtpParametersUpdated()
......@@ -337,8 +337,9 @@ namespace RTC
if (this->effectiveProfile != this->targetProfile && profile == this->targetProfile)
{
bool isKeyFrame = false;
bool canBeKeyFrame = Codecs::CanBeKeyFrame(this->rtpStream->GetMimeType());
if (Codecs::IsKnown(this->rtpStream->GetMimeType()) && Codecs::IsKeyFrame(this->rtpStream->GetMimeType(), packet))
if (canBeKeyFrame && packet->IsKeyFrame())
{
isKeyFrame = true;
......@@ -348,7 +349,7 @@ namespace RTC
RTC::RtpEncodingParameters::profile2String[profile].c_str());
}
if (!Codecs::IsKnown(this->rtpStream->GetMimeType()) || isKeyFrame)
if (isKeyFrame || !canBeKeyFrame)
{
SetEffectiveProfile(this->targetProfile);
......@@ -549,7 +550,7 @@ namespace RTC
this->rtpStream->ReceiveRtcpReceiverReport(report);
}
void Consumer::RequestFullFrame()
void Consumer::RequestKeyFrame()
{
MS_TRACE();
......@@ -561,7 +562,7 @@ namespace RTC
for (auto& listener : this->listeners)
{
listener->OnConsumerFullFrameRequired(this);
listener->OnConsumerKeyFrameRequired(this);
}
}
......@@ -727,7 +728,7 @@ namespace RTC
return;
if (IsEnabled() && !IsPaused())
RequestFullFrame();
RequestKeyFrame();
MS_DEBUG_TAG(
rtp,
......
......@@ -112,15 +112,15 @@ namespace RTC
this->nackList.erase(this->nackList.begin(), it);
// If the nack list is too large, clear it and request a full frame.
// If the nack list is too large, clear it and request a key frame.
uint32_t numNewNacks = seq32End - seq32Start;
if (this->nackList.size() + numNewNacks > MaxNackPackets)
{
MS_DEBUG_TAG(rtx, "NACK list too large, clearing it and requesting a full frame");
MS_DEBUG_TAG(rtx, "NACK list too large, clearing it and requesting a key frame");
this->nackList.clear();
this->listener->OnNackGeneratorFullFrameRequired();
this->listener->OnNackGeneratorKeyFrameRequired();
return;
}
......
......@@ -13,8 +13,7 @@ namespace RTC
{
/* Static. */
// TODO: Set a proper value.
static constexpr uint64_t FullFrameRequestBlockTimeout{ 1000 }; // In ms.
static constexpr uint64_t KeyFrameRequestBlockTimeout{ 1000 }; // In ms.
/* Instance methods. */
......@@ -43,8 +42,8 @@ namespace RTC
else
this->maxRtcpInterval = RTC::RTCP::MaxAudioIntervalMs;
// Set the RTP full frame request block timer.
this->fullFrameRequestBlockTimer = new Timer(this);
// Set the RTP key frame request block timer.
this->keyFrameRequestBlockTimer = new Timer(this);
}
Producer::~Producer()
......@@ -65,8 +64,8 @@ namespace RTC
listener->OnProducerClosed(this);
}
// Close the RTP full frame request block timer.
this->fullFrameRequestBlockTimer->Destroy();
// Close the RTP key frame request block timer.
this->keyFrameRequestBlockTimer->Destroy();
delete this;
}
......@@ -200,7 +199,7 @@ namespace RTC
listener->OnProducerResumed(this);
}
RequestFullFrame(true);
RequestKeyFrame(true);
}
void Producer::SetRtpRawEvent(bool enabled)
......@@ -221,9 +220,9 @@ namespace RTC
MS_DEBUG_DEV("Producer rtpraw event disabled [producerId:%" PRIu32 "]", this->producerId);
}
// If set (and not paused), require a full frame.
// If set (and not paused), require a key frame.
if (this->rtpRawEventEnabled && !this->paused)
RequestFullFrame(true);
RequestKeyFrame(true);
}
void Producer::SetRtpObjectEvent(bool enabled)
......@@ -244,9 +243,9 @@ namespace RTC
MS_DEBUG_DEV("Producer rtpobject event disabled [producerId:%" PRIu32 "]", this->producerId);
}
// If set (and not paused), require a full frame.
// If set (and not paused), require a key frame.
if (this->rtpObjectEventEnabled && !this->paused)
RequestFullFrame(true);
RequestKeyFrame(true);
}
void Producer::ReceiveRtpPacket(RTC::RtpPacket* packet)
......@@ -301,7 +300,10 @@ namespace RTC
return;
}
if (Codecs::IsKeyFrame(rtpStream->GetMimeType(), packet))
// Process the packet at codec level.
Codecs::ProcessRtpPacket(packet, rtpStream->GetMimeType());
if (packet->IsKeyFrame())
{
MS_DEBUG_TAG(
rtp,
......@@ -398,7 +400,7 @@ namespace RTC
this->transport->SendRtcpPacket(packet);
}
void Producer::RequestFullFrame(bool force)
void Producer::RequestKeyFrame(bool force)
{
MS_TRACE();
......@@ -408,30 +410,30 @@ namespace RTC
if (force)
{
// Stop the timer.
this->fullFrameRequestBlockTimer->Stop();
this->keyFrameRequestBlockTimer->Stop();
}
else if (this->fullFrameRequestBlockTimer->IsActive())
else if (this->keyFrameRequestBlockTimer->IsActive())
{
MS_DEBUG_TAG(rtcp, "blocking full frame request until timer expires");
MS_DEBUG_TAG(rtcp, "blocking key frame request until timer expires");
// Set flag.
this->isFullFrameRequested = true;
this->isKeyFrameRequested = true;
return;
}
// Run the timer.
this->fullFrameRequestBlockTimer->Start(FullFrameRequestBlockTimeout);
this->keyFrameRequestBlockTimer->Start(KeyFrameRequestBlockTimeout);
for (auto& kv : this->rtpStreams)
{
auto rtpStream = kv.second;
rtpStream->RequestFullFrame();
rtpStream->RequestKeyFrame();
}
// Reset flag.
this->isFullFrameRequested = false;
this->isKeyFrameRequested = false;
}
void Producer::FillHeaderExtensionIds()
......@@ -614,8 +616,8 @@ namespace RTC
}
}
// Request a full frame since we may have lost the first packets of this stream.
RequestFullFrame(true);
// Request a key frame since we may have lost the first packets of this stream.
RequestKeyFrame(true);
}
void Producer::ClearRtpStream(RTC::RtpStreamRecv* rtpStream)
......@@ -850,15 +852,15 @@ namespace RTC
{
MS_TRACE();
if (timer == this->fullFrameRequestBlockTimer)
if (timer == this->keyFrameRequestBlockTimer)
{
// Nobody asked for a full frame since the timer was started.
if (!this->isFullFrameRequested)
// Nobody asked for a key frame since the timer was started.
if (!this->isKeyFrameRequested)
return;
MS_DEBUG_TAG(rtcp, "requesting full frame after timer expires");
MS_DEBUG_TAG(rtcp, "requesting key frame after timer expires");
RequestFullFrame();
RequestKeyFrame();
}
}
} // namespace RTC
......@@ -1476,7 +1476,7 @@ namespace RTC
this->mapConsumerProducer.erase(consumer);
}
void Router::OnConsumerFullFrameRequired(RTC::Consumer* consumer)
void Router::OnConsumerKeyFrameRequired(RTC::Consumer* consumer)
{
MS_TRACE();
......@@ -1486,7 +1486,7 @@ namespace RTC
auto* producer = this->mapConsumerProducer[consumer];
producer->RequestFullFrame();
producer->RequestKeyFrame();
}
inline void Router::OnTimer(Timer* timer)
......
......@@ -291,9 +291,6 @@ namespace RTC
ptr += 4 + extensionValueSize;
}
// Parse and regenerate RFC 5285 extension header.
ParseExtensions();
// Add payload.
if (this->payload != nullptr)
{
......@@ -367,6 +364,9 @@ namespace RTC
// Clone seq32.
packet->seq32 = this->seq32;
// Copy isKeyFrame.
packet->isKeyFrame = this->isKeyFrame;
// Parse RFC 5285 extension header.
packet->ParseExtensions();
......
......@@ -213,7 +213,7 @@ namespace RTC
this->lastSrTimestamp += report->GetNtpFrac() >> 16;
}
void RtpStreamRecv::RequestFullFrame()
void RtpStreamRecv::RequestKeyFrame()
{
MS_TRACE();
......@@ -285,11 +285,11 @@ namespace RTC
this->listener->OnRtpStreamRecvNackRequired(this, seqNumbers);
}
void RtpStreamRecv::OnNackGeneratorFullFrameRequired()
void RtpStreamRecv::OnNackGeneratorKeyFrameRequired()
{
MS_TRACE();
RequestFullFrame();
RequestKeyFrame();
}
void RtpStreamRecv::SetRtx(uint8_t payloadType, uint32_t ssrc)
......
......@@ -40,7 +40,7 @@ namespace RTC
/* Static. */
static constexpr uint64_t EffectiveMaxBitrateCheckInterval{ 2000 }; // In ms.
static constexpr double EffectiveMaxBitrateThresholdBeforeFullFrame{ 0.6 }; // 0.0 - 1.0.
static constexpr double EffectiveMaxBitrateThresholdBeforeKeyFrame{ 0.6 }; // 0.0 - 1.0.
/* Instance methods. */
......@@ -461,16 +461,16 @@ namespace RTC
// Add us as listener.
consumer->AddListener(this);
// If we are connected, ask a fullrequest for this enabled Consumer.
// If we are connected, ask a key request for this enabled Consumer.
if (IsConnected())
{
if (consumer->kind == RTC::Media::Kind::VIDEO)
{
MS_DEBUG_TAG(
rtcp, "requesting full frame for new Consumer since Transport already connected");
rtcp, "requesting key frame for new Consumer since Transport already connected");
}
consumer->RequestFullFrame();
consumer->RequestKeyFrame();
}
}
......@@ -736,7 +736,7 @@ namespace RTC
break;
}
consumer->RequestFullFrame();
consumer->RequestKeyFrame();
break;
}
......@@ -1439,13 +1439,13 @@ namespace RTC
eventData[JsonStringDtlsRemoteCert] = remoteCert;
this->notifier->Emit(this->transportId, "dtlsstatechange", eventData);
// Iterate all the Consumers and request full frame.
// Iterate all the Consumers and request key frame.
for (auto* consumer : this->consumers)
{
if (consumer->kind == RTC::Media::Kind::VIDEO)
MS_DEBUG_TAG(rtcp, "Transport connected, requesting full frame for Consumers");
MS_DEBUG_TAG(rtcp, "Transport connected, requesting key frame for Consumers");
consumer->RequestFullFrame();
consumer->RequestKeyFrame();
}
}
......@@ -1550,21 +1550,21 @@ namespace RTC
packet.Serialize(RTC::RTCP::Buffer);
this->SendRtcpPacket(&packet);
// Trigger a full frame for all the suitable strams if the effective max bitrate
// Trigger a key frame for all the suitable streams if the effective max bitrate
// has decreased abruptly.
if (now - this->lastEffectiveMaxBitrateAt > EffectiveMaxBitrateCheckInterval)
{
if (
(bitrate != 0u) && (this->effectiveMaxBitrate != 0u) &&
static_cast<double>(effectiveBitrate) / static_cast<double>(this->effectiveMaxBitrate) <
EffectiveMaxBitrateThresholdBeforeFullFrame)
EffectiveMaxBitrateThresholdBeforeKeyFrame)
{
MS_WARN_TAG(rbe, "uplink effective max bitrate abruptly decrease, requesting full frames");
MS_WARN_TAG(rbe, "uplink effective max bitrate abruptly decrease, requesting key frames");
// Request full frame for all the Producers.
// Request key frame for all the Producers.
for (auto* producer : this->producers)
{
producer->RequestFullFrame();
producer->RequestKeyFrame();
}
}
......@@ -1631,7 +1631,7 @@ namespace RTC
this->consumers.erase(consumer);
}
void Transport::OnConsumerFullFrameRequired(RTC::Consumer* /*consumer*/)
void Transport::OnConsumerKeyFrameRequired(RTC::Consumer* /*consumer*/)
{
// Do nothing.
}
......
Markdown 格式
0% 或
您添加了 0 人 到此讨论。请谨慎行事。
请先完成此评论的编辑!
请 注册 或者 后发表评论