1 /*
2  *  Copyright 2019 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 
11 #include "video/quality_limitation_reason_tracker.h"
12 
13 #include <utility>
14 
15 #include "rtc_base/checks.h"
16 
17 namespace webrtc {
18 
QualityLimitationReasonTracker(Clock * clock)19 QualityLimitationReasonTracker::QualityLimitationReasonTracker(Clock* clock)
20     : clock_(clock),
21       current_reason_(QualityLimitationReason::kNone),
22       current_reason_updated_timestamp_ms_(clock_->TimeInMilliseconds()),
23       durations_ms_({std::make_pair(QualityLimitationReason::kNone, 0),
24                      std::make_pair(QualityLimitationReason::kCpu, 0),
25                      std::make_pair(QualityLimitationReason::kBandwidth, 0),
26                      std::make_pair(QualityLimitationReason::kOther, 0)}) {}
27 
current_reason() const28 QualityLimitationReason QualityLimitationReasonTracker::current_reason() const {
29   return current_reason_;
30 }
31 
SetReason(QualityLimitationReason reason)32 void QualityLimitationReasonTracker::SetReason(QualityLimitationReason reason) {
33   if (reason == current_reason_)
34     return;
35   int64_t now_ms = clock_->TimeInMilliseconds();
36   durations_ms_[current_reason_] +=
37       now_ms - current_reason_updated_timestamp_ms_;
38   current_reason_ = reason;
39   current_reason_updated_timestamp_ms_ = now_ms;
40 }
41 
42 std::map<QualityLimitationReason, int64_t>
DurationsMs() const43 QualityLimitationReasonTracker::DurationsMs() const {
44   std::map<QualityLimitationReason, int64_t> total_durations_ms = durations_ms_;
45   auto it = total_durations_ms.find(current_reason_);
46   RTC_DCHECK(it != total_durations_ms.end());
47   it->second +=
48       clock_->TimeInMilliseconds() - current_reason_updated_timestamp_ms_;
49   return total_durations_ms;
50 }
51 
52 }  // namespace webrtc
53