Updating XTBs based on .GRDs from branch master
[chromium-blink-merge.git] / media / capture / content / video_capture_oracle.h
blobaff9745295118330e1406f168b5ce4f3526e929c
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #ifndef MEDIA_CAPTURE_VIDEO_CAPTURE_ORACLE_H_
6 #define MEDIA_CAPTURE_VIDEO_CAPTURE_ORACLE_H_
8 #include "base/callback_forward.h"
9 #include "base/memory/scoped_ptr.h"
10 #include "base/time/time.h"
11 #include "media/base/media_export.h"
12 #include "media/capture/content/animated_content_sampler.h"
13 #include "media/capture/content/capture_resolution_chooser.h"
14 #include "media/capture/content/feedback_signal_accumulator.h"
15 #include "media/capture/content/smooth_event_sampler.h"
16 #include "ui/gfx/geometry/rect.h"
18 namespace media {
20 // VideoCaptureOracle manages the producer-side throttling of captured frames
21 // from a video capture device. It is informed of every update by the device;
22 // this empowers it to look into the future and decide if a particular frame
23 // ought to be captured in order to achieve its target frame rate.
24 class MEDIA_EXPORT VideoCaptureOracle {
25 public:
26 enum Event {
27 kTimerPoll,
28 kCompositorUpdate,
29 kNumEvents,
32 enum {
33 // The recommended minimum amount of time between calls to
34 // ObserveEventAndDecideCapture() for the kTimerPoll Event type. Anything
35 // lower than this isn't harmful, just wasteful.
36 kMinTimerPollPeriodMillis = 125, // 8 FPS
39 VideoCaptureOracle(base::TimeDelta min_capture_period,
40 const gfx::Size& max_frame_size,
41 media::ResolutionChangePolicy resolution_change_policy,
42 bool enable_auto_throttling);
44 virtual ~VideoCaptureOracle();
46 // Sets the source content size. This may not have an immediate effect on the
47 // proposed capture size, as the oracle will prevent too-frequent changes from
48 // occurring.
49 void SetSourceSize(const gfx::Size& source_size);
51 // Record a event of type |event|, and decide whether the caller should do a
52 // frame capture. |damage_rect| is the region of a frame about to be drawn,
53 // and may be an empty Rect, if this is not known. If the caller accepts the
54 // oracle's proposal, it should call RecordCapture() to indicate this.
55 bool ObserveEventAndDecideCapture(Event event,
56 const gfx::Rect& damage_rect,
57 base::TimeTicks event_time);
59 // Record and update internal state based on whether the frame capture will be
60 // started. |pool_utilization| is a value in the range 0.0 to 1.0 to indicate
61 // the current buffer pool utilization relative to a sustainable maximum (not
62 // the absolute maximum). This method should only be called if the last call
63 // to ObserveEventAndDecideCapture() returned true. The first method returns
64 // the |frame_number| to be used with CompleteCapture().
65 int RecordCapture(double pool_utilization);
66 void RecordWillNotCapture(double pool_utilization);
68 // Notify of the completion of a capture, and whether it was successful.
69 // Returns true iff the captured frame should be delivered. |frame_timestamp|
70 // is set to the timestamp that should be provided to the consumer of the
71 // frame.
72 bool CompleteCapture(int frame_number,
73 bool capture_was_successful,
74 base::TimeTicks* frame_timestamp);
76 // Record the resource utilization feedback for a frame that was processed by
77 // the consumer. This allows the oracle to reduce/increase future data volume
78 // if the consumer is overloaded/under-utilized. |resource_utilization| is a
79 // value in the range 0.0 to 1.0 to indicate the current consumer resource
80 // utilization relative to a sustainable maximum (not the absolute maximum).
81 // This method should only be called for frames where CompleteCapture()
82 // returned true.
83 void RecordConsumerFeedback(int frame_number, double resource_utilization);
85 base::TimeDelta min_capture_period() const {
86 return smoothing_sampler_.min_capture_period();
89 // Returns the oracle's estimate of the duration of the next frame. This
90 // should be called just after ObserveEventAndDecideCapture(), and will only
91 // be non-zero if the call returned true.
92 base::TimeDelta estimated_frame_duration() const {
93 return duration_of_next_frame_;
96 // Returns the capture frame size the client should use. This is updated by
97 // calls to ObserveEventAndDecideCapture(). The oracle prevents too-frequent
98 // changes to the capture size, to avoid stressing the end-to-end pipeline.
99 gfx::Size capture_size() const { return capture_size_; }
101 private:
102 // Retrieve/Assign a frame timestamp by capture |frame_number|. Only valid
103 // when IsFrameInRecentHistory(frame_number) returns true.
104 base::TimeTicks GetFrameTimestamp(int frame_number) const;
105 void SetFrameTimestamp(int frame_number, base::TimeTicks timestamp);
107 // Returns true if the frame timestamp ring-buffer currently includes a
108 // slot for the given |frame_number|.
109 bool IsFrameInRecentHistory(int frame_number) const;
111 // Queries the ResolutionChooser to update |capture_size_|, and resets all the
112 // FeedbackSignalAccumulator members to stable-state starting values. The
113 // accumulators are reset such that they can only apply feedback updates for
114 // future frames (those with a timestamp after |last_frame_time|).
115 void CommitCaptureSizeAndReset(base::TimeTicks last_frame_time);
117 // Called after a capture or no-capture decision was recorded. This analyzes
118 // current state and may result in a future change to the capture frame size.
119 void AnalyzeAndAdjust(base::TimeTicks analyze_time);
121 // Analyzes current feedback signal accumulators for an indication that the
122 // capture size should be decreased. Returns either a decreased area, or -1
123 // if no decrease should be made.
124 int AnalyzeForDecreasedArea(base::TimeTicks analyze_time);
126 // Analyzes current feedback signal accumulators for an indication that the
127 // the system has had excellent long-term performance and the capture size
128 // should be increased to improve quality. Returns either an increased area,
129 // or -1 if no increase should be made.
130 int AnalyzeForIncreasedArea(base::TimeTicks analyze_time);
132 // Set to false to prevent the oracle from automatically adjusting the capture
133 // size in response to end-to-end utilization.
134 const bool auto_throttling_enabled_;
136 // Incremented every time a paint or update event occurs.
137 int next_frame_number_;
139 // Stores the last |event_time| from the last observation/decision. Used to
140 // sanity-check that event times are monotonically non-decreasing.
141 base::TimeTicks last_event_time_[kNumEvents];
143 // Updated by the last call to ObserveEventAndDecideCapture() with the
144 // estimated duration of the next frame to sample. This is zero if the method
145 // returned false.
146 base::TimeDelta duration_of_next_frame_;
148 // Stores the frame number from the last successfully delivered frame.
149 int last_successfully_delivered_frame_number_;
151 // Stores the number of pending frame captures.
152 int num_frames_pending_;
154 // These track present/paint history and propose whether to sample each event
155 // for capture. |smoothing_sampler_| uses a "works for all" heuristic, while
156 // |content_sampler_| specifically detects animated content (e.g., video
157 // playback) and decides which events to sample to "lock into" that content.
158 SmoothEventSampler smoothing_sampler_;
159 AnimatedContentSampler content_sampler_;
161 // Determines video capture frame sizes.
162 CaptureResolutionChooser resolution_chooser_;
164 // The timestamp of the frame just before the last call to SetSourceSize().
165 base::TimeTicks source_size_change_time_;
167 // The current capture size. |resolution_chooser_| may hold an updated value
168 // because the oracle prevents this size from changing too frequently. This
169 // avoids over-stressing consumers (e.g., when a window is being activly
170 // drag-resized) and allowing the end-to-end system time to stabilize.
171 gfx::Size capture_size_;
173 // Recent history of frame timestamps proposed by VideoCaptureOracle. This is
174 // a ring-buffer, and should only be accessed by the Get/SetFrameTimestamp()
175 // methods.
176 enum { kMaxFrameTimestamps = 16 };
177 base::TimeTicks frame_timestamps_[kMaxFrameTimestamps];
179 // Recent average buffer pool utilization for capture.
180 FeedbackSignalAccumulator buffer_pool_utilization_;
182 // Estimated maximum frame area that currently can be handled by the consumer,
183 // in number of pixels per frame. This is used to adjust the capture size up
184 // or down to a data volume the consumer can handle. Note that some consumers
185 // do not provide feedback, and the analysis logic should account for that.
186 FeedbackSignalAccumulator estimated_capable_area_;
188 // The time of the first analysis which concluded the end-to-end system was
189 // under-utilized. If null, the system is not currently under-utilized. This
190 // is used to determine when a proving period has elapsed that justifies an
191 // increase in capture size.
192 base::TimeTicks start_time_of_underutilization_;
194 // The timestamp of the frame where |content_sampler_| last detected
195 // animation. This determines whether capture size increases will be
196 // aggressive (because content is not animating).
197 base::TimeTicks last_time_animation_was_detected_;
200 } // namespace media
202 #endif // MEDIA_CAPTURE_VIDEO_CAPTURE_ORACLE_H_