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 | #ifndef WEBRTC_VIDEO_ENGINE_OVERUSE_FRAME_DETECTOR_H_ |
| 12 | #define WEBRTC_VIDEO_ENGINE_OVERUSE_FRAME_DETECTOR_H_ |
| 13 | |
| 14 | #include <list> |
mflodman@webrtc.org | d4412fe | 2013-07-31 16:42:21 +0000 | [diff] [blame^] | 15 | #include <map> |
| 16 | #include <utility> |
mflodman@webrtc.org | e6168f5 | 2013-06-26 11:23:01 +0000 | [diff] [blame] | 17 | |
| 18 | #include "webrtc/modules/interface/module.h" |
| 19 | #include "webrtc/system_wrappers/interface/constructor_magic.h" |
| 20 | #include "webrtc/system_wrappers/interface/scoped_ptr.h" |
| 21 | |
| 22 | namespace webrtc { |
| 23 | |
| 24 | class Clock; |
| 25 | class CriticalSectionWrapper; |
mflodman@webrtc.org | 6879c8a | 2013-07-23 11:35:00 +0000 | [diff] [blame] | 26 | class CpuOveruseObserver; |
mflodman@webrtc.org | e6168f5 | 2013-06-26 11:23:01 +0000 | [diff] [blame] | 27 | |
mflodman@webrtc.org | 6879c8a | 2013-07-23 11:35:00 +0000 | [diff] [blame] | 28 | // Use to detect system overuse based on the number of captured frames vs the |
mflodman@webrtc.org | e6168f5 | 2013-06-26 11:23:01 +0000 | [diff] [blame] | 29 | // number of encoded frames. |
| 30 | class OveruseFrameDetector : public Module { |
| 31 | public: |
mflodman@webrtc.org | 6879c8a | 2013-07-23 11:35:00 +0000 | [diff] [blame] | 32 | explicit OveruseFrameDetector(Clock* clock); |
mflodman@webrtc.org | e6168f5 | 2013-06-26 11:23:01 +0000 | [diff] [blame] | 33 | ~OveruseFrameDetector(); |
| 34 | |
mflodman@webrtc.org | d4412fe | 2013-07-31 16:42:21 +0000 | [diff] [blame^] | 35 | // Registers an observer receiving overuse and underuse callbacks. Set |
| 36 | // 'observer' to NULL to disable callbacks. |
mflodman@webrtc.org | 6879c8a | 2013-07-23 11:35:00 +0000 | [diff] [blame] | 37 | void SetObserver(CpuOveruseObserver* observer); |
| 38 | |
mflodman@webrtc.org | d4412fe | 2013-07-31 16:42:21 +0000 | [diff] [blame^] | 39 | // TODO(mflodman): Move to another API? |
| 40 | // Enables usage of encode time to trigger normal usage after an overuse, |
| 41 | // default false. |
| 42 | void set_underuse_encode_timing_enabled(bool enable); |
| 43 | |
| 44 | // Called for each captured frame. |
| 45 | void FrameCaptured(); |
mflodman@webrtc.org | e6168f5 | 2013-06-26 11:23:01 +0000 | [diff] [blame] | 46 | |
| 47 | // Called for every encoded frame. |
mflodman@webrtc.org | d4412fe | 2013-07-31 16:42:21 +0000 | [diff] [blame^] | 48 | void FrameEncoded(int64_t encode_time, size_t width, size_t height); |
mflodman@webrtc.org | e6168f5 | 2013-06-26 11:23:01 +0000 | [diff] [blame] | 49 | |
| 50 | // Implements Module. |
| 51 | virtual int32_t TimeUntilNextProcess(); |
| 52 | virtual int32_t Process(); |
| 53 | |
| 54 | private: |
mflodman@webrtc.org | d4412fe | 2013-07-31 16:42:21 +0000 | [diff] [blame^] | 55 | // All private functions are assumed to be critical section protected. |
| 56 | // Clear samples older than the overuse history. |
| 57 | void RemoveOldSamples(); |
| 58 | // Clears the entire history, including samples still affecting the |
| 59 | // calculations. |
| 60 | void RemoveAllSamples(); |
| 61 | int64_t CalculateAverageEncodeTime() const; |
| 62 | // Returns true and resets calculations and history if a new resolution is |
| 63 | // discovered, false otherwise. |
| 64 | bool MaybeResetResolution(size_t width, size_t height); |
| 65 | |
| 66 | bool IsOverusing(); |
| 67 | bool IsUnderusing(int64_t time_now); |
mflodman@webrtc.org | e6168f5 | 2013-06-26 11:23:01 +0000 | [diff] [blame] | 68 | |
| 69 | // Protecting all members. |
| 70 | scoped_ptr<CriticalSectionWrapper> crit_; |
| 71 | |
| 72 | // Observer getting overuse reports. |
mflodman@webrtc.org | 6879c8a | 2013-07-23 11:35:00 +0000 | [diff] [blame] | 73 | CpuOveruseObserver* observer_; |
mflodman@webrtc.org | e6168f5 | 2013-06-26 11:23:01 +0000 | [diff] [blame] | 74 | |
| 75 | Clock* clock_; |
| 76 | int64_t last_process_time_; |
mflodman@webrtc.org | 6879c8a | 2013-07-23 11:35:00 +0000 | [diff] [blame] | 77 | int64_t last_callback_time_; |
mflodman@webrtc.org | e6168f5 | 2013-06-26 11:23:01 +0000 | [diff] [blame] | 78 | |
mflodman@webrtc.org | d4412fe | 2013-07-31 16:42:21 +0000 | [diff] [blame^] | 79 | // Sorted list of times captured frames were delivered, oldest frame first. |
mflodman@webrtc.org | e6168f5 | 2013-06-26 11:23:01 +0000 | [diff] [blame] | 80 | std::list<int64_t> capture_times_; |
mflodman@webrtc.org | d4412fe | 2013-07-31 16:42:21 +0000 | [diff] [blame^] | 81 | // <Encode report time, time spent encoding the frame>. |
| 82 | typedef std::pair<int64_t, int64_t> EncodeTime; |
| 83 | // Sorted list with oldest frame first. |
| 84 | std::list<EncodeTime> encode_times_; |
mflodman@webrtc.org | e6168f5 | 2013-06-26 11:23:01 +0000 | [diff] [blame] | 85 | |
mflodman@webrtc.org | d4412fe | 2013-07-31 16:42:21 +0000 | [diff] [blame^] | 86 | // True if encode time should be considered to trigger an underuse. |
| 87 | bool underuse_encode_timing_enabled_; |
| 88 | // Number of pixels in the currently encoded resolution. |
| 89 | int num_pixels_; |
| 90 | // Maximum resolution encoded. |
| 91 | int max_num_pixels_; |
| 92 | // <number of pixels, average encode time triggering an overuse>. |
| 93 | std::map<int, int64_t> encode_overuse_times_; |
mflodman@webrtc.org | e6168f5 | 2013-06-26 11:23:01 +0000 | [diff] [blame] | 94 | |
| 95 | DISALLOW_COPY_AND_ASSIGN(OveruseFrameDetector); |
| 96 | }; |
| 97 | |
| 98 | } // namespace webrtc |
| 99 | |
| 100 | #endif // WEBRTC_VIDEO_ENGINE_OVERUSE_FRAME_DETECTOR_H_ |