send_delay_stats.h 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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. #ifndef VIDEO_SEND_DELAY_STATS_H_
  11. #define VIDEO_SEND_DELAY_STATS_H_
  12. #include <stddef.h>
  13. #include <stdint.h>
  14. #include <map>
  15. #include <memory>
  16. #include <set>
  17. #include "call/video_send_stream.h"
  18. #include "modules/include/module_common_types_public.h"
  19. #include "rtc_base/synchronization/mutex.h"
  20. #include "rtc_base/thread_annotations.h"
  21. #include "system_wrappers/include/clock.h"
  22. #include "video/stats_counter.h"
  23. namespace webrtc {
  24. class SendDelayStats : public SendPacketObserver {
  25. public:
  26. explicit SendDelayStats(Clock* clock);
  27. ~SendDelayStats() override;
  28. // Adds the configured ssrcs for the rtp streams.
  29. // Stats will be calculated for these streams.
  30. void AddSsrcs(const VideoSendStream::Config& config);
  31. // Called when a packet is sent (leaving socket).
  32. bool OnSentPacket(int packet_id, int64_t time_ms);
  33. protected:
  34. // From SendPacketObserver.
  35. // Called when a packet is sent to the transport.
  36. void OnSendPacket(uint16_t packet_id,
  37. int64_t capture_time_ms,
  38. uint32_t ssrc) override;
  39. private:
  40. // Map holding sent packets (mapped by sequence number).
  41. struct SequenceNumberOlderThan {
  42. bool operator()(uint16_t seq1, uint16_t seq2) const {
  43. return IsNewerSequenceNumber(seq2, seq1);
  44. }
  45. };
  46. struct Packet {
  47. Packet(uint32_t ssrc, int64_t capture_time_ms, int64_t send_time_ms)
  48. : ssrc(ssrc),
  49. capture_time_ms(capture_time_ms),
  50. send_time_ms(send_time_ms) {}
  51. uint32_t ssrc;
  52. int64_t capture_time_ms;
  53. int64_t send_time_ms;
  54. };
  55. typedef std::map<uint16_t, Packet, SequenceNumberOlderThan> PacketMap;
  56. void UpdateHistograms();
  57. void RemoveOld(int64_t now, PacketMap* packets)
  58. RTC_EXCLUSIVE_LOCKS_REQUIRED(mutex_);
  59. AvgCounter* GetSendDelayCounter(uint32_t ssrc)
  60. RTC_EXCLUSIVE_LOCKS_REQUIRED(mutex_);
  61. Clock* const clock_;
  62. Mutex mutex_;
  63. PacketMap packets_ RTC_GUARDED_BY(mutex_);
  64. size_t num_old_packets_ RTC_GUARDED_BY(mutex_);
  65. size_t num_skipped_packets_ RTC_GUARDED_BY(mutex_);
  66. std::set<uint32_t> ssrcs_ RTC_GUARDED_BY(mutex_);
  67. // Mapped by SSRC.
  68. std::map<uint32_t, std::unique_ptr<AvgCounter>> send_delay_counters_
  69. RTC_GUARDED_BY(mutex_);
  70. };
  71. } // namespace webrtc
  72. #endif // VIDEO_SEND_DELAY_STATS_H_