asapersson | 35151f3 | 2016-05-03 06:44:01 | [diff] [blame] | 1 | /* |
| 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 Bonadei | 92ea95e | 2017-09-15 04:47:31 | [diff] [blame] | 11 | #ifndef VIDEO_SEND_DELAY_STATS_H_ |
| 12 | #define VIDEO_SEND_DELAY_STATS_H_ |
asapersson | 35151f3 | 2016-05-03 06:44:01 | [diff] [blame] | 13 | |
Yves Gerey | 3e70781 | 2018-11-28 15:47:49 | [diff] [blame] | 14 | #include <stddef.h> |
| 15 | #include <stdint.h> |
Jonas Olsson | a4d8737 | 2019-07-05 17:08:33 | [diff] [blame] | 16 | |
asapersson | 35151f3 | 2016-05-03 06:44:01 | [diff] [blame] | 17 | #include <map> |
| 18 | #include <memory> |
| 19 | #include <set> |
| 20 | |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 21 | #include "call/video_send_stream.h" |
Yves Gerey | 3e70781 | 2018-11-28 15:47:49 | [diff] [blame] | 22 | #include "modules/include/module_common_types_public.h" |
Markus Handell | a376518 | 2020-07-08 11:13:32 | [diff] [blame] | 23 | #include "rtc_base/synchronization/mutex.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 04:47:31 | [diff] [blame] | 24 | #include "rtc_base/thread_annotations.h" |
| 25 | #include "system_wrappers/include/clock.h" |
| 26 | #include "video/stats_counter.h" |
asapersson | 35151f3 | 2016-05-03 06:44:01 | [diff] [blame] | 27 | |
| 28 | namespace webrtc { |
| 29 | |
Tomas Gunnarsson | eb9c3f2 | 2021-04-19 10:53:09 | [diff] [blame] | 30 | // Used to collect delay stats for video streams. The class gets callbacks |
| 31 | // from more than one threads and internally uses a mutex for data access |
| 32 | // synchronization. |
| 33 | // TODO(bugs.webrtc.org/11993): OnSendPacket and OnSentPacket will eventually |
| 34 | // be called consistently on the same thread. Once we're there, we should be |
| 35 | // able to avoid locking (at least for the fast path). |
asapersson | 35151f3 | 2016-05-03 06:44:01 | [diff] [blame] | 36 | class SendDelayStats : public SendPacketObserver { |
| 37 | public: |
| 38 | explicit SendDelayStats(Clock* clock); |
Stefan Holmer | dbdb3a0 | 2018-07-17 14:03:46 | [diff] [blame] | 39 | ~SendDelayStats() override; |
asapersson | 35151f3 | 2016-05-03 06:44:01 | [diff] [blame] | 40 | |
| 41 | // Adds the configured ssrcs for the rtp streams. |
| 42 | // Stats will be calculated for these streams. |
| 43 | void AddSsrcs(const VideoSendStream::Config& config); |
| 44 | |
| 45 | // Called when a packet is sent (leaving socket). |
| 46 | bool OnSentPacket(int packet_id, int64_t time_ms); |
| 47 | |
| 48 | protected: |
| 49 | // From SendPacketObserver. |
| 50 | // Called when a packet is sent to the transport. |
| 51 | void OnSendPacket(uint16_t packet_id, |
| 52 | int64_t capture_time_ms, |
| 53 | uint32_t ssrc) override; |
| 54 | |
| 55 | private: |
| 56 | // Map holding sent packets (mapped by sequence number). |
| 57 | struct SequenceNumberOlderThan { |
| 58 | bool operator()(uint16_t seq1, uint16_t seq2) const { |
| 59 | return IsNewerSequenceNumber(seq2, seq1); |
| 60 | } |
| 61 | }; |
| 62 | struct Packet { |
| 63 | Packet(uint32_t ssrc, int64_t capture_time_ms, int64_t send_time_ms) |
| 64 | : ssrc(ssrc), |
| 65 | capture_time_ms(capture_time_ms), |
| 66 | send_time_ms(send_time_ms) {} |
| 67 | uint32_t ssrc; |
| 68 | int64_t capture_time_ms; |
| 69 | int64_t send_time_ms; |
| 70 | }; |
| 71 | typedef std::map<uint16_t, Packet, SequenceNumberOlderThan> PacketMap; |
| 72 | |
asapersson | 35151f3 | 2016-05-03 06:44:01 | [diff] [blame] | 73 | void UpdateHistograms(); |
| 74 | void RemoveOld(int64_t now, PacketMap* packets) |
Markus Handell | a376518 | 2020-07-08 11:13:32 | [diff] [blame] | 75 | RTC_EXCLUSIVE_LOCKS_REQUIRED(mutex_); |
asapersson | 40f5400 | 2016-06-09 07:09:22 | [diff] [blame] | 76 | AvgCounter* GetSendDelayCounter(uint32_t ssrc) |
Markus Handell | a376518 | 2020-07-08 11:13:32 | [diff] [blame] | 77 | RTC_EXCLUSIVE_LOCKS_REQUIRED(mutex_); |
asapersson | 35151f3 | 2016-05-03 06:44:01 | [diff] [blame] | 78 | |
| 79 | Clock* const clock_; |
Markus Handell | a376518 | 2020-07-08 11:13:32 | [diff] [blame] | 80 | Mutex mutex_; |
asapersson | 35151f3 | 2016-05-03 06:44:01 | [diff] [blame] | 81 | |
Markus Handell | a376518 | 2020-07-08 11:13:32 | [diff] [blame] | 82 | PacketMap packets_ RTC_GUARDED_BY(mutex_); |
| 83 | size_t num_old_packets_ RTC_GUARDED_BY(mutex_); |
| 84 | size_t num_skipped_packets_ RTC_GUARDED_BY(mutex_); |
asapersson | 35151f3 | 2016-05-03 06:44:01 | [diff] [blame] | 85 | |
Markus Handell | a376518 | 2020-07-08 11:13:32 | [diff] [blame] | 86 | std::set<uint32_t> ssrcs_ RTC_GUARDED_BY(mutex_); |
asapersson | 40f5400 | 2016-06-09 07:09:22 | [diff] [blame] | 87 | |
| 88 | // Mapped by SSRC. |
| 89 | std::map<uint32_t, std::unique_ptr<AvgCounter>> send_delay_counters_ |
Markus Handell | a376518 | 2020-07-08 11:13:32 | [diff] [blame] | 90 | RTC_GUARDED_BY(mutex_); |
asapersson | 35151f3 | 2016-05-03 06:44:01 | [diff] [blame] | 91 | }; |
| 92 | |
| 93 | } // namespace webrtc |
Mirko Bonadei | 92ea95e | 2017-09-15 04:47:31 | [diff] [blame] | 94 | #endif // VIDEO_SEND_DELAY_STATS_H_ |