blob: d9fa16a1267015bc8e86641cb3f56591aaa218b2 [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
Yves Gerey3e707812018-11-28 16:47:49 +010014#include <stddef.h>
15#include <stdint.h>
Jonas Olssona4d87372019-07-05 19:08:33 +020016
asapersson35151f32016-05-02 23:44:01 -070017#include <map>
18#include <memory>
19#include <set>
20
Yves Gerey665174f2018-06-19 15:03:05 +020021#include "call/video_send_stream.h"
Yves Gerey3e707812018-11-28 16:47:49 +010022#include "modules/include/module_common_types_public.h"
Steve Anton10542f22019-01-11 09:11:00 -080023#include "rtc_base/critical_section.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020024#include "rtc_base/thread_annotations.h"
25#include "system_wrappers/include/clock.h"
26#include "video/stats_counter.h"
asapersson35151f32016-05-02 23:44:01 -070027
28namespace webrtc {
29
30class SendDelayStats : public SendPacketObserver {
31 public:
32 explicit SendDelayStats(Clock* clock);
Stefan Holmerdbdb3a02018-07-17 16:03:46 +020033 ~SendDelayStats() override;
asapersson35151f32016-05-02 23:44:01 -070034
35 // Adds the configured ssrcs for the rtp streams.
36 // Stats will be calculated for these streams.
37 void AddSsrcs(const VideoSendStream::Config& config);
38
39 // Called when a packet is sent (leaving socket).
40 bool OnSentPacket(int packet_id, int64_t time_ms);
41
42 protected:
43 // From SendPacketObserver.
44 // Called when a packet is sent to the transport.
45 void OnSendPacket(uint16_t packet_id,
46 int64_t capture_time_ms,
47 uint32_t ssrc) override;
48
49 private:
50 // Map holding sent packets (mapped by sequence number).
51 struct SequenceNumberOlderThan {
52 bool operator()(uint16_t seq1, uint16_t seq2) const {
53 return IsNewerSequenceNumber(seq2, seq1);
54 }
55 };
56 struct Packet {
57 Packet(uint32_t ssrc, int64_t capture_time_ms, int64_t send_time_ms)
58 : ssrc(ssrc),
59 capture_time_ms(capture_time_ms),
60 send_time_ms(send_time_ms) {}
61 uint32_t ssrc;
62 int64_t capture_time_ms;
63 int64_t send_time_ms;
64 };
65 typedef std::map<uint16_t, Packet, SequenceNumberOlderThan> PacketMap;
66
asapersson35151f32016-05-02 23:44:01 -070067 void UpdateHistograms();
68 void RemoveOld(int64_t now, PacketMap* packets)
danilchapa37de392017-09-09 04:17:22 -070069 RTC_EXCLUSIVE_LOCKS_REQUIRED(crit_);
asapersson40f54002016-06-09 00:09:22 -070070 AvgCounter* GetSendDelayCounter(uint32_t ssrc)
danilchapa37de392017-09-09 04:17:22 -070071 RTC_EXCLUSIVE_LOCKS_REQUIRED(crit_);
asapersson35151f32016-05-02 23:44:01 -070072
73 Clock* const clock_;
74 rtc::CriticalSection crit_;
75
danilchapa37de392017-09-09 04:17:22 -070076 PacketMap packets_ RTC_GUARDED_BY(crit_);
77 size_t num_old_packets_ RTC_GUARDED_BY(crit_);
78 size_t num_skipped_packets_ RTC_GUARDED_BY(crit_);
asapersson35151f32016-05-02 23:44:01 -070079
danilchapa37de392017-09-09 04:17:22 -070080 std::set<uint32_t> ssrcs_ RTC_GUARDED_BY(crit_);
asapersson40f54002016-06-09 00:09:22 -070081
82 // Mapped by SSRC.
83 std::map<uint32_t, std::unique_ptr<AvgCounter>> send_delay_counters_
danilchapa37de392017-09-09 04:17:22 -070084 RTC_GUARDED_BY(crit_);
asapersson35151f32016-05-02 23:44:01 -070085};
86
87} // namespace webrtc
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020088#endif // VIDEO_SEND_DELAY_STATS_H_