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

Transport: trigger PLI if uplink bitrate abruptly decreases

上级 922152e7
...@@ -16,6 +16,12 @@ ...@@ -16,6 +16,12 @@
* Logs the current method/function if MS_LOG_TRACE macro is defined and the * Logs the current method/function if MS_LOG_TRACE macro is defined and the
* current debug level is "debug". * current debug level is "debug".
* *
* MS_HAS_DEBUG_TAG(tag)
* MS_HAS_WARN_TAG(tag)
*
* True if the current debug level is satisfied and the given tag is enabled
* (or if the current source file defines the MS_LOG_DEV macro).
*
* MS_DEBUG_TAG(tag, ...) * MS_DEBUG_TAG(tag, ...)
* MS_WARN_TAG(tag, ...) * MS_WARN_TAG(tag, ...)
* *
...@@ -152,6 +158,12 @@ public: ...@@ -152,6 +158,12 @@ public:
#define MS_TRACE_STD() ; #define MS_TRACE_STD() ;
#endif #endif
#define MS_HAS_DEBUG_TAG(tag) \
(LogLevel::LOG_DEBUG == Settings::configuration.logLevel && (_MS_TAG_ENABLED(tag) || _MS_LOG_DEV_ENABLED))
#define MS_HAS_WARN_TAG(tag) \
(LogLevel::LOG_WARN <= Settings::configuration.logLevel && (_MS_TAG_ENABLED(tag) || _MS_LOG_DEV_ENABLED))
#define MS_DEBUG_TAG(tag, desc, ...) \ #define MS_DEBUG_TAG(tag, desc, ...) \
do \ do \
{ \ { \
......
...@@ -79,6 +79,7 @@ namespace RTC ...@@ -79,6 +79,7 @@ namespace RTC
virtual void onTransportConnected(RTC::Transport* transport) override; virtual void onTransportConnected(RTC::Transport* transport) override;
virtual void onTransportClosed(RTC::Transport* transport) override; virtual void onTransportClosed(RTC::Transport* transport) override;
virtual void onTransportRtcpPacket(RTC::Transport* transport, RTC::RTCP::Packet* packet) override; virtual void onTransportRtcpPacket(RTC::Transport* transport, RTC::RTCP::Packet* packet) override;
virtual void onTransportFullFrameRequired(RTC::Transport* transport) override;
/* Pure virtual methods inherited from RTC::RtpReceiver::Listener. */ /* Pure virtual methods inherited from RTC::RtpReceiver::Listener. */
public: public:
......
...@@ -52,15 +52,6 @@ namespace RTC ...@@ -52,15 +52,6 @@ namespace RTC
uint32_t jitter = 0; // Estimated jitter. uint32_t jitter = 0; // Estimated jitter.
std::unique_ptr<RTC::NackGenerator> nackGenerator; std::unique_ptr<RTC::NackGenerator> nackGenerator;
}; };
/* Inline instance methods. */
inline
void RtpStreamRecv::RequestFullFrame()
{
if (this->params.usePli)
this->listener->onPliRequired(this);
}
} }
#endif #endif
...@@ -40,6 +40,7 @@ namespace RTC ...@@ -40,6 +40,7 @@ namespace RTC
virtual void onTransportConnected(RTC::Transport* transport) = 0; virtual void onTransportConnected(RTC::Transport* transport) = 0;
virtual void onTransportClosed(RTC::Transport* transport) = 0; virtual void onTransportClosed(RTC::Transport* transport) = 0;
virtual void onTransportRtcpPacket(RTC::Transport* transport, RTC::RTCP::Packet* packet) = 0; virtual void onTransportRtcpPacket(RTC::Transport* transport, RTC::RTCP::Packet* packet) = 0;
virtual void onTransportFullFrameRequired(RTC::Transport* transport) = 0;
}; };
private: private:
...@@ -136,6 +137,8 @@ namespace RTC ...@@ -136,6 +137,8 @@ namespace RTC
// REMB and bitrate stuff. // REMB and bitrate stuff.
std::unique_ptr<RemoteBitrateEstimatorAbsSendTime> remoteBitrateEstimator; std::unique_ptr<RemoteBitrateEstimatorAbsSendTime> remoteBitrateEstimator;
uint32_t maxBitrate = 0; uint32_t maxBitrate = 0;
uint32_t effectiveMaxBitrate = 0;
uint64_t lastEffectiveMaxBitrateAt = 0;
}; };
/* Inline instance methods. */ /* Inline instance methods. */
......
...@@ -761,6 +761,30 @@ namespace RTC ...@@ -761,6 +761,30 @@ namespace RTC
this->transports.erase(transport->transportId); this->transports.erase(transport->transportId);
} }
void Peer::onTransportFullFrameRequired(RTC::Transport* transport)
{
MS_TRACE();
// If the transport is used by any RtpReceiver (video/depth) notify the
// listener.
for (auto& kv : this->rtpReceivers)
{
RTC::RtpReceiver* rtpReceiver = kv.second;
if (
rtpReceiver->kind != RTC::Media::Kind::VIDEO &&
rtpReceiver->kind != RTC::Media::Kind::DEPTH)
{
continue;
}
if (rtpReceiver->GetTransport() != transport)
continue;
rtpReceiver->RequestFullFrame();
}
}
void Peer::onRtpReceiverParameters(RTC::RtpReceiver* rtpReceiver) void Peer::onRtpReceiverParameters(RTC::RtpReceiver* rtpReceiver)
{ {
MS_TRACE(); MS_TRACE();
......
...@@ -41,10 +41,10 @@ namespace RTC ...@@ -41,10 +41,10 @@ namespace RTC
MS_TRACE(); MS_TRACE();
const int64_t bitrateReductionInterval = std::max<int64_t>(std::min<int64_t>(this->rtt, 200), 10); const int64_t bitrateReductionInterval = std::max<int64_t>(std::min<int64_t>(this->rtt, 200), 10);
if (timeNow - this->timeLastBitrateChange >= bitrateReductionInterval) if (timeNow - this->timeLastBitrateChange >= bitrateReductionInterval)
{
return true; return true;
}
if (ValidEstimate()) if (ValidEstimate())
{ {
// TODO(terelius/holmer): Investigate consequences of increasing // TODO(terelius/holmer): Investigate consequences of increasing
...@@ -108,10 +108,10 @@ namespace RTC ...@@ -108,10 +108,10 @@ namespace RTC
double avgPacketSizeBits = bitsPerFrame / packetsPerFrame; double avgPacketSizeBits = bitsPerFrame / packetsPerFrame;
// Approximate the over-use estimator delay to 100 ms. // Approximate the over-use estimator delay to 100 ms.
const int64_t responseTime = (this->rtt + 100) * 2; const int64_t responseTime = (this->rtt + 100) * 2;
constexpr double kMinIncreaseRateBps = 4000; constexpr double kMinIncreaseRateBps = 4000;
return static_cast<int>(std::max(kMinIncreaseRateBps, (avgPacketSizeBits * 1000) / responseTime)); return static_cast<int>(std::max(kMinIncreaseRateBps,
(avgPacketSizeBits * 1000) / responseTime));
} }
uint32_t AimdRateControl::ChangeBitrate(uint32_t newBitrateBps, uint32_t incomingBitrateBps, int64_t nowMs) uint32_t AimdRateControl::ChangeBitrate(uint32_t newBitrateBps, uint32_t incomingBitrateBps, int64_t nowMs)
...@@ -119,9 +119,7 @@ namespace RTC ...@@ -119,9 +119,7 @@ namespace RTC
MS_TRACE(); MS_TRACE();
if (!this->updated) if (!this->updated)
{
return this->currentBitrateBps; return this->currentBitrateBps;
}
// An over-use should always trigger us to reduce the bitrate, even though // An over-use should always trigger us to reduce the bitrate, even though
// we have not yet established our first estimate. By acting on the over-use, // we have not yet established our first estimate. By acting on the over-use,
...@@ -152,11 +150,13 @@ namespace RTC ...@@ -152,11 +150,13 @@ namespace RTC
if (this->rateControlRegion == kRcNearMax) if (this->rateControlRegion == kRcNearMax)
{ {
uint32_t additiveIncreaseBps = AdditiveRateIncrease(nowMs, this->timeLastBitrateChange); uint32_t additiveIncreaseBps = AdditiveRateIncrease(nowMs, this->timeLastBitrateChange);
newBitrateBps += additiveIncreaseBps; newBitrateBps += additiveIncreaseBps;
} }
else else
{ {
uint32_t multiplicativeIncreaseBps = MultiplicativeRateIncrease(nowMs, this->timeLastBitrateChange, newBitrateBps); uint32_t multiplicativeIncreaseBps = MultiplicativeRateIncrease(nowMs, this->timeLastBitrateChange, newBitrateBps);
newBitrateBps += multiplicativeIncreaseBps; newBitrateBps += multiplicativeIncreaseBps;
} }
...@@ -168,6 +168,7 @@ namespace RTC ...@@ -168,6 +168,7 @@ namespace RTC
// Set bit rate to something slightly lower than max // Set bit rate to something slightly lower than max
// to get rid of any self-induced delay. // to get rid of any self-induced delay.
newBitrateBps = static_cast<uint32_t>(this->beta * incomingBitrateBps + 0.5); newBitrateBps = static_cast<uint32_t>(this->beta * incomingBitrateBps + 0.5);
if (newBitrateBps > this->currentBitrateBps) if (newBitrateBps > this->currentBitrateBps)
{ {
// Avoid increasing the rate when over-using. // Avoid increasing the rate when over-using.
...@@ -177,12 +178,14 @@ namespace RTC ...@@ -177,12 +178,14 @@ namespace RTC
} }
newBitrateBps = std::min(newBitrateBps, this->currentBitrateBps); newBitrateBps = std::min(newBitrateBps, this->currentBitrateBps);
} }
ChangeRegion(kRcNearMax); ChangeRegion(kRcNearMax);
if (incomingBitrateBps < this->currentBitrateBps) if (incomingBitrateBps < this->currentBitrateBps)
{ {
this->lastDecrease = int(this->currentBitrateBps - newBitrateBps); this->lastDecrease = int(this->currentBitrateBps - newBitrateBps);
} }
if (incomingBitrateKbps < this->avgMaxBitrateKbps - 3 * stdMaxBitRate) if (incomingBitrateKbps < this->avgMaxBitrateKbps - 3 * stdMaxBitRate)
{ {
this->avgMaxBitrateKbps = -1.0f; this->avgMaxBitrateKbps = -1.0f;
...@@ -228,6 +231,7 @@ namespace RTC ...@@ -228,6 +231,7 @@ namespace RTC
if (lastMs > -1) if (lastMs > -1)
{ {
int timeSinceLastUpdateMs = std::min(static_cast<int>(nowMs - lastMs), 1000); int timeSinceLastUpdateMs = std::min(static_cast<int>(nowMs - lastMs), 1000);
alpha = pow(alpha, timeSinceLastUpdateMs / 1000.0); alpha = pow(alpha, timeSinceLastUpdateMs / 1000.0);
} }
...@@ -257,6 +261,7 @@ namespace RTC ...@@ -257,6 +261,7 @@ namespace RTC
this->varMaxBitrateKbps = (1 - alpha) * this->varMaxBitrateKbps + this->varMaxBitrateKbps = (1 - alpha) * this->varMaxBitrateKbps +
alpha * (this->avgMaxBitrateKbps - incomingBitrateKbps) * (this->avgMaxBitrateKbps - incomingBitrateKbps) / norm; alpha * (this->avgMaxBitrateKbps - incomingBitrateKbps) * (this->avgMaxBitrateKbps - incomingBitrateKbps) / norm;
// 0.4 ~= 14 kbit/s at 500 kbit/s // 0.4 ~= 14 kbit/s at 500 kbit/s
if (this->varMaxBitrateKbps < 0.4f) if (this->varMaxBitrateKbps < 0.4f)
{ {
......
...@@ -49,9 +49,11 @@ namespace RTC ...@@ -49,9 +49,11 @@ namespace RTC
{ {
*timestamp_delta = this->currentTimestampGroup.timestamp - this->prevTimestampGroup.timestamp; *timestamp_delta = this->currentTimestampGroup.timestamp - this->prevTimestampGroup.timestamp;
*arrival_time_delta_ms = this->currentTimestampGroup.complete_time_ms - this->prevTimestampGroup.complete_time_ms; *arrival_time_delta_ms = this->currentTimestampGroup.complete_time_ms - this->prevTimestampGroup.complete_time_ms;
// Check system time differences to see if we have an unproportional jump // Check system time differences to see if we have an unproportional jump
// in arrival time. In that case reset the inter-arrival computations. // in arrival time. In that case reset the inter-arrival computations.
int64_t system_time_delta_ms = this->currentTimestampGroup.last_system_time_ms - this->prevTimestampGroup.last_system_time_ms; int64_t system_time_delta_ms = this->currentTimestampGroup.last_system_time_ms - this->prevTimestampGroup.last_system_time_ms;
if (*arrival_time_delta_ms - system_time_delta_ms >= kArrivalTimeOffsetThresholdMs) if (*arrival_time_delta_ms - system_time_delta_ms >= kArrivalTimeOffsetThresholdMs)
{ {
MS_WARN_TAG(rbe, "the arrival time clock offset has changed, resetting [diff:%" PRId64 "ms]", MS_WARN_TAG(rbe, "the arrival time clock offset has changed, resetting [diff:%" PRId64 "ms]",
...@@ -60,6 +62,7 @@ namespace RTC ...@@ -60,6 +62,7 @@ namespace RTC
Reset(); Reset();
return false; return false;
} }
if (*arrival_time_delta_ms < 0) if (*arrival_time_delta_ms < 0)
{ {
// The group of packets has been reordered since receiving its local // The group of packets has been reordered since receiving its local
...@@ -72,6 +75,7 @@ namespace RTC ...@@ -72,6 +75,7 @@ namespace RTC
"packet for bandwidth estimation, resetting"); "packet for bandwidth estimation, resetting");
Reset(); Reset();
} }
return false; return false;
} }
else else
...@@ -84,6 +88,7 @@ namespace RTC ...@@ -84,6 +88,7 @@ namespace RTC
*packet_size_delta = static_cast<int>(this->currentTimestampGroup.size) - static_cast<int>(this->prevTimestampGroup.size); *packet_size_delta = static_cast<int>(this->currentTimestampGroup.size) - static_cast<int>(this->prevTimestampGroup.size);
calculated_deltas = true; calculated_deltas = true;
} }
this->prevTimestampGroup = this->currentTimestampGroup; this->prevTimestampGroup = this->currentTimestampGroup;
// The new timestamp is now the current frame. // The new timestamp is now the current frame.
this->currentTimestampGroup.first_timestamp = timestamp; this->currentTimestampGroup.first_timestamp = timestamp;
...@@ -138,6 +143,7 @@ namespace RTC ...@@ -138,6 +143,7 @@ namespace RTC
else else
{ {
uint32_t timestamp_diff = timestamp - this->currentTimestampGroup.first_timestamp; uint32_t timestamp_diff = timestamp - this->currentTimestampGroup.first_timestamp;
return timestamp_diff > kTimestampGroupLengthTicks; return timestamp_diff > kTimestampGroupLengthTicks;
} }
} }
...@@ -150,7 +156,9 @@ namespace RTC ...@@ -150,7 +156,9 @@ namespace RTC
{ {
return false; return false;
} }
MS_ASSERT(this->currentTimestampGroup.complete_time_ms >= 0, "invalid complete_time_ms value"); MS_ASSERT(this->currentTimestampGroup.complete_time_ms >= 0, "invalid complete_time_ms value");
int64_t arrival_time_delta_ms = arrival_time_ms - this->currentTimestampGroup.complete_time_ms; int64_t arrival_time_delta_ms = arrival_time_ms - this->currentTimestampGroup.complete_time_ms;
uint32_t timestamp_diff = timestamp - this->currentTimestampGroup.timestamp; uint32_t timestamp_diff = timestamp - this->currentTimestampGroup.timestamp;
int64_t ts_delta_ms = this->timestampToMsCoeff * timestamp_diff + 0.5; int64_t ts_delta_ms = this->timestampToMsCoeff * timestamp_diff + 0.5;
......
...@@ -95,10 +95,12 @@ namespace RTC ...@@ -95,10 +95,12 @@ namespace RTC
const double k = fabs(modifiedOffset) < this->threshold ? this->kDown : this->kUp; const double k = fabs(modifiedOffset) < this->threshold ? this->kDown : this->kUp;
const int64_t kMaxTimeDeltaMs = 100; const int64_t kMaxTimeDeltaMs = 100;
int64_t timeDeltaMs = std::min(nowMs - this->lastUpdateMs, kMaxTimeDeltaMs); int64_t timeDeltaMs = std::min(nowMs - this->lastUpdateMs, kMaxTimeDeltaMs);
this->threshold += k * (fabs(modifiedOffset) - this->threshold) * timeDeltaMs; this->threshold += k * (fabs(modifiedOffset) - this->threshold) * timeDeltaMs;
const double kMinThreshold = 6; const double kMinThreshold = 6;
const double kMaxThreshold = 600; const double kMaxThreshold = 600;
this->threshold = std::min(std::max(this->threshold, kMinThreshold), kMaxThreshold); this->threshold = std::min(std::max(this->threshold, kMinThreshold), kMaxThreshold);
this->lastUpdateMs = nowMs; this->lastUpdateMs = nowMs;
......
...@@ -38,17 +38,21 @@ namespace RTC ...@@ -38,17 +38,21 @@ namespace RTC
std::vector<K> Keys(const std::map<K, V>& map) std::vector<K> Keys(const std::map<K, V>& map)
{ {
std::vector<K> keys; std::vector<K> keys;
keys.reserve(map.size()); keys.reserve(map.size());
for (typename std::map<K, V>::const_iterator it = map.begin(); it != map.end(); ++it) for (typename std::map<K, V>::const_iterator it = map.begin(); it != map.end(); ++it)
{ {
keys.push_back(it->first); keys.push_back(it->first);
} }
return keys; return keys;
} }
uint32_t ConvertMsTo24Bits(int64_t timeMs) uint32_t ConvertMsTo24Bits(int64_t timeMs)
{ {
uint32_t time_24Bits = static_cast<uint32_t>(((static_cast<uint64_t>(timeMs) << kAbsSendTimeFraction) + 500) / 1000) & 0x00FFFFFF; uint32_t time_24Bits = static_cast<uint32_t>(((static_cast<uint64_t>(timeMs) << kAbsSendTimeFraction) + 500) / 1000) & 0x00FFFFFF;
return time_24Bits; return time_24Bits;
} }
...@@ -58,7 +62,9 @@ namespace RTC ...@@ -58,7 +62,9 @@ namespace RTC
if (clusterAggregate.count == 0) if (clusterAggregate.count == 0)
return true; return true;
float clusterMean = clusterAggregate.sendMeanMs / static_cast<float>(clusterAggregate.count); float clusterMean = clusterAggregate.sendMeanMs / static_cast<float>(clusterAggregate.count);
return fabs(static_cast<float>(sendDeltaMs) - clusterMean) < 2.5f; return fabs(static_cast<float>(sendDeltaMs) - clusterMean) < 2.5f;
} }
...@@ -79,30 +85,37 @@ namespace RTC ...@@ -79,30 +85,37 @@ namespace RTC
Cluster current; Cluster current;
int64_t prevSendTime = -1; int64_t prevSendTime = -1;
int64_t prevRecvTime = -1; int64_t prevRecvTime = -1;
for (std::list<Probe>::const_iterator it = this->probes.begin(); it != this->probes.end(); ++it) for (std::list<Probe>::const_iterator it = this->probes.begin(); it != this->probes.end(); ++it)
{ {
if (prevSendTime >= 0) if (prevSendTime >= 0)
{ {
int sendDeltaMs = it->sendTimeMs - prevSendTime; int sendDeltaMs = it->sendTimeMs - prevSendTime;
int recvDeltaMs = it->recvTimeMs - prevRecvTime; int recvDeltaMs = it->recvTimeMs - prevRecvTime;
if (sendDeltaMs >= 1 && recvDeltaMs >= 1) if (sendDeltaMs >= 1 && recvDeltaMs >= 1)
{ {
++current.numAboveMinDelta; ++current.numAboveMinDelta;
} }
if (!IsWithinClusterBounds(sendDeltaMs, current)) if (!IsWithinClusterBounds(sendDeltaMs, current))
{ {
if (current.count >= kMinClusterSize) if (current.count >= kMinClusterSize)
AddCluster(clusters, &current); AddCluster(clusters, &current);
current = Cluster(); current = Cluster();
} }
current.sendMeanMs += sendDeltaMs; current.sendMeanMs += sendDeltaMs;
current.recvMeanMs += recvDeltaMs; current.recvMeanMs += recvDeltaMs;
current.meanSize += it->payloadSize; current.meanSize += it->payloadSize;
++current.count; ++current.count;
} }
prevSendTime = it->sendTimeMs; prevSendTime = it->sendTimeMs;
prevRecvTime = it->recvTimeMs; prevRecvTime = it->recvTimeMs;
} }
if (current.count >= kMinClusterSize) if (current.count >= kMinClusterSize)
AddCluster(clusters, &current); AddCluster(clusters, &current);
} }
...@@ -113,13 +126,16 @@ namespace RTC ...@@ -113,13 +126,16 @@ namespace RTC
int highestProbeBitrateBps = 0; int highestProbeBitrateBps = 0;
std::list<Cluster>::const_iterator bestIt = clusters.end(); std::list<Cluster>::const_iterator bestIt = clusters.end();
for (std::list<Cluster>::const_iterator it = clusters.begin(); it != clusters.end(); ++it) for (std::list<Cluster>::const_iterator it = clusters.begin(); it != clusters.end(); ++it)
{ {
if (it->sendMeanMs == 0 || it->recvMeanMs == 0) if (it->sendMeanMs == 0 || it->recvMeanMs == 0)
continue; continue;
if (it->numAboveMinDelta > it->count / 2 && (it->recvMeanMs - it->sendMeanMs <= 2.0f && it->sendMeanMs - it->recvMeanMs <= 5.0f)) if (it->numAboveMinDelta > it->count / 2 && (it->recvMeanMs - it->sendMeanMs <= 2.0f && it->sendMeanMs - it->recvMeanMs <= 5.0f))
{ {
int probeBitrateBps = std::min(it->GetSendBitrateBps(), it->GetRecvBitrateBps()); int probeBitrateBps = std::min(it->GetSendBitrateBps(), it->GetRecvBitrateBps());
if (probeBitrateBps > highestProbeBitrateBps) if (probeBitrateBps > highestProbeBitrateBps)
{ {
highestProbeBitrateBps = probeBitrateBps; highestProbeBitrateBps = probeBitrateBps;
...@@ -137,6 +153,7 @@ namespace RTC ...@@ -137,6 +153,7 @@ namespace RTC
break; break;
} }
} }
return bestIt; return bestIt;
} }
...@@ -146,12 +163,14 @@ namespace RTC ...@@ -146,12 +163,14 @@ namespace RTC
std::list<Cluster> clusters; std::list<Cluster> clusters;
ComputeClusters(&clusters); ComputeClusters(&clusters);
if (clusters.empty()) if (clusters.empty())
{ {
// If we reach the max number of probe packets and still have no clusters, // If we reach the max number of probe packets and still have no clusters,
// we will remove the oldest one. // we will remove the oldest one.
if (this->probes.size() >= kMaxProbePackets) if (this->probes.size() >= kMaxProbePackets)
this->probes.pop_front(); this->probes.pop_front();
return ProbeResult::kNoUpdate; return ProbeResult::kNoUpdate;
} }
...@@ -159,6 +178,7 @@ namespace RTC ...@@ -159,6 +178,7 @@ namespace RTC
if (bestIt != clusters.end()) if (bestIt != clusters.end())
{ {
int probeBitrateBps = std::min(bestIt->GetSendBitrateBps(), bestIt->GetRecvBitrateBps()); int probeBitrateBps = std::min(bestIt->GetSendBitrateBps(), bestIt->GetRecvBitrateBps());
// Make sure that a probe sent on a lower bitrate than our estimate can't // Make sure that a probe sent on a lower bitrate than our estimate can't
// reduce the estimate. // reduce the estimate.
if (IsBitrateImproving(probeBitrateBps)) if (IsBitrateImproving(probeBitrateBps))
...@@ -167,6 +187,7 @@ namespace RTC ...@@ -167,6 +187,7 @@ namespace RTC
bestIt->GetSendBitrateBps(), bestIt->GetRecvBitrateBps(), bestIt->sendMeanMs, bestIt->recvMeanMs, bestIt->count); bestIt->GetSendBitrateBps(), bestIt->GetRecvBitrateBps(), bestIt->sendMeanMs, bestIt->recvMeanMs, bestIt->count);
this->remoteRate.SetEstimate(probeBitrateBps, nowMs); this->remoteRate.SetEstimate(probeBitrateBps, nowMs);
return ProbeResult::kBitrateUpdated; return ProbeResult::kBitrateUpdated;
} }
} }
...@@ -175,6 +196,7 @@ namespace RTC ...@@ -175,6 +196,7 @@ namespace RTC
// of probes. // of probes.
if (clusters.size() >= kExpectedNumberOfProbes) if (clusters.size() >= kExpectedNumberOfProbes)
this->probes.clear(); this->probes.clear();
return ProbeResult::kNoUpdate; return ProbeResult::kNoUpdate;
} }
...@@ -183,7 +205,9 @@ namespace RTC ...@@ -183,7 +205,9 @@ namespace RTC
MS_TRACE(); MS_TRACE();
bool initialProbe = !this->remoteRate.ValidEstimate() && newBitrateBps > 0; bool initialProbe = !this->remoteRate.ValidEstimate() && newBitrateBps > 0;
bool bitrateAboveEstimate = this->remoteRate.ValidEstimate() && newBitrateBps > static_cast<int>(this->remoteRate.LatestEstimate()); bool bitrateAboveEstimate = this->remoteRate.ValidEstimate() &&
newBitrateBps > static_cast<int>(this->remoteRate.LatestEstimate());
return initialProbe || bitrateAboveEstimate; return initialProbe || bitrateAboveEstimate;
} }
...@@ -201,9 +225,7 @@ namespace RTC ...@@ -201,9 +225,7 @@ namespace RTC
MS_ASSERT(sendTime_24bits < (1ul << 24), "invalid sendTime_24bits value"); MS_ASSERT(sendTime_24bits < (1ul << 24), "invalid sendTime_24bits value");
if (!this->umaRecorded) if (!this->umaRecorded)
{
this->umaRecorded = true; this->umaRecorded = true;
}
// Shift up send time to use the full 32 bits that interArrival works with, // Shift up send time to use the full 32 bits that interArrival works with,
// so wrapping works properly. // so wrapping works properly.
...@@ -212,9 +234,9 @@ namespace RTC ...@@ -212,9 +234,9 @@ namespace RTC
int64_t nowMs = DepLibUV::GetTime(); int64_t nowMs = DepLibUV::GetTime();
// TODO(holmer): SSRCs are only needed for REMB, should be broken out from // TODO(holmer): SSRCs are only needed for REMB, should be broken out from
// here. // here.
// Check if incoming bitrate estimate is valid, and if it needs to be reset. // Check if incoming bitrate estimate is valid, and if it needs to be reset.
uint32_t incomingBitrate = this->incomingBitrate.GetRate(arrivalTimeMs); uint32_t incomingBitrate = this->incomingBitrate.GetRate(arrivalTimeMs);
if (incomingBitrate) if (incomingBitrate)
{ {
this->incomingBitrateInitialized = true; this->incomingBitrateInitialized = true;
...@@ -227,6 +249,7 @@ namespace RTC ...@@ -227,6 +249,7 @@ namespace RTC
this->incomingBitrate.Reset(); this->incomingBitrate.Reset();
this->incomingBitrateInitialized = false; this->incomingBitrateInitialized = false;
} }
this->incomingBitrate.Update(payloadSize, arrivalTimeMs); this->incomingBitrate.Update(payloadSize, arrivalTimeMs);
if (this->firstPacketTimeMs == -1) if (this->firstPacketTimeMs == -1)
...@@ -238,16 +261,20 @@ namespace RTC ...@@ -238,16 +261,20 @@ namespace RTC
bool updateEstimate = false; bool updateEstimate = false;
uint32_t targetBitrateBps = 0; uint32_t targetBitrateBps = 0;
std::vector<uint32_t> ssrcs; std::vector<uint32_t> ssrcs;
{ {
TimeoutStreams(nowMs); TimeoutStreams(nowMs);
// MS_DASSERT(this->interArrival.get());
// MS_DASSERT(this->estimator.get()); // MS_ASSERT(this->interArrival.get());
// MS_ASSERT(this->estimator.get());
this->ssrcs[ssrc] = nowMs; this->ssrcs[ssrc] = nowMs;
// For now only try to detect probes while we don't have a valid estimate. // For now only try to detect probes while we don't have a valid estimate.
// We currently assume that only packets larger than 200 bytes are paced by // We currently assume that only packets larger than 200 bytes are paced by
// the sender. // the sender.
const size_t kMinProbePacketSize = 200; const size_t kMinProbePacketSize = 200;
if (payloadSize > kMinProbePacketSize && (!this->remoteRate.ValidEstimate() || nowMs - this->firstPacketTimeMs < kInitialProbingIntervalMs)) if (payloadSize > kMinProbePacketSize && (!this->remoteRate.ValidEstimate() || nowMs - this->firstPacketTimeMs < kInitialProbingIntervalMs))
{ {
// TODO(holmer): Use a map instead to get correct order? // TODO(holmer): Use a map instead to get correct order?
...@@ -255,23 +282,30 @@ namespace RTC ...@@ -255,23 +282,30 @@ namespace RTC
{ {
int sendDeltaMs = -1; int sendDeltaMs = -1;
int recvDeltaMs = -1; int recvDeltaMs = -1;
if (!this->probes.empty()) if (!this->probes.empty())
{ {
sendDeltaMs = sendTimeMs - this->probes.back().sendTimeMs; sendDeltaMs = sendTimeMs - this->probes.back().sendTimeMs;
recvDeltaMs = arrivalTimeMs - this->probes.back().recvTimeMs; recvDeltaMs = arrivalTimeMs - this->probes.back().recvTimeMs;
} }
MS_DEBUG_TAG(rbe, "probe packet received: send time=%" PRId64 " ms, recv time=%" PRId64 " ms, send delta=%d ms, recv delta=%d ms", sendTimeMs, arrivalTimeMs, sendDeltaMs, recvDeltaMs);
MS_DEBUG_TAG(rbe,
"probe packet received [send time:%" PRId64 "ms, recv time:%" PRId64 "ms, send delta:%dms, recv delta:%d ms]", sendTimeMs, arrivalTimeMs, sendDeltaMs, recvDeltaMs);
} }
this->probes.push_back(Probe(sendTimeMs, arrivalTimeMs, payloadSize)); this->probes.push_back(Probe(sendTimeMs, arrivalTimeMs, payloadSize));
++this->totalProbesReceived; ++this->totalProbesReceived;
// Make sure that a probe which updated the bitrate immediately has an // Make sure that a probe which updated the bitrate immediately has an
// effect by calling the onReceiveBitrateChanged callback. // effect by calling the onReceiveBitrateChanged callback.
if (ProcessClusters(nowMs) == ProbeResult::kBitrateUpdated) if (ProcessClusters(nowMs) == ProbeResult::kBitrateUpdated)
updateEstimate = true; updateEstimate = true;
} }
if (this->interArrival->ComputeDeltas(timestamp, arrivalTimeMs, nowMs, payloadSize, &tsDelta, &tDelta, &sizeDelta)) if (this->interArrival->ComputeDeltas(timestamp, arrivalTimeMs, nowMs, payloadSize, &tsDelta, &tDelta, &sizeDelta))
{ {
double tsDeltaMs = (1000.0 * tsDelta) / (1 << kInterArrivalShift); double tsDeltaMs = (1000.0 * tsDelta) / (1 << kInterArrivalShift);
this->estimator->Update(tDelta, tsDeltaMs, sizeDelta, this->detector.State(), arrivalTimeMs); this->estimator->Update(tDelta, tsDeltaMs, sizeDelta, this->detector.State(), arrivalTimeMs);
this->detector.Detect(this->estimator->GetOffset(), tsDeltaMs, this->estimator->GetNumOfDeltas(), arrivalTimeMs); this->detector.Detect(this->estimator->GetOffset(), tsDeltaMs, this->estimator->GetNumOfDeltas(), arrivalTimeMs);
} }
...@@ -280,32 +314,37 @@ namespace RTC ...@@ -280,32 +314,37 @@ namespace RTC
{ {
// Check if it's time for a periodic update or if we should update because // Check if it's time for a periodic update or if we should update because
// of an over-use. // of an over-use.
if (this->lastUpdateMs == -1 || nowMs - this->lastUpdateMs > this->remoteRate.GetFeedbackInterval()) if (
this->lastUpdateMs == -1 ||
nowMs - this->lastUpdateMs > this->remoteRate.GetFeedbackInterval())
{ {
updateEstimate = true; updateEstimate = true;
} }
else if (this->detector.State() == kBwOverusing) else if (this->detector.State() == kBwOverusing)
{ {
uint32_t incomingRate = this->incomingBitrate.GetRate(arrivalTimeMs); uint32_t incomingRate = this->incomingBitrate.GetRate(arrivalTimeMs);
if (incomingRate && this->remoteRate.TimeToReduceFurther(nowMs, incomingRate)) if (incomingRate && this->remoteRate.TimeToReduceFurther(nowMs, incomingRate))
{
updateEstimate = true; updateEstimate = true;
} }
} }
}
if (updateEstimate) if (updateEstimate)
{ {
// The first overuse should immediately trigger a new estimate. // The first overuse should immediately trigger a new estimate.
// We also have to update the estimate immediately if we are overusing // We also have to update the estimate immediately if we are overusing
// and the target bitrate is too high compared to what we are receiving. // and the target bitrate is too high compared to what we are receiving.
const RateControlInput input(this->detector.State(), this->incomingBitrate.GetRate(arrivalTimeMs), this->estimator->GetVarNoise()); const RateControlInput input(
this->detector.State(), this->incomingBitrate.GetRate(arrivalTimeMs),
this->estimator->GetVarNoise());
this->remoteRate.Update(&input, nowMs); this->remoteRate.Update(&input, nowMs);
targetBitrateBps = this->remoteRate.UpdateBandwidthEstimate(nowMs); targetBitrateBps = this->remoteRate.UpdateBandwidthEstimate(nowMs);
updateEstimate = this->remoteRate.ValidEstimate(); updateEstimate = this->remoteRate.ValidEstimate();
ssrcs = Keys(this->ssrcs); ssrcs = Keys(this->ssrcs);
} }
} }
if (updateEstimate) if (updateEstimate)
{ {
this->lastUpdateMs = nowMs; this->lastUpdateMs = nowMs;
...@@ -320,14 +359,11 @@ namespace RTC ...@@ -320,14 +359,11 @@ namespace RTC
for (Ssrcs::iterator it = this->ssrcs.begin(); it != this->ssrcs.end();) for (Ssrcs::iterator it = this->ssrcs.begin(); it != this->ssrcs.end();)
{ {
if ((nowMs - it->second) > kStreamTimeOutMs) if ((nowMs - it->second) > kStreamTimeOutMs)
{
this->ssrcs.erase(it++); this->ssrcs.erase(it++);
}
else else
{
++it; ++it;
} }
}
if (this->ssrcs.empty()) if (this->ssrcs.empty())
{ {
// We can't update the estimate if we don't have any active streams. // We can't update the estimate if we don't have any active streams.
...@@ -342,21 +378,17 @@ namespace RTC ...@@ -342,21 +378,17 @@ namespace RTC
{ {
MS_TRACE(); MS_TRACE();
// MS_DASSERT(ssrcs); // MS_ASSERT(ssrcs);
// MS_DASSERT(bitrateBps); // MS_ASSERT(bitrateBps);
if (!this->remoteRate.ValidEstimate()) if (!this->remoteRate.ValidEstimate())
{
return false; return false;
}
*ssrcs = Keys(this->ssrcs); *ssrcs = Keys(this->ssrcs);
if (this->ssrcs.empty()) if (this->ssrcs.empty())
{
*bitrateBps = 0; *bitrateBps = 0;
}
else else
{
*bitrateBps = this->remoteRate.LatestEstimate(); *bitrateBps = this->remoteRate.LatestEstimate();
}
return true; return true;
} }
......
...@@ -30,9 +30,11 @@ namespace RTC ...@@ -30,9 +30,11 @@ namespace RTC
{ {
this->umaRecorded = true; this->umaRecorded = true;
} }
uint32_t ssrc = packet.GetSsrc(); uint32_t ssrc = packet.GetSsrc();
uint32_t rtpTimestamp = packet.GetTimestamp() + transmissionTimeOffset; uint32_t rtpTimestamp = packet.GetTimestamp() + transmissionTimeOffset;
int64_t nowMs = DepLibUV::GetTime(); int64_t nowMs = DepLibUV::GetTime();
SsrcOveruseEstimatorMap::iterator it = this->overuseDetectors.find(ssrc); SsrcOveruseEstimatorMap::iterator it = this->overuseDetectors.find(ssrc);
if (it == this->overuseDetectors.end()) if (it == this->overuseDetectors.end())
{ {
...@@ -46,11 +48,13 @@ namespace RTC ...@@ -46,11 +48,13 @@ namespace RTC
this->overuseDetectors.insert(std::make_pair(ssrc, new Detector(nowMs, OverUseDetectorOptions(), true))); this->overuseDetectors.insert(std::make_pair(ssrc, new Detector(nowMs, OverUseDetectorOptions(), true)));
it = insertResult.first; it = insertResult.first;
} }
Detector* estimator = it->second; Detector* estimator = it->second;
estimator->lastPacketTimeMs = nowMs; estimator->lastPacketTimeMs = nowMs;
// Check if incoming bitrate estimate is valid, and if it needs to be reset. // Check if incoming bitrate estimate is valid, and if it needs to be reset.
uint32_t incomingBitrate = this->incomingBitrate.GetRate(nowMs); uint32_t incomingBitrate = this->incomingBitrate.GetRate(nowMs);
if (incomingBitrate) if (incomingBitrate)
{ {
this->lastValidIncomingBitrate = incomingBitrate; this->lastValidIncomingBitrate = incomingBitrate;
...@@ -63,21 +67,26 @@ namespace RTC ...@@ -63,21 +67,26 @@ namespace RTC
this->incomingBitrate.Reset(); this->incomingBitrate.Reset();
this->lastValidIncomingBitrate = 0; this->lastValidIncomingBitrate = 0;
} }
this->incomingBitrate.Update(payloadSize, nowMs); this->incomingBitrate.Update(payloadSize, nowMs);
const BandwidthUsage priorState = estimator->detector.State(); const BandwidthUsage priorState = estimator->detector.State();
uint32_t timestampDelta = 0; uint32_t timestampDelta = 0;
int64_t timeDelta = 0; int64_t timeDelta = 0;
int sizeDelta = 0; int sizeDelta = 0;
if (estimator->interArrival.ComputeDeltas(rtpTimestamp, arrivalTimeMs, nowMs, payloadSize, &timestampDelta, &timeDelta, &sizeDelta)) if (estimator->interArrival.ComputeDeltas(rtpTimestamp, arrivalTimeMs, nowMs, payloadSize, &timestampDelta, &timeDelta, &sizeDelta))
{ {
double timestampDeltaMs = timestampDelta * kTimestampToMs; double timestampDeltaMs = timestampDelta * kTimestampToMs;
estimator->estimator.Update(timeDelta, timestampDeltaMs, sizeDelta, estimator->detector.State(), nowMs); estimator->estimator.Update(timeDelta, timestampDeltaMs, sizeDelta, estimator->detector.State(), nowMs);
estimator->detector.Detect(estimator->estimator.GetOffset(), timestampDeltaMs, estimator->estimator.GetNumOfDeltas(), nowMs); estimator->detector.Detect(estimator->estimator.GetOffset(), timestampDeltaMs, estimator->estimator.GetNumOfDeltas(), nowMs);
} }
if (estimator->detector.State() == kBwOverusing) if (estimator->detector.State() == kBwOverusing)
{ {
uint32_t incomingBitrateBps = this->incomingBitrate.GetRate(nowMs); uint32_t incomingBitrateBps = this->incomingBitrate.GetRate(nowMs);
if (incomingBitrateBps && (priorState != kBwOverusing || GetRemoteRate()->TimeToReduceFurther(nowMs, incomingBitrateBps))) if (incomingBitrateBps && (priorState != kBwOverusing || GetRemoteRate()->TimeToReduceFurther(nowMs, incomingBitrateBps)))
{ {
// The first overuse should immediately trigger a new estimate. // The first overuse should immediately trigger a new estimate.
...@@ -93,10 +102,9 @@ namespace RTC ...@@ -93,10 +102,9 @@ namespace RTC
MS_TRACE(); MS_TRACE();
if (this->lastProcessTime < 0) if (this->lastProcessTime < 0)
{
return 0; return 0;
}
// MS_DASSERT(this->processIntervalMs > 0); // MS_ASSERT(this->processIntervalMs > 0);
return this->lastProcessTime + this->processIntervalMs - DepLibUV::GetTime(); return this->lastProcessTime + this->processIntervalMs - DepLibUV::GetTime();
} }
...@@ -107,9 +115,11 @@ namespace RTC ...@@ -107,9 +115,11 @@ namespace RTC
BandwidthUsage bwState = kBwNormal; BandwidthUsage bwState = kBwNormal;
double sumVarNoise = 0.0; double sumVarNoise = 0.0;
SsrcOveruseEstimatorMap::iterator it = this->overuseDetectors.begin(); SsrcOveruseEstimatorMap::iterator it = this->overuseDetectors.begin();
while (it != this->overuseDetectors.end()) while (it != this->overuseDetectors.end())
{ {
const int64_t timeOfLastReceivedPacket = it->second->lastPacketTimeMs; const int64_t timeOfLastReceivedPacket = it->second->lastPacketTimeMs;
if (timeOfLastReceivedPacket >= 0 && nowMs - timeOfLastReceivedPacket > kStreamTimeOutMs) if (timeOfLastReceivedPacket >= 0 && nowMs - timeOfLastReceivedPacket > kStreamTimeOutMs)
{ {
// This over-use detector hasn't received packets for |kStreamTimeOutMs| // This over-use detector hasn't received packets for |kStreamTimeOutMs|
...@@ -123,9 +133,8 @@ namespace RTC ...@@ -123,9 +133,8 @@ namespace RTC
// Make sure that we trigger an over-use if any of the over-use detectors // Make sure that we trigger an over-use if any of the over-use detectors
// is detecting over-use. // is detecting over-use.
if (it->second->detector.State() > bwState) if (it->second->detector.State() > bwState)
{
bwState = it->second->detector.State(); bwState = it->second->detector.State();
}
++it; ++it;
} }
} }
...@@ -134,16 +143,19 @@ namespace RTC ...@@ -134,16 +143,19 @@ namespace RTC
{ {
return; return;
} }
AimdRateControl* remoteRate = GetRemoteRate();
AimdRateControl* remoteRate = GetRemoteRate();
double meanNoiseVar = sumVarNoise / static_cast<double>(this->overuseDetectors.size()); double meanNoiseVar = sumVarNoise / static_cast<double>(this->overuseDetectors.size());
const RateControlInput input(bwState, this->incomingBitrate.GetRate(nowMs), meanNoiseVar); const RateControlInput input(bwState, this->incomingBitrate.GetRate(nowMs), meanNoiseVar);
remoteRate->Update(&input, nowMs); remoteRate->Update(&input, nowMs);
uint32_t targetBitrate = remoteRate->UpdateBandwidthEstimate(nowMs); uint32_t targetBitrate = remoteRate->UpdateBandwidthEstimate(nowMs);
if (remoteRate->ValidEstimate()) if (remoteRate->ValidEstimate())
{ {
this->processIntervalMs = remoteRate->GetFeedbackInterval(); this->processIntervalMs = remoteRate->GetFeedbackInterval();
// MS_DASSERT(this->processIntervalMs > 0); // MS_ASSERT(this->processIntervalMs > 0);
std::vector<uint32_t> ssrcs; std::vector<uint32_t> ssrcs;
...@@ -156,12 +168,10 @@ namespace RTC ...@@ -156,12 +168,10 @@ namespace RTC
{ {
MS_TRACE(); MS_TRACE();
MS_ASSERT(bitrateBps, "'bitrateBps' missing"); MS_ASSERT(bitrateBps, "bitrateBps missing");
if (!this->remoteRate->ValidEstimate()) if (!this->remoteRate->ValidEstimate())
{
return false; return false;
}
GetSsrcs(ssrcs); GetSsrcs(ssrcs);
if (ssrcs->empty()) if (ssrcs->empty())
...@@ -176,11 +186,12 @@ namespace RTC ...@@ -176,11 +186,12 @@ namespace RTC
{ {
MS_TRACE(); MS_TRACE();
MS_ASSERT(ssrcs, "'ssrcs' missing"); MS_ASSERT(ssrcs, "ssrcs missing");
ssrcs->resize(this->overuseDetectors.size()); ssrcs->resize(this->overuseDetectors.size());
int i = 0; int i = 0;
for (SsrcOveruseEstimatorMap::const_iterator it = this->overuseDetectors.begin(); it != this->overuseDetectors.end(); ++it, ++i) for (SsrcOveruseEstimatorMap::const_iterator it = this->overuseDetectors.begin(); it != this->overuseDetectors.end(); ++it, ++i)
{ {
(*ssrcs)[i] = it->first; (*ssrcs)[i] = it->first;
......
...@@ -69,6 +69,8 @@ namespace RTC ...@@ -69,6 +69,8 @@ namespace RTC
RTC::RTCP::ReceiverReport* RtpStreamRecv::GetRtcpReceiverReport() RTC::RTCP::ReceiverReport* RtpStreamRecv::GetRtcpReceiverReport()
{ {
MS_TRACE();
RTC::RTCP::ReceiverReport* report = new RTC::RTCP::ReceiverReport(); RTC::RTCP::ReceiverReport* report = new RTC::RTCP::ReceiverReport();
// Calculate Packets Expected and Lost. // Calculate Packets Expected and Lost.
...@@ -123,13 +125,31 @@ namespace RTC ...@@ -123,13 +125,31 @@ namespace RTC
void RtpStreamRecv::ReceiveRtcpSenderReport(RTC::RTCP::SenderReport* report) void RtpStreamRecv::ReceiveRtcpSenderReport(RTC::RTCP::SenderReport* report)
{ {
MS_TRACE();
this->last_sr_received = DepLibUV::GetTime(); this->last_sr_received = DepLibUV::GetTime();
this->last_sr_timestamp = report->GetNtpSec() << 16; this->last_sr_timestamp = report->GetNtpSec() << 16;
this->last_sr_timestamp += report->GetNtpFrac() >> 16; this->last_sr_timestamp += report->GetNtpFrac() >> 16;
} }
void RtpStreamRecv::RequestFullFrame()
{
MS_TRACE();
if (this->params.usePli)
{
// Reset NackGenerator.
if (this->params.useNack)
this->nackGenerator.reset(new RTC::NackGenerator(this));
this->listener->onPliRequired(this);
}
}
void RtpStreamRecv::CalculateJitter(uint32_t rtpTimestamp) void RtpStreamRecv::CalculateJitter(uint32_t rtpTimestamp)
{ {
MS_TRACE();
if (!this->params.clockRate) if (!this->params.clockRate)
return; return;
......
...@@ -7,7 +7,7 @@ ...@@ -7,7 +7,7 @@
#include "Utils.hpp" #include "Utils.hpp"
#define RTP_SEQ_MOD (1<<16) #define RTP_SEQ_MOD (1<<16)
#define MAX_RETRANSMISSION_AGE 1000 // Don't retransmit packets older than this (ms). #define MAX_RETRANSMISSION_AGE 500 // Don't retransmit packets older than this (ms).
#define DEFAULT_RTT 100 // Default RTT if not set (in ms). #define DEFAULT_RTT 100 // Default RTT if not set (in ms).
namespace RTC namespace RTC
......
...@@ -8,6 +8,8 @@ ...@@ -8,6 +8,8 @@
#include "Utils.hpp" #include "Utils.hpp"
#include "MediaSoupError.hpp" #include "MediaSoupError.hpp"
#include "Logger.hpp" #include "Logger.hpp"
#include <sstream> // std::ostringstream()
#include <iterator> // std::ostream_iterator<>()
#include <cmath> // std::pow() #include <cmath> // std::pow()
#define ICE_CANDIDATE_DEFAULT_LOCAL_PRIORITY 20000 #define ICE_CANDIDATE_DEFAULT_LOCAL_PRIORITY 20000
...@@ -34,6 +36,11 @@ uint32_t generateIceCandidatePriority(uint16_t local_preference) ...@@ -34,6 +36,11 @@ uint32_t generateIceCandidatePriority(uint16_t local_preference)
namespace RTC namespace RTC
{ {
/* Consts. */
static constexpr uint64_t EffectiveMaxBitrateCheckInterval = 2000; // In ms.
static constexpr double EffectiveMaxBitrateThresholdBeforeFullFrame = 0.6; // 0.0 - 1.0.
/* Class variables. */ /* Class variables. */
uint8_t Transport::rtcpBuffer[MS_RTCP_BUFFER_SIZE]; uint8_t Transport::rtcpBuffer[MS_RTCP_BUFFER_SIZE];
...@@ -282,6 +289,7 @@ namespace RTC ...@@ -282,6 +289,7 @@ namespace RTC
static const Json::StaticString v_failed("failed"); static const Json::StaticString v_failed("failed");
static const Json::StaticString k_useRemb("useRemb"); static const Json::StaticString k_useRemb("useRemb");
static const Json::StaticString k_maxBitrate("maxBitrate"); static const Json::StaticString k_maxBitrate("maxBitrate");
static const Json::StaticString k_effectiveMaxBitrate("effectiveMaxBitrate");
static const Json::StaticString k_rtpListener("rtpListener"); static const Json::StaticString k_rtpListener("rtpListener");
Json::Value json(Json::objectValue); Json::Value json(Json::objectValue);
...@@ -368,6 +376,9 @@ namespace RTC ...@@ -368,6 +376,9 @@ namespace RTC
// Add `maxBitrate`. // Add `maxBitrate`.
json[k_maxBitrate] = (Json::UInt)this->maxBitrate; json[k_maxBitrate] = (Json::UInt)this->maxBitrate;
// Add `effectiveMaxBitrate`.
json[k_effectiveMaxBitrate] = (Json::UInt)this->effectiveMaxBitrate;
// Add `rtpListener`. // Add `rtpListener`.
json[k_rtpListener] = this->rtpListener.toJson(); json[k_rtpListener] = this->rtpListener.toJson();
...@@ -1164,19 +1175,25 @@ namespace RTC ...@@ -1164,19 +1175,25 @@ namespace RTC
{ {
MS_TRACE(); MS_TRACE();
uint32_t effective_bitrate = bitrate; uint32_t effective_bitrate;
uint64_t now = DepLibUV::GetTime();
// Limit bitrate if requested via API. // Limit bitrate if requested via API.
if (this->maxBitrate) if (this->maxBitrate)
effective_bitrate = std::min(bitrate, this->maxBitrate); effective_bitrate = std::min(bitrate, this->maxBitrate);
else
effective_bitrate = bitrate;
MS_DEBUG_TAG(rbe, if (MS_HAS_DEBUG_TAG(rbe))
"sending RTCP REMB packet [estimated:%" PRIu32 "bps, effective:%" PRIu32 "bps]",
bitrate, effective_bitrate);
for (auto ssrc: ssrcs)
{ {
MS_DEBUG_TAG(rbe, " ssrc : %" PRIu32, ssrc); std::ostringstream ssrcs_stream;
std::copy(ssrcs.begin(), ssrcs.end() - 1, std::ostream_iterator<int>(ssrcs_stream, ","));
ssrcs_stream << ssrcs.back();
MS_DEBUG_TAG(rbe,
"sending RTCP REMB packet [estimated:%" PRIu32 "bps, effective:%" PRIu32 "bps, ssrcs:%s]",
bitrate, effective_bitrate, ssrcs_stream.str().c_str());
} }
RTC::RTCP::FeedbackPsRembPacket packet(0, 0); RTC::RTCP::FeedbackPsRembPacket packet(0, 0);
...@@ -1184,5 +1201,23 @@ namespace RTC ...@@ -1184,5 +1201,23 @@ namespace RTC
packet.SetSsrcs(ssrcs); packet.SetSsrcs(ssrcs);
packet.Serialize(Transport::rtcpBuffer); packet.Serialize(Transport::rtcpBuffer);
this->SendRtcpPacket(&packet); this->SendRtcpPacket(&packet);
// Trigger a full frame for all the suitable strams if the effective max bitrate
// has decreased abruptly.
if (now - this->lastEffectiveMaxBitrateAt > EffectiveMaxBitrateCheckInterval)
{
if (
bitrate &&
this->effectiveMaxBitrate &&
(double)effective_bitrate / (double)this->effectiveMaxBitrate < EffectiveMaxBitrateThresholdBeforeFullFrame)
{
MS_WARN_TAG(rbe, "uplink effective max bitrate abruptly decrease, requesting full frames");
this->listener->onTransportFullFrameRequired(this);
}
this->lastEffectiveMaxBitrateAt = now;
this->effectiveMaxBitrate = effective_bitrate;
}
} }
} }
Markdown 格式
0% 或
您添加了 0 人 到此讨论。请谨慎行事。
请先完成此评论的编辑!
请 注册 或者 后发表评论