receive_statistics_impl.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /*
  2. * Copyright (c) 2013 The WebRTC project authors. All Rights Reserved.
  3. *
  4. * Use of this source code is governed by a BSD-style license
  5. * that can be found in the LICENSE file in the root of the source
  6. * tree. An additional intellectual property rights grant can be found
  7. * in the file PATENTS. All contributing project authors may
  8. * be found in the AUTHORS file in the root of the source tree.
  9. */
  10. #ifndef MODULES_RTP_RTCP_SOURCE_RECEIVE_STATISTICS_IMPL_H_
  11. #define MODULES_RTP_RTCP_SOURCE_RECEIVE_STATISTICS_IMPL_H_
  12. #include <algorithm>
  13. #include <map>
  14. #include <vector>
  15. #include "absl/types/optional.h"
  16. #include "modules/include/module_common_types_public.h"
  17. #include "modules/rtp_rtcp/include/receive_statistics.h"
  18. #include "rtc_base/rate_statistics.h"
  19. #include "rtc_base/synchronization/mutex.h"
  20. #include "rtc_base/thread_annotations.h"
  21. namespace webrtc {
  22. class StreamStatisticianImpl : public StreamStatistician {
  23. public:
  24. StreamStatisticianImpl(uint32_t ssrc,
  25. Clock* clock,
  26. int max_reordering_threshold);
  27. ~StreamStatisticianImpl() override;
  28. RtpReceiveStats GetStats() const override;
  29. bool GetActiveStatisticsAndReset(RtcpStatistics* statistics);
  30. absl::optional<int> GetFractionLostInPercent() const override;
  31. StreamDataCounters GetReceiveStreamDataCounters() const override;
  32. uint32_t BitrateReceived() const override;
  33. void SetMaxReorderingThreshold(int max_reordering_threshold);
  34. void EnableRetransmitDetection(bool enable);
  35. // Updates StreamStatistician for incoming packets.
  36. void UpdateCounters(const RtpPacketReceived& packet);
  37. private:
  38. bool IsRetransmitOfOldPacket(const RtpPacketReceived& packet,
  39. int64_t now_ms) const
  40. RTC_EXCLUSIVE_LOCKS_REQUIRED(stream_lock_);
  41. RtcpStatistics CalculateRtcpStatistics()
  42. RTC_EXCLUSIVE_LOCKS_REQUIRED(stream_lock_);
  43. void UpdateJitter(const RtpPacketReceived& packet, int64_t receive_time_ms)
  44. RTC_EXCLUSIVE_LOCKS_REQUIRED(stream_lock_);
  45. // Updates StreamStatistician for out of order packets.
  46. // Returns true if packet considered to be out of order.
  47. bool UpdateOutOfOrder(const RtpPacketReceived& packet,
  48. int64_t sequence_number,
  49. int64_t now_ms)
  50. RTC_EXCLUSIVE_LOCKS_REQUIRED(stream_lock_);
  51. // Checks if this StreamStatistician received any rtp packets.
  52. bool ReceivedRtpPacket() const RTC_EXCLUSIVE_LOCKS_REQUIRED(stream_lock_) {
  53. return received_seq_first_ >= 0;
  54. }
  55. const uint32_t ssrc_;
  56. Clock* const clock_;
  57. mutable Mutex stream_lock_;
  58. RateStatistics incoming_bitrate_ RTC_GUARDED_BY(&stream_lock_);
  59. // In number of packets or sequence numbers.
  60. int max_reordering_threshold_ RTC_GUARDED_BY(&stream_lock_);
  61. bool enable_retransmit_detection_ RTC_GUARDED_BY(&stream_lock_);
  62. // Stats on received RTP packets.
  63. uint32_t jitter_q4_ RTC_GUARDED_BY(&stream_lock_);
  64. // Cumulative loss according to RFC 3550, which may be negative (and often is,
  65. // if packets are reordered and there are non-RTX retransmissions).
  66. int32_t cumulative_loss_ RTC_GUARDED_BY(&stream_lock_);
  67. // Offset added to outgoing rtcp reports, to make ensure that the reported
  68. // cumulative loss is non-negative. Reports with negative values confuse some
  69. // senders, in particular, our own loss-based bandwidth estimator.
  70. int32_t cumulative_loss_rtcp_offset_ RTC_GUARDED_BY(&stream_lock_);
  71. int64_t last_receive_time_ms_ RTC_GUARDED_BY(&stream_lock_);
  72. uint32_t last_received_timestamp_ RTC_GUARDED_BY(&stream_lock_);
  73. SequenceNumberUnwrapper seq_unwrapper_ RTC_GUARDED_BY(&stream_lock_);
  74. int64_t received_seq_first_ RTC_GUARDED_BY(&stream_lock_);
  75. int64_t received_seq_max_ RTC_GUARDED_BY(&stream_lock_);
  76. // Assume that the other side restarted when there are two sequential packets
  77. // with large jump from received_seq_max_.
  78. absl::optional<uint16_t> received_seq_out_of_order_
  79. RTC_GUARDED_BY(&stream_lock_);
  80. // Current counter values.
  81. StreamDataCounters receive_counters_ RTC_GUARDED_BY(&stream_lock_);
  82. // Counter values when we sent the last report.
  83. int32_t last_report_cumulative_loss_ RTC_GUARDED_BY(&stream_lock_);
  84. int64_t last_report_seq_max_ RTC_GUARDED_BY(&stream_lock_);
  85. };
  86. class ReceiveStatisticsImpl : public ReceiveStatistics {
  87. public:
  88. explicit ReceiveStatisticsImpl(Clock* clock);
  89. ~ReceiveStatisticsImpl() override;
  90. // Implements ReceiveStatisticsProvider.
  91. std::vector<rtcp::ReportBlock> RtcpReportBlocks(size_t max_blocks) override;
  92. // Implements RtpPacketSinkInterface
  93. void OnRtpPacket(const RtpPacketReceived& packet) override;
  94. // Implements ReceiveStatistics.
  95. // Note: More specific return type for use in the implementation.
  96. StreamStatisticianImpl* GetStatistician(uint32_t ssrc) const override;
  97. void SetMaxReorderingThreshold(int max_reordering_threshold) override;
  98. void SetMaxReorderingThreshold(uint32_t ssrc,
  99. int max_reordering_threshold) override;
  100. void EnableRetransmitDetection(uint32_t ssrc, bool enable) override;
  101. private:
  102. StreamStatisticianImpl* GetOrCreateStatistician(uint32_t ssrc);
  103. Clock* const clock_;
  104. mutable Mutex receive_statistics_lock_;
  105. uint32_t last_returned_ssrc_;
  106. int max_reordering_threshold_ RTC_GUARDED_BY(receive_statistics_lock_);
  107. std::map<uint32_t, StreamStatisticianImpl*> statisticians_
  108. RTC_GUARDED_BY(receive_statistics_lock_);
  109. };
  110. } // namespace webrtc
  111. #endif // MODULES_RTP_RTCP_SOURCE_RECEIVE_STATISTICS_IMPL_H_