webrtc_m130/modules/rtp_rtcp/source/receive_statistics_impl.h

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

253 lines
9.6 KiB
C
Raw Normal View History

/*
* Copyright (c) 2013 The WebRTC project authors. All Rights Reserved.
*
* Use of this source code is governed by a BSD-style license
* that can be found in the LICENSE file in the root of the source
* tree. An additional intellectual property rights grant can be found
* in the file PATENTS. All contributing project authors may
* be found in the AUTHORS file in the root of the source tree.
*/
#ifndef MODULES_RTP_RTCP_SOURCE_RECEIVE_STATISTICS_IMPL_H_
#define MODULES_RTP_RTCP_SOURCE_RECEIVE_STATISTICS_IMPL_H_
#include <algorithm>
#include <functional>
#include <memory>
#include <utility>
#include <vector>
Reland "Change ReceiveStatistics reaction to large sequence numbers jumps" This reverts commit 7e0299e2452b021fcd14a8fdb86257459eeacf90. Reason for revert: audio receive stream fix not to use 0 reordering threshold Original change's description: > Revert "Change ReceiveStatistics reaction to large sequence numbers jumps" > > This reverts commit c4f120130f495e9726bf221356642de69125f4a2. > > Reason for revert: breaks downstream tests due to zero max reordering for audio receive channels > > Original change's description: > > Change ReceiveStatistics reaction to large sequence numbers jumps > > > > Consider stream restart when two sequential packets arrived far from > > previous packets' sequence numbers. > > instead of resetting on single one. > > For packet loss calculation ignore sequence number gap during reset. > > > > Bug: webrtc:9445, b/38179459 > > Change-Id: I0c2717ef8f9ec182b280ae757b5582f56d9afcef > > Reviewed-on: https://webrtc-review.googlesource.com/c/111962 > > Commit-Queue: Danil Chapovalov <danilchap@webrtc.org> > > Reviewed-by: Åsa Persson <asapersson@webrtc.org> > > Cr-Commit-Position: refs/heads/master@{#25890} > > TBR=danilchap@webrtc.org,asapersson@webrtc.org > > Change-Id: Icc9f4d86d9f0b07f0fa2f3d443f9a90aa91f5e21 > No-Presubmit: true > No-Tree-Checks: true > No-Try: true > Bug: webrtc:9445, b/38179459 > Reviewed-on: https://webrtc-review.googlesource.com/c/113067 > Reviewed-by: Danil Chapovalov <danilchap@webrtc.org> > Commit-Queue: Danil Chapovalov <danilchap@webrtc.org> > Cr-Commit-Position: refs/heads/master@{#25897} TBR=danilchap@webrtc.org,asapersson@webrtc.org Change-Id: I8747aa5cb6209b92fafefed077bc19d305d11db6 No-Presubmit: true No-Tree-Checks: true No-Try: true Bug: webrtc:9445, b/38179459 Reviewed-on: https://webrtc-review.googlesource.com/c/113263 Reviewed-by: Danil Chapovalov <danilchap@webrtc.org> Commit-Queue: Danil Chapovalov <danilchap@webrtc.org> Cr-Commit-Position: refs/heads/master@{#25907}
2018-12-05 14:55:46 +00:00
#include "absl/types/optional.h"
#include "api/units/time_delta.h"
#include "api/units/timestamp.h"
#include "modules/rtp_rtcp/include/receive_statistics.h"
#include "modules/rtp_rtcp/source/rtcp_packet/report_block.h"
#include "rtc_base/containers/flat_map.h"
#include "rtc_base/numerics/sequence_number_unwrapper.h"
#include "rtc_base/rate_statistics.h"
#include "rtc_base/synchronization/mutex.h"
#include "rtc_base/thread_annotations.h"
namespace webrtc {
// Extends StreamStatistician with methods needed by the implementation.
class StreamStatisticianImplInterface : public StreamStatistician {
public:
virtual ~StreamStatisticianImplInterface() = default;
virtual void MaybeAppendReportBlockAndReset(
std::vector<rtcp::ReportBlock>& report_blocks) = 0;
virtual void SetMaxReorderingThreshold(int max_reordering_threshold) = 0;
virtual void EnableRetransmitDetection(bool enable) = 0;
virtual void UpdateCounters(const RtpPacketReceived& packet) = 0;
};
// Thread-compatible implementation of StreamStatisticianImplInterface.
class StreamStatisticianImpl : public StreamStatisticianImplInterface {
public:
StreamStatisticianImpl(uint32_t ssrc,
Clock* clock,
int max_reordering_threshold);
~StreamStatisticianImpl() override;
// Implements StreamStatistician
RtpReceiveStats GetStats() const override;
absl::optional<int> GetFractionLostInPercent() const override;
StreamDataCounters GetReceiveStreamDataCounters() const override;
uint32_t BitrateReceived() const override;
// Implements StreamStatisticianImplInterface
void MaybeAppendReportBlockAndReset(
std::vector<rtcp::ReportBlock>& report_blocks) override;
void SetMaxReorderingThreshold(int max_reordering_threshold) override;
void EnableRetransmitDetection(bool enable) override;
// Updates StreamStatistician for incoming packets.
void UpdateCounters(const RtpPacketReceived& packet) override;
private:
bool IsRetransmitOfOldPacket(const RtpPacketReceived& packet,
Timestamp now) const;
void UpdateJitter(const RtpPacketReceived& packet, Timestamp receive_time);
void ReviseFrequencyAndJitter(int payload_type_frequency);
Reland "Change ReceiveStatistics reaction to large sequence numbers jumps" This reverts commit 7e0299e2452b021fcd14a8fdb86257459eeacf90. Reason for revert: audio receive stream fix not to use 0 reordering threshold Original change's description: > Revert "Change ReceiveStatistics reaction to large sequence numbers jumps" > > This reverts commit c4f120130f495e9726bf221356642de69125f4a2. > > Reason for revert: breaks downstream tests due to zero max reordering for audio receive channels > > Original change's description: > > Change ReceiveStatistics reaction to large sequence numbers jumps > > > > Consider stream restart when two sequential packets arrived far from > > previous packets' sequence numbers. > > instead of resetting on single one. > > For packet loss calculation ignore sequence number gap during reset. > > > > Bug: webrtc:9445, b/38179459 > > Change-Id: I0c2717ef8f9ec182b280ae757b5582f56d9afcef > > Reviewed-on: https://webrtc-review.googlesource.com/c/111962 > > Commit-Queue: Danil Chapovalov <danilchap@webrtc.org> > > Reviewed-by: Åsa Persson <asapersson@webrtc.org> > > Cr-Commit-Position: refs/heads/master@{#25890} > > TBR=danilchap@webrtc.org,asapersson@webrtc.org > > Change-Id: Icc9f4d86d9f0b07f0fa2f3d443f9a90aa91f5e21 > No-Presubmit: true > No-Tree-Checks: true > No-Try: true > Bug: webrtc:9445, b/38179459 > Reviewed-on: https://webrtc-review.googlesource.com/c/113067 > Reviewed-by: Danil Chapovalov <danilchap@webrtc.org> > Commit-Queue: Danil Chapovalov <danilchap@webrtc.org> > Cr-Commit-Position: refs/heads/master@{#25897} TBR=danilchap@webrtc.org,asapersson@webrtc.org Change-Id: I8747aa5cb6209b92fafefed077bc19d305d11db6 No-Presubmit: true No-Tree-Checks: true No-Try: true Bug: webrtc:9445, b/38179459 Reviewed-on: https://webrtc-review.googlesource.com/c/113263 Reviewed-by: Danil Chapovalov <danilchap@webrtc.org> Commit-Queue: Danil Chapovalov <danilchap@webrtc.org> Cr-Commit-Position: refs/heads/master@{#25907}
2018-12-05 14:55:46 +00:00
// Updates StreamStatistician for out of order packets.
// Returns true if packet considered to be out of order.
bool UpdateOutOfOrder(const RtpPacketReceived& packet,
int64_t sequence_number,
Timestamp now);
Reland "Change ReceiveStatistics reaction to large sequence numbers jumps" This reverts commit 7e0299e2452b021fcd14a8fdb86257459eeacf90. Reason for revert: audio receive stream fix not to use 0 reordering threshold Original change's description: > Revert "Change ReceiveStatistics reaction to large sequence numbers jumps" > > This reverts commit c4f120130f495e9726bf221356642de69125f4a2. > > Reason for revert: breaks downstream tests due to zero max reordering for audio receive channels > > Original change's description: > > Change ReceiveStatistics reaction to large sequence numbers jumps > > > > Consider stream restart when two sequential packets arrived far from > > previous packets' sequence numbers. > > instead of resetting on single one. > > For packet loss calculation ignore sequence number gap during reset. > > > > Bug: webrtc:9445, b/38179459 > > Change-Id: I0c2717ef8f9ec182b280ae757b5582f56d9afcef > > Reviewed-on: https://webrtc-review.googlesource.com/c/111962 > > Commit-Queue: Danil Chapovalov <danilchap@webrtc.org> > > Reviewed-by: Åsa Persson <asapersson@webrtc.org> > > Cr-Commit-Position: refs/heads/master@{#25890} > > TBR=danilchap@webrtc.org,asapersson@webrtc.org > > Change-Id: Icc9f4d86d9f0b07f0fa2f3d443f9a90aa91f5e21 > No-Presubmit: true > No-Tree-Checks: true > No-Try: true > Bug: webrtc:9445, b/38179459 > Reviewed-on: https://webrtc-review.googlesource.com/c/113067 > Reviewed-by: Danil Chapovalov <danilchap@webrtc.org> > Commit-Queue: Danil Chapovalov <danilchap@webrtc.org> > Cr-Commit-Position: refs/heads/master@{#25897} TBR=danilchap@webrtc.org,asapersson@webrtc.org Change-Id: I8747aa5cb6209b92fafefed077bc19d305d11db6 No-Presubmit: true No-Tree-Checks: true No-Try: true Bug: webrtc:9445, b/38179459 Reviewed-on: https://webrtc-review.googlesource.com/c/113263 Reviewed-by: Danil Chapovalov <danilchap@webrtc.org> Commit-Queue: Danil Chapovalov <danilchap@webrtc.org> Cr-Commit-Position: refs/heads/master@{#25907}
2018-12-05 14:55:46 +00:00
// Checks if this StreamStatistician received any rtp packets.
bool ReceivedRtpPacket() const { return last_receive_time_.has_value(); }
const uint32_t ssrc_;
Clock* const clock_;
// Delta used to map internal timestamps to Unix epoch ones.
const TimeDelta delta_internal_unix_epoch_;
RateStatistics incoming_bitrate_;
// In number of packets or sequence numbers.
int max_reordering_threshold_;
bool enable_retransmit_detection_;
bool cumulative_loss_is_capped_;
// Stats on received RTP packets.
uint32_t jitter_q4_;
// Cumulative loss according to RFC 3550, which may be negative (and often is,
// if packets are reordered and there are non-RTX retransmissions).
int32_t cumulative_loss_;
// Offset added to outgoing rtcp reports, to make ensure that the reported
// cumulative loss is non-negative. Reports with negative values confuse some
// senders, in particular, our own loss-based bandwidth estimator.
int32_t cumulative_loss_rtcp_offset_;
absl::optional<Timestamp> last_receive_time_;
uint32_t last_received_timestamp_;
RtpSequenceNumberUnwrapper seq_unwrapper_;
int64_t received_seq_first_;
int64_t received_seq_max_;
Reland "Change ReceiveStatistics reaction to large sequence numbers jumps" This reverts commit 7e0299e2452b021fcd14a8fdb86257459eeacf90. Reason for revert: audio receive stream fix not to use 0 reordering threshold Original change's description: > Revert "Change ReceiveStatistics reaction to large sequence numbers jumps" > > This reverts commit c4f120130f495e9726bf221356642de69125f4a2. > > Reason for revert: breaks downstream tests due to zero max reordering for audio receive channels > > Original change's description: > > Change ReceiveStatistics reaction to large sequence numbers jumps > > > > Consider stream restart when two sequential packets arrived far from > > previous packets' sequence numbers. > > instead of resetting on single one. > > For packet loss calculation ignore sequence number gap during reset. > > > > Bug: webrtc:9445, b/38179459 > > Change-Id: I0c2717ef8f9ec182b280ae757b5582f56d9afcef > > Reviewed-on: https://webrtc-review.googlesource.com/c/111962 > > Commit-Queue: Danil Chapovalov <danilchap@webrtc.org> > > Reviewed-by: Åsa Persson <asapersson@webrtc.org> > > Cr-Commit-Position: refs/heads/master@{#25890} > > TBR=danilchap@webrtc.org,asapersson@webrtc.org > > Change-Id: Icc9f4d86d9f0b07f0fa2f3d443f9a90aa91f5e21 > No-Presubmit: true > No-Tree-Checks: true > No-Try: true > Bug: webrtc:9445, b/38179459 > Reviewed-on: https://webrtc-review.googlesource.com/c/113067 > Reviewed-by: Danil Chapovalov <danilchap@webrtc.org> > Commit-Queue: Danil Chapovalov <danilchap@webrtc.org> > Cr-Commit-Position: refs/heads/master@{#25897} TBR=danilchap@webrtc.org,asapersson@webrtc.org Change-Id: I8747aa5cb6209b92fafefed077bc19d305d11db6 No-Presubmit: true No-Tree-Checks: true No-Try: true Bug: webrtc:9445, b/38179459 Reviewed-on: https://webrtc-review.googlesource.com/c/113263 Reviewed-by: Danil Chapovalov <danilchap@webrtc.org> Commit-Queue: Danil Chapovalov <danilchap@webrtc.org> Cr-Commit-Position: refs/heads/master@{#25907}
2018-12-05 14:55:46 +00:00
// Assume that the other side restarted when there are two sequential packets
// with large jump from received_seq_max_.
absl::optional<uint16_t> received_seq_out_of_order_;
// Current counter values.
StreamDataCounters receive_counters_;
Revert "Update packetsLost and jitter stats any time a packet is received." This reverts commit 84916937b70472715efe5682bc273e91c3a72695. Reason for revert: breaking downstream projects. Original change's description: > Update packetsLost and jitter stats any time a packet is received. > > Before this CL, the packetsLost and jitter stats (as returned by > GetStats, at the API level) were only being updated when an RTCP SR or > RR is generated. According to the stats spec, "local" stats like this > should be updated any time a packet is received. > > This CL also fixes some minor issues with the calculation of packetsLost > (and fractionLost): > * Packets weren't being count as lost if lost over a sequence number > rollover. > * Temporary periods of "negative" loss (caused by duplicate or out of > order packets) weren't being accumulated into the cumulative loss > counter. Example: > Period 1: Received packets 1, 2, 4 > Loss over that period: 1 (expected 4 packets, got 3) > Reported cumulative loss: 1 > Period 2: Received packets 3, 5 > Loss over that period: -1 (expected 1 packet, got 2) > Reported cumulative loss: 1 (should be 0!) > > Landing with NOTRY because Android compile bots are broken for an > unrelated reason. > NOTRY=True > > Bug: webrtc:8804 > Change-Id: I840ba34de8957b1276f6bdaf93718f805629f5c8 > Reviewed-on: https://webrtc-review.googlesource.com/50020 > Commit-Queue: Taylor Brandstetter <deadbeef@webrtc.org> > Reviewed-by: Danil Chapovalov <danilchap@webrtc.org> > Reviewed-by: Oskar Sundbom <ossu@webrtc.org> > Cr-Commit-Position: refs/heads/master@{#23731} TBR=danilchap@webrtc.org,deadbeef@webrtc.org,ossu@webrtc.org # Not skipping CQ checks because original CL landed > 1 day ago. Landing with NOTRY because ios64_sim_ios10_dbg bot is broken. Passing all other bots. NOTRY=True Bug: webrtc:8804 Change-Id: I07bd6b1206d5a8d211792ad392842f9ed6c505e9 Reviewed-on: https://webrtc-review.googlesource.com/95280 Commit-Queue: Qingsi Wang <qingsi@webrtc.org> Reviewed-by: Qingsi Wang <qingsi@webrtc.org> Cr-Commit-Position: refs/heads/master@{#24370}
2018-08-21 14:24:26 -07:00
// Counter values when we sent the last report.
int32_t last_report_cumulative_loss_;
int64_t last_report_seq_max_;
// The sample frequency of the last received packet.
int last_payload_type_frequency_;
};
// Thread-safe implementation of StreamStatisticianImplInterface.
class StreamStatisticianLocked : public StreamStatisticianImplInterface {
public:
StreamStatisticianLocked(uint32_t ssrc,
Clock* clock,
int max_reordering_threshold)
: impl_(ssrc, clock, max_reordering_threshold) {}
~StreamStatisticianLocked() override = default;
RtpReceiveStats GetStats() const override {
MutexLock lock(&stream_lock_);
return impl_.GetStats();
}
absl::optional<int> GetFractionLostInPercent() const override {
MutexLock lock(&stream_lock_);
return impl_.GetFractionLostInPercent();
}
StreamDataCounters GetReceiveStreamDataCounters() const override {
MutexLock lock(&stream_lock_);
return impl_.GetReceiveStreamDataCounters();
}
uint32_t BitrateReceived() const override {
MutexLock lock(&stream_lock_);
return impl_.BitrateReceived();
}
void MaybeAppendReportBlockAndReset(
std::vector<rtcp::ReportBlock>& report_blocks) override {
MutexLock lock(&stream_lock_);
impl_.MaybeAppendReportBlockAndReset(report_blocks);
}
void SetMaxReorderingThreshold(int max_reordering_threshold) override {
MutexLock lock(&stream_lock_);
return impl_.SetMaxReorderingThreshold(max_reordering_threshold);
}
void EnableRetransmitDetection(bool enable) override {
MutexLock lock(&stream_lock_);
return impl_.EnableRetransmitDetection(enable);
}
void UpdateCounters(const RtpPacketReceived& packet) override {
MutexLock lock(&stream_lock_);
return impl_.UpdateCounters(packet);
}
private:
mutable Mutex stream_lock_;
StreamStatisticianImpl impl_ RTC_GUARDED_BY(&stream_lock_);
};
// Thread-compatible implementation.
class ReceiveStatisticsImpl : public ReceiveStatistics {
public:
ReceiveStatisticsImpl(
Clock* clock,
std::function<std::unique_ptr<StreamStatisticianImplInterface>(
uint32_t ssrc,
Clock* clock,
int max_reordering_threshold)> stream_statistician_factory);
~ReceiveStatisticsImpl() override = default;
// Implements ReceiveStatisticsProvider.
std::vector<rtcp::ReportBlock> RtcpReportBlocks(size_t max_blocks) override;
// Implements RtpPacketSinkInterface
void OnRtpPacket(const RtpPacketReceived& packet) override;
// Implements ReceiveStatistics.
StreamStatistician* GetStatistician(uint32_t ssrc) const override;
void SetMaxReorderingThreshold(int max_reordering_threshold) override;
void SetMaxReorderingThreshold(uint32_t ssrc,
int max_reordering_threshold) override;
void EnableRetransmitDetection(uint32_t ssrc, bool enable) override;
private:
StreamStatisticianImplInterface* GetOrCreateStatistician(uint32_t ssrc);
Clock* const clock_;
std::function<std::unique_ptr<StreamStatisticianImplInterface>(
uint32_t ssrc,
Clock* clock,
int max_reordering_threshold)>
stream_statistician_factory_;
// The index within `all_ssrcs_` that was last returned.
size_t last_returned_ssrc_idx_;
std::vector<uint32_t> all_ssrcs_;
int max_reordering_threshold_;
flat_map<uint32_t /*ssrc*/, std::unique_ptr<StreamStatisticianImplInterface>>
statisticians_;
};
// Thread-safe implementation wrapping access to ReceiveStatisticsImpl with a
// mutex.
class ReceiveStatisticsLocked : public ReceiveStatistics {
public:
explicit ReceiveStatisticsLocked(
Clock* clock,
std::function<std::unique_ptr<StreamStatisticianImplInterface>(
uint32_t ssrc,
Clock* clock,
int max_reordering_threshold)> stream_statitician_factory)
: impl_(clock, std::move(stream_statitician_factory)) {}
~ReceiveStatisticsLocked() override = default;
std::vector<rtcp::ReportBlock> RtcpReportBlocks(size_t max_blocks) override {
MutexLock lock(&receive_statistics_lock_);
return impl_.RtcpReportBlocks(max_blocks);
}
void OnRtpPacket(const RtpPacketReceived& packet) override {
MutexLock lock(&receive_statistics_lock_);
return impl_.OnRtpPacket(packet);
}
StreamStatistician* GetStatistician(uint32_t ssrc) const override {
MutexLock lock(&receive_statistics_lock_);
return impl_.GetStatistician(ssrc);
}
void SetMaxReorderingThreshold(int max_reordering_threshold) override {
MutexLock lock(&receive_statistics_lock_);
return impl_.SetMaxReorderingThreshold(max_reordering_threshold);
}
void SetMaxReorderingThreshold(uint32_t ssrc,
int max_reordering_threshold) override {
MutexLock lock(&receive_statistics_lock_);
return impl_.SetMaxReorderingThreshold(ssrc, max_reordering_threshold);
}
void EnableRetransmitDetection(uint32_t ssrc, bool enable) override {
MutexLock lock(&receive_statistics_lock_);
return impl_.EnableRetransmitDetection(ssrc, enable);
}
private:
mutable Mutex receive_statistics_lock_;
ReceiveStatisticsImpl impl_ RTC_GUARDED_BY(&receive_statistics_lock_);
};
} // namespace webrtc
#endif // MODULES_RTP_RTCP_SOURCE_RECEIVE_STATISTICS_IMPL_H_