blob: f6aec69ffb937a9fb50acbf3961bc012f6c8e64a [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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020020#include "rtc_base/criticalsection.h"
21#include "rtc_base/rate_statistics.h"
Danil Chapovalovebb50c22018-11-22 14:04:02 +010022#include "rtc_base/thread_annotations.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020023#include "system_wrappers/include/ntp_time.h"
wu@webrtc.org822fbd82013-08-15 23:38:54 +000024
25namespace webrtc {
26
Niels Möllerdbb988b2018-11-15 08:05:16 +010027class StreamStatisticianImpl : public StreamStatistician,
28 public RtpPacketSinkInterface {
wu@webrtc.org822fbd82013-08-15 23:38:54 +000029 public:
danilchapec86be02017-08-14 05:51:02 -070030 StreamStatisticianImpl(uint32_t ssrc,
31 Clock* clock,
Niels Möller5304a322018-08-27 13:27:05 +020032 bool enable_retransmit_detection,
Danil Chapovalovebb50c22018-11-22 14:04:02 +010033 int max_reordering_threshold,
sprang@webrtc.org0e932572014-01-23 10:00:39 +000034 RtcpStatisticsCallback* rtcp_callback,
35 StreamDataCountersCallback* rtp_callback);
Danil Chapovalov2a5ce2b2018-02-07 09:38:31 +010036 ~StreamStatisticianImpl() override;
wu@webrtc.org822fbd82013-08-15 23:38:54 +000037
Qingsi Wang2370b082018-08-21 14:24:26 -070038 // |reset| here and in next method restarts calculation of fraction_lost stat.
39 bool GetStatistics(RtcpStatistics* statistics, bool reset) override;
Danil Chapovalovc5267d22017-09-18 13:57:19 +020040 bool GetActiveStatisticsAndReset(RtcpStatistics* statistics);
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000041 void GetDataCounters(size_t* bytes_received,
42 uint32_t* packets_received) const override;
43 void GetReceiveStreamDataCounters(
44 StreamDataCounters* data_counters) const override;
45 uint32_t BitrateReceived() const override;
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +000046
Niels Möllerdbb988b2018-11-15 08:05:16 +010047 // Implements RtpPacketSinkInterface
48 void OnRtpPacket(const RtpPacketReceived& packet) override;
49
50 void FecPacketReceived(const RtpPacketReceived& packet);
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +000051 void SetMaxReorderingThreshold(int max_reordering_threshold);
Niels Möller5304a322018-08-27 13:27:05 +020052 void EnableRetransmitDetection(bool enable);
wu@webrtc.org822fbd82013-08-15 23:38:54 +000053
54 private:
Danil Chapovalov44727b42018-11-22 11:28:45 +010055 bool IsRetransmitOfOldPacket(const RtpPacketReceived& packet,
56 int64_t now_ms) const
Niels Möllerb615d1a2018-08-27 12:32:21 +020057 RTC_EXCLUSIVE_LOCKS_REQUIRED(stream_lock_);
Qingsi Wang2370b082018-08-21 14:24:26 -070058 RtcpStatistics CalculateRtcpStatistics()
danilchap56359be2017-09-07 07:53:45 -070059 RTC_EXCLUSIVE_LOCKS_REQUIRED(stream_lock_);
Niels Möllerdbb988b2018-11-15 08:05:16 +010060 void UpdateJitter(const RtpPacketReceived& packet, NtpTime receive_time)
Niels Möllerb615d1a2018-08-27 12:32:21 +020061 RTC_EXCLUSIVE_LOCKS_REQUIRED(stream_lock_);
Danil Chapovalov44727b42018-11-22 11:28:45 +010062 StreamDataCounters UpdateCounters(const RtpPacketReceived& packet);
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +000063
danilchapec86be02017-08-14 05:51:02 -070064 const uint32_t ssrc_;
sprangcd349d92016-07-13 09:11:28 -070065 Clock* const clock_;
danilchap7c9426c2016-04-14 03:05:31 -070066 rtc::CriticalSection stream_lock_;
Niels Möllerb615d1a2018-08-27 12:32:21 +020067 RateStatistics incoming_bitrate_ RTC_GUARDED_BY(&stream_lock_);
68 // In number of packets or sequence numbers.
69 int max_reordering_threshold_ RTC_GUARDED_BY(&stream_lock_);
Niels Möller5304a322018-08-27 13:27:05 +020070 bool enable_retransmit_detection_ RTC_GUARDED_BY(&stream_lock_);
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +000071
wu@webrtc.org822fbd82013-08-15 23:38:54 +000072 // Stats on received RTP packets.
Niels Möllerb615d1a2018-08-27 12:32:21 +020073 uint32_t jitter_q4_ RTC_GUARDED_BY(&stream_lock_);
74 uint32_t cumulative_loss_ RTC_GUARDED_BY(&stream_lock_);
wu@webrtc.org822fbd82013-08-15 23:38:54 +000075
Niels Möllerb615d1a2018-08-27 12:32:21 +020076 int64_t last_receive_time_ms_ RTC_GUARDED_BY(&stream_lock_);
77 NtpTime last_receive_time_ntp_ RTC_GUARDED_BY(&stream_lock_);
78 uint32_t last_received_timestamp_ RTC_GUARDED_BY(&stream_lock_);
79 uint16_t received_seq_first_ RTC_GUARDED_BY(&stream_lock_);
80 uint16_t received_seq_max_ RTC_GUARDED_BY(&stream_lock_);
81 uint16_t received_seq_wraps_ RTC_GUARDED_BY(&stream_lock_);
wu@webrtc.org822fbd82013-08-15 23:38:54 +000082
83 // Current counter values.
Niels Möllerb615d1a2018-08-27 12:32:21 +020084 StreamDataCounters receive_counters_ RTC_GUARDED_BY(&stream_lock_);
wu@webrtc.org822fbd82013-08-15 23:38:54 +000085
Qingsi Wang2370b082018-08-21 14:24:26 -070086 // Counter values when we sent the last report.
Niels Möllerb615d1a2018-08-27 12:32:21 +020087 uint32_t last_report_inorder_packets_ RTC_GUARDED_BY(&stream_lock_);
88 uint32_t last_report_old_packets_ RTC_GUARDED_BY(&stream_lock_);
89 uint16_t last_report_seq_max_ RTC_GUARDED_BY(&stream_lock_);
90 RtcpStatistics last_reported_statistics_ RTC_GUARDED_BY(&stream_lock_);
sprang@webrtc.org54ae4ff2013-12-19 13:26:02 +000091
danilchapec86be02017-08-14 05:51:02 -070092 // stream_lock_ shouldn't be held when calling callbacks.
sprang@webrtc.org54ae4ff2013-12-19 13:26:02 +000093 RtcpStatisticsCallback* const rtcp_callback_;
sprang@webrtc.org0e932572014-01-23 10:00:39 +000094 StreamDataCountersCallback* const rtp_callback_;
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +000095};
96
sprang@webrtc.org54ae4ff2013-12-19 13:26:02 +000097class ReceiveStatisticsImpl : public ReceiveStatistics,
sprang@webrtc.org0e932572014-01-23 10:00:39 +000098 public RtcpStatisticsCallback,
99 public StreamDataCountersCallback {
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000100 public:
101 explicit ReceiveStatisticsImpl(Clock* clock);
102
Danil Chapovalov2a5ce2b2018-02-07 09:38:31 +0100103 ~ReceiveStatisticsImpl() override;
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000104
Niels Möllerdbb988b2018-11-15 08:05:16 +0100105 // Implements ReceiveStatisticsProvider.
danilchap0bc84232017-08-11 08:12:54 -0700106 std::vector<rtcp::ReportBlock> RtcpReportBlocks(size_t max_blocks) override;
107
Niels Möllerdbb988b2018-11-15 08:05:16 +0100108 // Implements RtpPacketSinkInterface
Niels Möller1f3206c2018-09-14 08:26:32 +0200109 void OnRtpPacket(const RtpPacketReceived& packet) override;
110
Niels Möllerdbb988b2018-11-15 08:05:16 +0100111 // Implements ReceiveStatistics.
Niels Möller1f3206c2018-09-14 08:26:32 +0200112 void FecPacketReceived(const RtpPacketReceived& packet) override;
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000113 StreamStatistician* GetStatistician(uint32_t ssrc) const override;
114 void SetMaxReorderingThreshold(int max_reordering_threshold) override;
Niels Möller5304a322018-08-27 13:27:05 +0200115 void EnableRetransmitDetection(uint32_t ssrc, bool enable) override;
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000116
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000117 void RegisterRtcpStatisticsCallback(
118 RtcpStatisticsCallback* callback) override;
sprang@webrtc.org54ae4ff2013-12-19 13:26:02 +0000119
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000120 void RegisterRtpStatisticsCallback(
121 StreamDataCountersCallback* callback) override;
sprang@webrtc.org54ae4ff2013-12-19 13:26:02 +0000122
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000123 private:
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000124 void StatisticsUpdated(const RtcpStatistics& statistics,
125 uint32_t ssrc) override;
126 void CNameChanged(const char* cname, uint32_t ssrc) override;
127 void DataCountersUpdated(const StreamDataCounters& counters,
128 uint32_t ssrc) override;
sprang@webrtc.org0e932572014-01-23 10:00:39 +0000129
sprangcd349d92016-07-13 09:11:28 -0700130 Clock* const clock_;
danilchap7c9426c2016-04-14 03:05:31 -0700131 rtc::CriticalSection receive_statistics_lock_;
Danil Chapovalovd1996b72018-01-16 11:07:18 +0100132 uint32_t last_returned_ssrc_;
Danil Chapovalovebb50c22018-11-22 14:04:02 +0100133 int max_reordering_threshold_ RTC_GUARDED_BY(receive_statistics_lock_);
134 std::map<uint32_t, StreamStatisticianImpl*> statisticians_
135 RTC_GUARDED_BY(receive_statistics_lock_);
sprang@webrtc.org54ae4ff2013-12-19 13:26:02 +0000136
137 RtcpStatisticsCallback* rtcp_stats_callback_;
sprang@webrtc.org0e932572014-01-23 10:00:39 +0000138 StreamDataCountersCallback* rtp_stats_callback_;
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000139};
140} // namespace webrtc
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200141#endif // MODULES_RTP_RTCP_SOURCE_RECEIVE_STATISTICS_IMPL_H_