blob: 0221d857d2b645a3c266a5296c23fca2558f5085 [file] [log] [blame]
wu@webrtc.org822fbd82013-08-15 23:38:54 +00001/*
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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#ifndef MODULES_RTP_RTCP_SOURCE_RECEIVE_STATISTICS_IMPL_H_
12#define MODULES_RTP_RTCP_SOURCE_RECEIVE_STATISTICS_IMPL_H_
wu@webrtc.org822fbd82013-08-15 23:38:54 +000013
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020014#include "modules/rtp_rtcp/include/receive_statistics.h"
wu@webrtc.org822fbd82013-08-15 23:38:54 +000015
16#include <algorithm>
danilchapb8b6fbb2015-12-10 05:05:27 -080017#include <map>
danilchap0bc84232017-08-11 08:12:54 -070018#include <vector>
wu@webrtc.org822fbd82013-08-15 23:38:54 +000019
Danil Chapovalovb438b5a2018-12-05 14:55:46 +000020#include "absl/types/optional.h"
21#include "modules/include/module_common_types_public.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020022#include "rtc_base/criticalsection.h"
23#include "rtc_base/rate_statistics.h"
Danil Chapovalovebb50c22018-11-22 14:04:02 +010024#include "rtc_base/thread_annotations.h"
wu@webrtc.org822fbd82013-08-15 23:38:54 +000025
26namespace webrtc {
27
Niels Möllerdbb988b2018-11-15 08:05:16 +010028class StreamStatisticianImpl : public StreamStatistician,
29 public RtpPacketSinkInterface {
wu@webrtc.org822fbd82013-08-15 23:38:54 +000030 public:
danilchapec86be02017-08-14 05:51:02 -070031 StreamStatisticianImpl(uint32_t ssrc,
32 Clock* clock,
Niels Möller5304a322018-08-27 13:27:05 +020033 bool enable_retransmit_detection,
Danil Chapovalovebb50c22018-11-22 14:04:02 +010034 int max_reordering_threshold,
sprang@webrtc.org0e932572014-01-23 10:00:39 +000035 RtcpStatisticsCallback* rtcp_callback,
36 StreamDataCountersCallback* rtp_callback);
Danil Chapovalov2a5ce2b2018-02-07 09:38:31 +010037 ~StreamStatisticianImpl() override;
wu@webrtc.org822fbd82013-08-15 23:38:54 +000038
Qingsi Wang2370b082018-08-21 14:24:26 -070039 // |reset| here and in next method restarts calculation of fraction_lost stat.
40 bool GetStatistics(RtcpStatistics* statistics, bool reset) override;
Danil Chapovalovc5267d22017-09-18 13:57:19 +020041 bool GetActiveStatisticsAndReset(RtcpStatistics* statistics);
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000042 void GetDataCounters(size_t* bytes_received,
43 uint32_t* packets_received) const override;
44 void GetReceiveStreamDataCounters(
45 StreamDataCounters* data_counters) const override;
46 uint32_t BitrateReceived() const override;
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +000047
Niels Möllerdbb988b2018-11-15 08:05:16 +010048 // Implements RtpPacketSinkInterface
49 void OnRtpPacket(const RtpPacketReceived& packet) override;
50
51 void FecPacketReceived(const RtpPacketReceived& packet);
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +000052 void SetMaxReorderingThreshold(int max_reordering_threshold);
Niels Möller5304a322018-08-27 13:27:05 +020053 void EnableRetransmitDetection(bool enable);
wu@webrtc.org822fbd82013-08-15 23:38:54 +000054
55 private:
Danil Chapovalov44727b42018-11-22 11:28:45 +010056 bool IsRetransmitOfOldPacket(const RtpPacketReceived& packet,
57 int64_t now_ms) const
Niels Möllerb615d1a2018-08-27 12:32:21 +020058 RTC_EXCLUSIVE_LOCKS_REQUIRED(stream_lock_);
Qingsi Wang2370b082018-08-21 14:24:26 -070059 RtcpStatistics CalculateRtcpStatistics()
danilchap56359be2017-09-07 07:53:45 -070060 RTC_EXCLUSIVE_LOCKS_REQUIRED(stream_lock_);
Danil Chapovalov856cf222018-11-26 10:20:01 +010061 void UpdateJitter(const RtpPacketReceived& packet, int64_t receive_time_ms)
Niels Möllerb615d1a2018-08-27 12:32:21 +020062 RTC_EXCLUSIVE_LOCKS_REQUIRED(stream_lock_);
Danil Chapovalovb438b5a2018-12-05 14:55:46 +000063 // Updates StreamStatistician for out of order packets.
64 // Returns true if packet considered to be out of order.
65 bool UpdateOutOfOrder(const RtpPacketReceived& packet,
66 int64_t sequence_number,
67 int64_t now_ms)
68 RTC_EXCLUSIVE_LOCKS_REQUIRED(stream_lock_);
69 // Updates StreamStatistician for incoming packets.
Danil Chapovalov44727b42018-11-22 11:28:45 +010070 StreamDataCounters UpdateCounters(const RtpPacketReceived& packet);
Danil Chapovalovb438b5a2018-12-05 14:55:46 +000071 // Checks if this StreamStatistician received any rtp packets.
72 bool ReceivedRtpPacket() const RTC_EXCLUSIVE_LOCKS_REQUIRED(stream_lock_) {
73 return received_seq_max_ >= 0;
74 }
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +000075
danilchapec86be02017-08-14 05:51:02 -070076 const uint32_t ssrc_;
sprangcd349d92016-07-13 09:11:28 -070077 Clock* const clock_;
danilchap7c9426c2016-04-14 03:05:31 -070078 rtc::CriticalSection stream_lock_;
Niels Möllerb615d1a2018-08-27 12:32:21 +020079 RateStatistics incoming_bitrate_ RTC_GUARDED_BY(&stream_lock_);
80 // In number of packets or sequence numbers.
81 int max_reordering_threshold_ RTC_GUARDED_BY(&stream_lock_);
Niels Möller5304a322018-08-27 13:27:05 +020082 bool enable_retransmit_detection_ RTC_GUARDED_BY(&stream_lock_);
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +000083
wu@webrtc.org822fbd82013-08-15 23:38:54 +000084 // Stats on received RTP packets.
Niels Möllerb615d1a2018-08-27 12:32:21 +020085 uint32_t jitter_q4_ RTC_GUARDED_BY(&stream_lock_);
86 uint32_t cumulative_loss_ RTC_GUARDED_BY(&stream_lock_);
wu@webrtc.org822fbd82013-08-15 23:38:54 +000087
Niels Möllerb615d1a2018-08-27 12:32:21 +020088 int64_t last_receive_time_ms_ RTC_GUARDED_BY(&stream_lock_);
Niels Möllerb615d1a2018-08-27 12:32:21 +020089 uint32_t last_received_timestamp_ RTC_GUARDED_BY(&stream_lock_);
Danil Chapovalovb438b5a2018-12-05 14:55:46 +000090 SequenceNumberUnwrapper seq_unwrapper_ RTC_GUARDED_BY(&stream_lock_);
91 int64_t received_seq_first_ RTC_GUARDED_BY(&stream_lock_);
92 int64_t received_seq_max_ RTC_GUARDED_BY(&stream_lock_);
93 // Assume that the other side restarted when there are two sequential packets
94 // with large jump from received_seq_max_.
95 absl::optional<uint16_t> received_seq_out_of_order_
96 RTC_GUARDED_BY(&stream_lock_);
wu@webrtc.org822fbd82013-08-15 23:38:54 +000097
98 // Current counter values.
Niels Möllerb615d1a2018-08-27 12:32:21 +020099 StreamDataCounters receive_counters_ RTC_GUARDED_BY(&stream_lock_);
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000100
Qingsi Wang2370b082018-08-21 14:24:26 -0700101 // Counter values when we sent the last report.
Niels Möllerb615d1a2018-08-27 12:32:21 +0200102 uint32_t last_report_inorder_packets_ RTC_GUARDED_BY(&stream_lock_);
103 uint32_t last_report_old_packets_ RTC_GUARDED_BY(&stream_lock_);
Danil Chapovalovb438b5a2018-12-05 14:55:46 +0000104 int64_t last_report_seq_max_ RTC_GUARDED_BY(&stream_lock_);
Niels Möllerb615d1a2018-08-27 12:32:21 +0200105 RtcpStatistics last_reported_statistics_ RTC_GUARDED_BY(&stream_lock_);
sprang@webrtc.org54ae4ff2013-12-19 13:26:02 +0000106
danilchapec86be02017-08-14 05:51:02 -0700107 // stream_lock_ shouldn't be held when calling callbacks.
sprang@webrtc.org54ae4ff2013-12-19 13:26:02 +0000108 RtcpStatisticsCallback* const rtcp_callback_;
sprang@webrtc.org0e932572014-01-23 10:00:39 +0000109 StreamDataCountersCallback* const rtp_callback_;
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000110};
111
Danil Chapovalov8ce0d2b2018-11-23 11:03:25 +0100112class ReceiveStatisticsImpl : public ReceiveStatistics {
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000113 public:
Danil Chapovalov8ce0d2b2018-11-23 11:03:25 +0100114 ReceiveStatisticsImpl(Clock* clock,
115 RtcpStatisticsCallback* rtcp_callback,
116 StreamDataCountersCallback* rtp_callback);
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000117
Danil Chapovalov2a5ce2b2018-02-07 09:38:31 +0100118 ~ReceiveStatisticsImpl() override;
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000119
Niels Möllerdbb988b2018-11-15 08:05:16 +0100120 // Implements ReceiveStatisticsProvider.
danilchap0bc84232017-08-11 08:12:54 -0700121 std::vector<rtcp::ReportBlock> RtcpReportBlocks(size_t max_blocks) override;
122
Niels Möllerdbb988b2018-11-15 08:05:16 +0100123 // Implements RtpPacketSinkInterface
Niels Möller1f3206c2018-09-14 08:26:32 +0200124 void OnRtpPacket(const RtpPacketReceived& packet) override;
125
Niels Möllerdbb988b2018-11-15 08:05:16 +0100126 // Implements ReceiveStatistics.
Niels Möller1f3206c2018-09-14 08:26:32 +0200127 void FecPacketReceived(const RtpPacketReceived& packet) override;
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000128 StreamStatistician* GetStatistician(uint32_t ssrc) const override;
129 void SetMaxReorderingThreshold(int max_reordering_threshold) override;
Niels Möller5304a322018-08-27 13:27:05 +0200130 void EnableRetransmitDetection(uint32_t ssrc, bool enable) override;
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000131
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000132 private:
sprangcd349d92016-07-13 09:11:28 -0700133 Clock* const clock_;
danilchap7c9426c2016-04-14 03:05:31 -0700134 rtc::CriticalSection receive_statistics_lock_;
Danil Chapovalovd1996b72018-01-16 11:07:18 +0100135 uint32_t last_returned_ssrc_;
Danil Chapovalovebb50c22018-11-22 14:04:02 +0100136 int max_reordering_threshold_ RTC_GUARDED_BY(receive_statistics_lock_);
137 std::map<uint32_t, StreamStatisticianImpl*> statisticians_
138 RTC_GUARDED_BY(receive_statistics_lock_);
sprang@webrtc.org54ae4ff2013-12-19 13:26:02 +0000139
Danil Chapovalov8ce0d2b2018-11-23 11:03:25 +0100140 RtcpStatisticsCallback* const rtcp_stats_callback_;
141 StreamDataCountersCallback* const rtp_stats_callback_;
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000142};
143} // namespace webrtc
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200144#endif // MODULES_RTP_RTCP_SOURCE_RECEIVE_STATISTICS_IMPL_H_