mflodman@webrtc.org | e6168f5 | 2013-06-26 11:23:01 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2013 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 "webrtc/video_engine/overuse_frame_detector.h" |
| 12 | |
| 13 | #include <cassert> |
| 14 | |
| 15 | #include "webrtc/system_wrappers/interface/clock.h" |
| 16 | #include "webrtc/system_wrappers/interface/critical_section_wrapper.h" |
| 17 | |
| 18 | namespace webrtc { |
| 19 | |
| 20 | // TODO(mflodman) Test different thresholds. |
| 21 | const int64_t kProcessIntervalMs = 2000; |
| 22 | const int kOveruseHistoryMs = 5000; |
| 23 | const float kMinEncodeRatio = 29 / 30.0f; |
| 24 | |
| 25 | OveruseFrameDetector::OveruseFrameDetector(Clock* clock, |
| 26 | OveruseObserver* observer) |
| 27 | : crit_(CriticalSectionWrapper::CreateCriticalSection()), |
| 28 | observer_(observer), |
| 29 | clock_(clock), |
| 30 | last_process_time_(clock->TimeInMilliseconds()) { |
| 31 | assert(observer); |
| 32 | } |
| 33 | |
| 34 | OveruseFrameDetector::~OveruseFrameDetector() { |
| 35 | } |
| 36 | |
| 37 | void OveruseFrameDetector::CapturedFrame() { |
| 38 | CriticalSectionScoped cs(crit_.get()); |
| 39 | CleanOldSamples(); |
| 40 | capture_times_.push_back(clock_->TimeInMilliseconds()); |
| 41 | } |
| 42 | |
| 43 | void OveruseFrameDetector::EncodedFrame() { |
| 44 | CriticalSectionScoped cs(crit_.get()); |
| 45 | encode_times_.push_back(clock_->TimeInMilliseconds()); |
| 46 | } |
| 47 | |
| 48 | int32_t OveruseFrameDetector::TimeUntilNextProcess() { |
| 49 | return last_process_time_ + kProcessIntervalMs - clock_->TimeInMilliseconds(); |
| 50 | } |
| 51 | |
| 52 | int32_t OveruseFrameDetector::Process() { |
| 53 | CriticalSectionScoped cs(crit_.get()); |
| 54 | if (clock_->TimeInMilliseconds() < last_process_time_ + kProcessIntervalMs) |
| 55 | return 0; |
| 56 | |
| 57 | last_process_time_ = clock_->TimeInMilliseconds(); |
| 58 | CleanOldSamples(); |
| 59 | |
| 60 | if (encode_times_.size() == 0 || capture_times_.size() == 0) |
| 61 | return 0; |
| 62 | |
| 63 | float encode_ratio = encode_times_.size() / |
| 64 | static_cast<float>(capture_times_.size()); |
| 65 | if (encode_ratio < kMinEncodeRatio) { |
| 66 | observer_->OveruseDetected(); |
| 67 | } |
| 68 | return 0; |
| 69 | } |
| 70 | |
| 71 | void OveruseFrameDetector::CleanOldSamples() { |
| 72 | int64_t time_now = clock_->TimeInMilliseconds(); |
| 73 | while (capture_times_.size() > 0 && |
| 74 | capture_times_.front() < time_now - kOveruseHistoryMs) { |
| 75 | capture_times_.pop_front(); |
| 76 | } |
| 77 | while (encode_times_.size() > 0 && |
| 78 | encode_times_.front() < time_now - kOveruseHistoryMs) { |
| 79 | encode_times_.pop_front(); |
| 80 | } |
| 81 | } |
| 82 | } // namespace webrtc |