blob: 9b9e9214d9c8b5dd230c0a4de4220ac18785c71a [file] [log] [blame]
asapersson35151f32016-05-02 23:44:01 -07001/*
2 * Copyright (c) 2016 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 VIDEO_SEND_DELAY_STATS_H_
12#define VIDEO_SEND_DELAY_STATS_H_
asapersson35151f32016-05-02 23:44:01 -070013
14#include <map>
15#include <memory>
16#include <set>
17
Yves Gerey665174f2018-06-19 15:03:05 +020018#include "call/video_send_stream.h"
Mirko Bonadei71207422017-09-15 13:58:09 +020019#include "common_types.h" // NOLINT(build/include)
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020020#include "modules/include/module_common_types.h"
21#include "rtc_base/criticalsection.h"
22#include "rtc_base/thread_annotations.h"
23#include "system_wrappers/include/clock.h"
24#include "video/stats_counter.h"
asapersson35151f32016-05-02 23:44:01 -070025
26namespace webrtc {
27
28class SendDelayStats : public SendPacketObserver {
29 public:
30 explicit SendDelayStats(Clock* clock);
31 virtual ~SendDelayStats();
32
33 // Adds the configured ssrcs for the rtp streams.
34 // Stats will be calculated for these streams.
35 void AddSsrcs(const VideoSendStream::Config& config);
36
37 // Called when a packet is sent (leaving socket).
38 bool OnSentPacket(int packet_id, int64_t time_ms);
39
40 protected:
41 // From SendPacketObserver.
42 // Called when a packet is sent to the transport.
43 void OnSendPacket(uint16_t packet_id,
44 int64_t capture_time_ms,
45 uint32_t ssrc) override;
46
47 private:
48 // Map holding sent packets (mapped by sequence number).
49 struct SequenceNumberOlderThan {
50 bool operator()(uint16_t seq1, uint16_t seq2) const {
51 return IsNewerSequenceNumber(seq2, seq1);
52 }
53 };
54 struct Packet {
55 Packet(uint32_t ssrc, int64_t capture_time_ms, int64_t send_time_ms)
56 : ssrc(ssrc),
57 capture_time_ms(capture_time_ms),
58 send_time_ms(send_time_ms) {}
59 uint32_t ssrc;
60 int64_t capture_time_ms;
61 int64_t send_time_ms;
62 };
63 typedef std::map<uint16_t, Packet, SequenceNumberOlderThan> PacketMap;
64
asapersson35151f32016-05-02 23:44:01 -070065 void UpdateHistograms();
66 void RemoveOld(int64_t now, PacketMap* packets)
danilchapa37de392017-09-09 04:17:22 -070067 RTC_EXCLUSIVE_LOCKS_REQUIRED(crit_);
asapersson40f54002016-06-09 00:09:22 -070068 AvgCounter* GetSendDelayCounter(uint32_t ssrc)
danilchapa37de392017-09-09 04:17:22 -070069 RTC_EXCLUSIVE_LOCKS_REQUIRED(crit_);
asapersson35151f32016-05-02 23:44:01 -070070
71 Clock* const clock_;
72 rtc::CriticalSection crit_;
73
danilchapa37de392017-09-09 04:17:22 -070074 PacketMap packets_ RTC_GUARDED_BY(crit_);
75 size_t num_old_packets_ RTC_GUARDED_BY(crit_);
76 size_t num_skipped_packets_ RTC_GUARDED_BY(crit_);
asapersson35151f32016-05-02 23:44:01 -070077
danilchapa37de392017-09-09 04:17:22 -070078 std::set<uint32_t> ssrcs_ RTC_GUARDED_BY(crit_);
asapersson40f54002016-06-09 00:09:22 -070079
80 // Mapped by SSRC.
81 std::map<uint32_t, std::unique_ptr<AvgCounter>> send_delay_counters_
danilchapa37de392017-09-09 04:17:22 -070082 RTC_GUARDED_BY(crit_);
asapersson35151f32016-05-02 23:44:01 -070083};
84
85} // namespace webrtc
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020086#endif // VIDEO_SEND_DELAY_STATS_H_