You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

120 lines
3.7 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  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. #include "video/send_delay_stats.h"
  11. #include <utility>
  12. #include "rtc_base/logging.h"
  13. #include "system_wrappers/include/metrics.h"
  14. namespace webrtc {
  15. namespace {
  16. // Packet with a larger delay are removed and excluded from the delay stats.
  17. // Set to larger than max histogram delay which is 10000.
  18. const int64_t kMaxSentPacketDelayMs = 11000;
  19. const size_t kMaxPacketMapSize = 2000;
  20. // Limit for the maximum number of streams to calculate stats for.
  21. const size_t kMaxSsrcMapSize = 50;
  22. const int kMinRequiredPeriodicSamples = 5;
  23. } // namespace
  24. SendDelayStats::SendDelayStats(Clock* clock)
  25. : clock_(clock), num_old_packets_(0), num_skipped_packets_(0) {}
  26. SendDelayStats::~SendDelayStats() {
  27. if (num_old_packets_ > 0 || num_skipped_packets_ > 0) {
  28. RTC_LOG(LS_WARNING) << "Delay stats: number of old packets "
  29. << num_old_packets_ << ", skipped packets "
  30. << num_skipped_packets_ << ". Number of streams "
  31. << send_delay_counters_.size();
  32. }
  33. UpdateHistograms();
  34. }
  35. void SendDelayStats::UpdateHistograms() {
  36. rtc::CritScope lock(&crit_);
  37. for (const auto& it : send_delay_counters_) {
  38. AggregatedStats stats = it.second->GetStats();
  39. if (stats.num_samples >= kMinRequiredPeriodicSamples) {
  40. RTC_HISTOGRAM_COUNTS_10000("WebRTC.Video.SendDelayInMs", stats.average);
  41. RTC_LOG(LS_INFO) << "WebRTC.Video.SendDelayInMs, " << stats.ToString();
  42. }
  43. }
  44. }
  45. void SendDelayStats::AddSsrcs(const VideoSendStream::Config& config) {
  46. rtc::CritScope lock(&crit_);
  47. if (ssrcs_.size() > kMaxSsrcMapSize)
  48. return;
  49. for (const auto& ssrc : config.rtp.ssrcs)
  50. ssrcs_.insert(ssrc);
  51. }
  52. AvgCounter* SendDelayStats::GetSendDelayCounter(uint32_t ssrc) {
  53. const auto& it = send_delay_counters_.find(ssrc);
  54. if (it != send_delay_counters_.end())
  55. return it->second.get();
  56. AvgCounter* counter = new AvgCounter(clock_, nullptr, false);
  57. send_delay_counters_[ssrc].reset(counter);
  58. return counter;
  59. }
  60. void SendDelayStats::OnSendPacket(uint16_t packet_id,
  61. int64_t capture_time_ms,
  62. uint32_t ssrc) {
  63. // Packet sent to transport.
  64. rtc::CritScope lock(&crit_);
  65. if (ssrcs_.find(ssrc) == ssrcs_.end())
  66. return;
  67. int64_t now = clock_->TimeInMilliseconds();
  68. RemoveOld(now, &packets_);
  69. if (packets_.size() > kMaxPacketMapSize) {
  70. ++num_skipped_packets_;
  71. return;
  72. }
  73. packets_.insert(
  74. std::make_pair(packet_id, Packet(ssrc, capture_time_ms, now)));
  75. }
  76. bool SendDelayStats::OnSentPacket(int packet_id, int64_t time_ms) {
  77. // Packet leaving socket.
  78. if (packet_id == -1)
  79. return false;
  80. rtc::CritScope lock(&crit_);
  81. auto it = packets_.find(packet_id);
  82. if (it == packets_.end())
  83. return false;
  84. // TODO(asapersson): Remove SendSideDelayUpdated(), use capture -> sent.
  85. // Elapsed time from send (to transport) -> sent (leaving socket).
  86. int diff_ms = time_ms - it->second.send_time_ms;
  87. GetSendDelayCounter(it->second.ssrc)->Add(diff_ms);
  88. packets_.erase(it);
  89. return true;
  90. }
  91. void SendDelayStats::RemoveOld(int64_t now, PacketMap* packets) {
  92. while (!packets->empty()) {
  93. auto it = packets->begin();
  94. if (now - it->second.capture_time_ms < kMaxSentPacketDelayMs)
  95. break;
  96. packets->erase(it);
  97. ++num_old_packets_;
  98. }
  99. }
  100. } // namespace webrtc