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

WIP: BWE

上级 0b322bf5
......@@ -50,6 +50,7 @@ namespace RTC
RTC::RtpParameters::Type GetType() const;
const std::vector<uint32_t>& GetMediaSsrcs() const;
virtual bool IsActive() const;
virtual void SetBitrateExternallyManaged();
void TransportConnected();
void TransportDisconnected();
bool IsPaused() const;
......@@ -61,6 +62,7 @@ namespace RTC
virtual void ProducerRtpStreamScore(
RTC::RtpStream* rtpStream, uint8_t score, uint8_t previousScore) = 0;
void ProducerClosed();
virtual uint32_t UseBitrate(uint32_t bitrate);
virtual void SendRtpPacket(RTC::RtpPacket* packet) = 0;
virtual void GetRtcp(RTC::RTCP::CompoundPacket* packet, uint64_t now) = 0;
virtual void NeedWorstRemoteFractionLost(uint32_t mappedSsrc, uint8_t& worstRemoteFractionLost) = 0;
......@@ -137,6 +139,11 @@ namespace RTC
this->transportConnected && !this->paused && !this->producerPaused && !this->producerClosed);
}
inline void Consumer::SetBitrateExternallyManaged()
{
// Do nothing.
}
inline bool Consumer::IsPaused() const
{
return this->paused;
......
......@@ -20,9 +20,11 @@ namespace RTC
void FillJsonScore(json& jsonObject) const override;
void HandleRequest(Channel::Request* request) override;
bool IsActive() const override;
void SetBitrateExternallyManaged() override;
void ProducerRtpStream(RTC::RtpStream* rtpStream, uint32_t mappedSsrc) override;
void ProducerNewRtpStream(RTC::RtpStream* rtpStream, uint32_t mappedSsrc) override;
void ProducerRtpStreamScore(RTC::RtpStream* rtpStream, uint8_t score, uint8_t previousScore) override;
virtual uint32_t UseBitrate(uint32_t bitrate) override;
void SendRtpPacket(RTC::RtpPacket* packet) override;
void GetRtcp(RTC::RTCP::CompoundPacket* packet, uint64_t now) override;
void NeedWorstRemoteFractionLost(uint32_t mappedSsrc, uint8_t& worstRemoteFractionLost) override;
......@@ -40,9 +42,11 @@ namespace RTC
void CreateRtpStream();
void RequestKeyFrame();
void RetransmitRtpPacket(RTC::RtpPacket* packet);
void EmitScore() const;
void UpdateCurrentLayers();
void RecalculateTargetSpatialLayer();
void RecalculateTargetLayers();
void RecalculateAndApplyTargetLayers();
void EmitScore() const;
void EmitLayersChange() const;
RTC::RtpStream* GetProducerCurrentRtpStream() const;
RTC::RtpStream* GetProducerTargetRtpStream() const;
......@@ -61,13 +65,14 @@ namespace RTC
bool syncRequired{ false };
RTC::SeqManager<uint16_t> rtpSeqManager;
RTC::SeqManager<uint32_t> rtpTimestampManager;
std::unique_ptr<RTC::Codecs::EncodingContext> encodingContext;
int16_t preferredSpatialLayer{ -1 };
int16_t preferredTemporalLayer{ -1 };
int16_t targetSpatialLayer{ -1 };
int16_t targetTemporalLayer{ -1 };
int16_t currentSpatialLayer{ -1 };
int16_t currentTemporalLayer{ -1 };
std::unique_ptr<RTC::Codecs::EncodingContext> encodingContext;
bool bitrateExternallyManaged{ false };
};
/* Inline methods. */
......@@ -85,6 +90,11 @@ namespace RTC
);
// clang-format on
}
inline void SimulcastConsumer::SetBitrateExternallyManaged()
{
this->bitrateExternallyManaged = false;
}
} // namespace RTC
#endif
......@@ -25,7 +25,7 @@ namespace Channel
MS_ASSERT(Notifier::channel != nullptr, "channel unset");
json jsonNotification(json::object());
json jsonNotification = json::object();
jsonNotification["targetId"] = targetId;
jsonNotification["event"] = event;
......@@ -39,7 +39,7 @@ namespace Channel
MS_ASSERT(Notifier::channel != nullptr, "channel unset");
json jsonNotification(json::object());
json jsonNotification = json::object();
jsonNotification["targetId"] = targetId;
jsonNotification["event"] = event;
......
......@@ -108,7 +108,7 @@ namespace Channel
this->replied = true;
json jsonResponse(json::object());
json jsonResponse = json::object();
jsonResponse["id"] = this->id;
jsonResponse["accepted"] = true;
......@@ -124,7 +124,7 @@ namespace Channel
this->replied = true;
json jsonResponse(json::object());
json jsonResponse = json::object();
jsonResponse["id"] = this->id;
jsonResponse["accepted"] = true;
......@@ -143,7 +143,7 @@ namespace Channel
this->replied = true;
json jsonResponse(json::object());
json jsonResponse = json::object();
jsonResponse["id"] = this->id;
jsonResponse["error"] = "Error";
......@@ -162,7 +162,7 @@ namespace Channel
this->replied = true;
json jsonResponse(json::object());
json jsonResponse = json::object();
jsonResponse["id"] = this->id;
jsonResponse["error"] = "TypeError";
......
......@@ -183,7 +183,7 @@ namespace RTC
{
case Channel::Request::MethodId::CONSUMER_DUMP:
{
json data(json::object());
json data = json::object();
FillJson(data);
......@@ -194,7 +194,7 @@ namespace RTC
case Channel::Request::MethodId::CONSUMER_GET_STATS:
{
json data(json::array());
json data = json::array();
FillJsonStats(data);
......@@ -326,4 +326,13 @@ namespace RTC
this->listener->onConsumerProducerClosed(this);
}
uint32_t Consumer::UseBitrate(uint32_t /*bitrate*/)
{
MS_TRACE();
// This method must be override by subclasses with layers. By default
// it just returns 0.
return 0;
}
} // namespace RTC
......@@ -226,7 +226,7 @@ namespace RTC
}
// Tell the caller about the selected local DTLS role.
json data(json::object());
json data = json::object();
this->tuple->FillJson(data["tuple"]);
......
......@@ -400,7 +400,7 @@ namespace RTC
}
// Tell the caller about the selected local DTLS role.
json data(json::object());
json data = json::object();
this->tuple->FillJson(data["tuple"]);
......
......@@ -312,7 +312,7 @@ namespace RTC
{
case Channel::Request::MethodId::PRODUCER_DUMP:
{
json data(json::object());
json data = json::object();
FillJson(data);
......@@ -323,7 +323,7 @@ namespace RTC
case Channel::Request::MethodId::PRODUCER_GET_STATS:
{
json data(json::array());
json data = json::array();
FillJsonStats(data);
......
......@@ -37,9 +37,6 @@ namespace RTC
{
MS_TRACE();
// TODO: REMOVE
MS_ERROR("remb:%" PRIu64, remb->GetBitrate());
uint64_t now = DepLibUV::GetTime();
// If we don't have recent data yet, start from here.
......
......@@ -134,7 +134,7 @@ namespace RTC
{
case Channel::Request::MethodId::ROUTER_DUMP:
{
json data(json::object());
json data = json::object();
FillJson(data);
......@@ -158,7 +158,7 @@ namespace RTC
MS_DEBUG_DEV("WebRtcTransport created [transportId:%s]", transportId.c_str());
json data(json::object());
json data = json::object();
webRtcTransport->FillJson(data);
......@@ -181,7 +181,7 @@ namespace RTC
MS_DEBUG_DEV("PlainRtpTransport created [transportId:%s]", transportId.c_str());
json data(json::object());
json data = json::object();
plainRtpTransport->FillJson(data);
......@@ -204,7 +204,7 @@ namespace RTC
MS_DEBUG_DEV("PipeTransport created [transportId:%s]", transportId.c_str());
json data(json::object());
json data = json::object();
pipeTransport->FillJson(data);
......
......@@ -2,6 +2,7 @@
// #define MS_LOG_DEV
#include "RTC/SimpleConsumer.hpp"
#include "DepLibUV.hpp"
#include "Logger.hpp"
#include "MediaSoupErrors.hpp"
#include "Channel/Notifier.hpp"
......
......@@ -2,6 +2,7 @@
// #define MS_LOG_DEV
#include "RTC/SimulcastConsumer.hpp"
#include "DepLibUV.hpp"
#include "Logger.hpp"
#include "MediaSoupErrors.hpp"
#include "Channel/Notifier.hpp"
......@@ -221,14 +222,20 @@ namespace RTC
request->Accept();
// clang-format off
if (
IsActive() && (this->preferredSpatialLayer != previousPreferredSpatialLayer ||
this->preferredTemporalLayer != previousPreferredTemporalLayer))
IsActive() &&
(
this->preferredSpatialLayer != previousPreferredSpatialLayer ||
this->preferredTemporalLayer != previousPreferredTemporalLayer
)
)
// clang-format on
{
this->listener->OnConsumerNeedBitrateChange(this);
// TODO: No.
RecalculateTargetSpatialLayer();
if (this->bitrateExternallyManaged)
this->listener->OnConsumerNeedBitrateChange(this);
else
RecalculateAndApplyTargetLayers();
}
break;
......@@ -269,12 +276,13 @@ namespace RTC
if (IsActive())
{
this->listener->OnConsumerNeedBitrateChange(this);
// TODO: No.
RecalculateTargetSpatialLayer();
if (this->bitrateExternallyManaged)
this->listener->OnConsumerNeedBitrateChange(this);
else
RecalculateAndApplyTargetLayers();
}
// TODO: Should just do it if the new stream has been selected.
// Emit the score event.
EmitScore();
}
......@@ -292,14 +300,27 @@ namespace RTC
// redistribution.
}
// TODO: NO, we just can downgrade layers.
if (IsActive())
RecalculateTargetSpatialLayer();
{
if (this->bitrateExternallyManaged)
this->listener->OnConsumerNeedBitrateChange(this);
else
RecalculateAndApplyTargetLayers();
}
// TODO: Should just do it if the stream is the selected one.
// Emit the score event.
EmitScore();
}
uint32_t SimulcastConsumer::UseBitrate(uint32_t bitrate)
{
MS_TRACE();
// TODO.
return bitrate;
}
void SimulcastConsumer::SendRtpPacket(RTC::RtpPacket* packet)
{
MS_TRACE();
......@@ -329,7 +350,20 @@ namespace RTC
if (this->keyFrameSupported && !packet->IsKeyFrame())
return;
// Change current spatial layer.
// If the new current spatial layer is the preferred one honor the preferred
// temporal layer.
if (this->targetSpatialLayer == this->preferredSpatialLayer)
{
this->targetTemporalLayer = this->preferredTemporalLayer;
}
// Otherwise, choose the highest temporal layer.
// TODO: No, target temporal layer could be less (as per BWE).
else
{
this->targetTemporalLayer = this->rtpStream->GetTemporalLayers() - 1;
}
// Update current spatial and temporal layers.
UpdateCurrentLayers();
// Need to resync the stream.
......@@ -383,7 +417,7 @@ namespace RTC
return;
}
// Update temporal layer only if we are sending the target spatial layer.
// Update current temporal layer if the packet honors the target temporal layer.
if (
this->currentTemporalLayer != this->targetTemporalLayer &&
packet->GetTemporalLayer() == this->targetTemporalLayer)
......@@ -552,10 +586,11 @@ namespace RTC
if (IsActive())
{
this->rtpStream->Resume();
this->listener->OnConsumerNeedBitrateChange(this);
// TODO: No.
RecalculateTargetSpatialLayer();
if (this->bitrateExternallyManaged)
this->listener->OnConsumerNeedBitrateChange(this);
else
RecalculateAndApplyTargetLayers();
}
}
......@@ -576,7 +611,7 @@ namespace RTC
this->encodingContext->preferences.temporalLayer = this->rtpStream->GetTemporalLayers() - 1;
}
// TODO: Emit JS event?
EmitLayersChange();
}
void SimulcastConsumer::UserOnPaused()
......@@ -596,7 +631,7 @@ namespace RTC
this->encodingContext->preferences.temporalLayer = this->rtpStream->GetTemporalLayers() - 1;
}
// TODO: Emit JS event?
EmitLayersChange();
}
void SimulcastConsumer::UserOnResumed()
......@@ -608,10 +643,11 @@ namespace RTC
if (IsActive())
{
this->rtpStream->Resume();
this->listener->OnConsumerNeedBitrateChange(this);
// TODO: No.
RecalculateTargetSpatialLayer();
if (this->bitrateExternallyManaged)
this->listener->OnConsumerNeedBitrateChange(this);
else
RecalculateAndApplyTargetLayers();
}
}
......@@ -726,17 +762,6 @@ namespace RTC
}
}
inline void SimulcastConsumer::EmitScore() const
{
MS_TRACE();
json data = json::object();
FillJsonScore(data);
Channel::Notifier::Emit(this->id, "score", data);
}
void SimulcastConsumer::UpdateCurrentLayers()
{
MS_TRACE();
......@@ -746,20 +771,14 @@ namespace RTC
return;
}
bool emitScore{ false };
// Reset the score of our RtpStream to 10 if spatial layer changed.
if (this->targetSpatialLayer != this->currentSpatialLayer)
{
this->rtpStream->ResetScore(10, false);
// If the new current spatial layer is the preferred one honor the preferred
// temporal layer.
if (this->targetSpatialLayer == this->preferredSpatialLayer)
{
this->targetTemporalLayer = this->preferredTemporalLayer;
}
// Otherwise, choose the highest temporal layer.
else
{
this->targetTemporalLayer = this->rtpStream->GetTemporalLayers() - 1;
emitScore = true;
}
this->currentSpatialLayer = this->targetSpatialLayer;
......@@ -774,18 +793,15 @@ namespace RTC
this->currentTemporalLayer,
this->id.c_str());
json data(json::object());
data["spatialLayer"] = this->currentSpatialLayer;
data["temporalLayer"] = this->currentTemporalLayer;
// Emit the layersChange event.
EmitLayersChange();
Channel::Notifier::Emit(this->id, "layerschange", data);
// Emit the score event.
EmitScore();
// Emit the score event (just if spatial layer changed).
if (emitScore)
EmitScore();
}
void SimulcastConsumer::RecalculateTargetSpatialLayer()
void SimulcastConsumer::RecalculateAndApplyTargetLayers()
{
MS_TRACE();
......@@ -836,7 +852,7 @@ namespace RTC
this->encodingContext->preferences.temporalLayer = this->rtpStream->GetTemporalLayers() - 1;
}
// TODO: Emit JS event?
EmitLayersChange();
return;
}
......@@ -860,6 +876,29 @@ namespace RTC
RequestKeyFrame();
}
inline void SimulcastConsumer::EmitScore() const
{
MS_TRACE();
json data = json::object();
FillJsonScore(data);
Channel::Notifier::Emit(this->id, "score", data);
}
inline void SimulcastConsumer::EmitLayersChange() const
{
MS_TRACE();
json data = json::object();
data["spatialLayer"] = this->currentSpatialLayer;
data["temporalLayer"] = this->currentTemporalLayer;
Channel::Notifier::Emit(this->id, "layerschange", data);
}
inline RTC::RtpStream* SimulcastConsumer::GetProducerCurrentRtpStream() const
{
MS_TRACE();
......@@ -887,9 +926,13 @@ namespace RTC
{
MS_TRACE();
// TODO: NO.
if (IsActive())
RecalculateTargetSpatialLayer();
{
if (this->bitrateExternallyManaged)
this->listener->OnConsumerNeedBitrateChange(this);
else
RecalculateAndApplyTargetLayers();
}
// Emit the score event.
EmitScore();
......
......@@ -144,7 +144,7 @@ namespace RTC
{
case Channel::Request::MethodId::TRANSPORT_DUMP:
{
json data(json::object());
json data = json::object();
FillJson(data);
......@@ -155,7 +155,7 @@ namespace RTC
case Channel::Request::MethodId::TRANSPORT_GET_STATS:
{
json data(json::array());
json data = json::array();
FillJsonStats(data);
......@@ -224,7 +224,7 @@ namespace RTC
this->rtpHeaderExtensionIds.absSendTime = producerRtpHeaderExtensionIds.absSendTime;
// Create status response.
json data(json::object());
json data = json::object();
data["type"] = RTC::RtpParameters::GetTypeString(producer->GetType());
......@@ -326,7 +326,7 @@ namespace RTC
"Consumer created [consumerId:%s, producerId:%s]", consumerId.c_str(), producerId.c_str());
// Create status response.
json data(json::object());
json data = json::object();
data["paused"] = consumer->IsPaused();
data["producerPaused"] = consumer->IsProducerPaused();
......
......@@ -590,7 +590,7 @@ namespace RTC
}
// Tell the caller about the selected local DTLS role.
json data(json::object());
json data = json::object();
switch (this->dtlsRole)
{
......@@ -645,7 +645,7 @@ namespace RTC
MS_DEBUG_DEV("WebRtcTransport ICE usernameFragment and password changed [id:%s]", this->id);
// Reply with the updated ICE local parameters.
json data(json::object());
json data = json::object();
data["iceParameters"] = json::object();
auto jsonIceParametersIt = data.find("iceParameters");
......@@ -1233,7 +1233,7 @@ namespace RTC
MS_DEBUG_TAG(ice, "ICE elected tuple");
// Notify the Node WebRtcTransport.
json data(json::object());
json data = json::object();
this->iceSelectedTuple->FillJson(data["iceSelectedTuple"]);
......@@ -1247,7 +1247,7 @@ namespace RTC
MS_DEBUG_TAG(ice, "ICE connected");
// Notify the Node WebRtcTransport.
json data(json::object());
json data = json::object();
data["iceState"] = "connected";
......@@ -1264,7 +1264,7 @@ namespace RTC
MS_DEBUG_TAG(ice, "ICE completed");
// Notify the Node WebRtcTransport.
json data(json::object());
json data = json::object();
data["iceState"] = "completed";
......@@ -1284,7 +1284,7 @@ namespace RTC
MS_DEBUG_TAG(ice, "ICE disconnected");
// Notify the Node WebRtcTransport.
json data(json::object());
json data = json::object();
data["iceState"] = "disconnected";
......@@ -1301,7 +1301,7 @@ namespace RTC
MS_DEBUG_TAG(dtls, "DTLS connecting");
// Notify the Node WebRtcTransport.
json data(json::object());
json data = json::object();
data["dtlsState"] = "connecting";
......@@ -1357,7 +1357,7 @@ namespace RTC
}
// Notify the Node WebRtcTransport.
json data(json::object());
json data = json::object();
data["dtlsState"] = "connected";
data["dtlsRemoteCert"] = remoteCert;
......@@ -1375,7 +1375,7 @@ namespace RTC
MS_WARN_TAG(dtls, "DTLS failed");
// Notify the Node WebRtcTransport.
json data(json::object());
json data = json::object();
data["dtlsState"] = "failed";
......@@ -1389,7 +1389,7 @@ namespace RTC
MS_WARN_TAG(dtls, "DTLS remotely closed");
// Notify the Node WebRtcTransport.
json data(json::object());
json data = json::object();
data["dtlsState"] = "closed";
......
......@@ -129,7 +129,7 @@ inline void Worker::OnChannelRequest(Channel::UnixStreamSocket* /*channel*/, Cha
{
case Channel::Request::MethodId::WORKER_DUMP:
{
json data(json::object());
json data = json::object();
FillJson(data);
......
Markdown 格式
0% 或
您添加了 0 人 到此讨论。请谨慎行事。
请先完成此评论的编辑!
请 注册 或者 后发表评论