blob: 7772266902f4669d12d7186c894a2f2f6a3e414b [file] [log] [blame]
mflodman@webrtc.orge6168f52013-06-26 11:23:01 +00001/*
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.orgd4412fe2013-07-31 16:42:21 +000015#include <map>
16#include <utility>
mflodman@webrtc.orge6168f52013-06-26 11:23:01 +000017
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
22namespace webrtc {
23
24class Clock;
25class CriticalSectionWrapper;
mflodman@webrtc.org6879c8a2013-07-23 11:35:00 +000026class CpuOveruseObserver;
mflodman@webrtc.orge6168f52013-06-26 11:23:01 +000027
mflodman@webrtc.org6879c8a2013-07-23 11:35:00 +000028// Use to detect system overuse based on the number of captured frames vs the
mflodman@webrtc.orge6168f52013-06-26 11:23:01 +000029// number of encoded frames.
30class OveruseFrameDetector : public Module {
31 public:
mflodman@webrtc.org6879c8a2013-07-23 11:35:00 +000032 explicit OveruseFrameDetector(Clock* clock);
mflodman@webrtc.orge6168f52013-06-26 11:23:01 +000033 ~OveruseFrameDetector();
34
mflodman@webrtc.orgd4412fe2013-07-31 16:42:21 +000035 // Registers an observer receiving overuse and underuse callbacks. Set
36 // 'observer' to NULL to disable callbacks.
mflodman@webrtc.org6879c8a2013-07-23 11:35:00 +000037 void SetObserver(CpuOveruseObserver* observer);
38
mflodman@webrtc.orgd4412fe2013-07-31 16:42:21 +000039 // 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.orge6168f52013-06-26 11:23:01 +000046
47 // Called for every encoded frame.
mflodman@webrtc.orgd4412fe2013-07-31 16:42:21 +000048 void FrameEncoded(int64_t encode_time, size_t width, size_t height);
mflodman@webrtc.orge6168f52013-06-26 11:23:01 +000049
50 // Implements Module.
51 virtual int32_t TimeUntilNextProcess();
52 virtual int32_t Process();
53
54 private:
mflodman@webrtc.orgd4412fe2013-07-31 16:42:21 +000055 // 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.orge6168f52013-06-26 11:23:01 +000068
69 // Protecting all members.
70 scoped_ptr<CriticalSectionWrapper> crit_;
71
72 // Observer getting overuse reports.
mflodman@webrtc.org6879c8a2013-07-23 11:35:00 +000073 CpuOveruseObserver* observer_;
mflodman@webrtc.orge6168f52013-06-26 11:23:01 +000074
75 Clock* clock_;
76 int64_t last_process_time_;
mflodman@webrtc.org6879c8a2013-07-23 11:35:00 +000077 int64_t last_callback_time_;
mflodman@webrtc.orge6168f52013-06-26 11:23:01 +000078
mflodman@webrtc.orgd4412fe2013-07-31 16:42:21 +000079 // Sorted list of times captured frames were delivered, oldest frame first.
mflodman@webrtc.orge6168f52013-06-26 11:23:01 +000080 std::list<int64_t> capture_times_;
mflodman@webrtc.orgd4412fe2013-07-31 16:42:21 +000081 // <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.orge6168f52013-06-26 11:23:01 +000085
mflodman@webrtc.orgd4412fe2013-07-31 16:42:21 +000086 // 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.orge6168f52013-06-26 11:23:01 +000094
95 DISALLOW_COPY_AND_ASSIGN(OveruseFrameDetector);
96};
97
98} // namespace webrtc
99
100#endif // WEBRTC_VIDEO_ENGINE_OVERUSE_FRAME_DETECTOR_H_