[AndroidWebViewShell] Replace rebaseline script with a new version using test_runner.py
[chromium-blink-merge.git] / media / capture / video_capture_oracle.h
blob94bdc009bcebc0664db9d1b1a7e2b7af10be1571
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/animated_content_sampler.h"
13 #include "media/capture/capture_resolution_chooser.h"
14 #include "media/capture/feedback_signal_accumulator.h"
15 #include "media/capture/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 current capture size. |resolution_chooser_| may hold an updated value
165 // because the oracle prevents this size from changing too frequently. This
166 // avoids over-stressing consumers (e.g., when a window is being activly
167 // drag-resized) and allowing the end-to-end system time to stabilize.
168 gfx::Size capture_size_;
170 // Recent history of frame timestamps proposed by VideoCaptureOracle. This is
171 // a ring-buffer, and should only be accessed by the Get/SetFrameTimestamp()
172 // methods.
173 enum { kMaxFrameTimestamps = 16 };
174 base::TimeTicks frame_timestamps_[kMaxFrameTimestamps];
176 // Recent average buffer pool utilization for capture.
177 FeedbackSignalAccumulator buffer_pool_utilization_;
179 // Estimated maximum frame area that currently can be handled by the consumer,
180 // in number of pixels per frame. This is used to adjust the capture size up
181 // or down to a data volume the consumer can handle. Note that some consumers
182 // do not provide feedback, and the analysis logic should account for that.
183 FeedbackSignalAccumulator estimated_capable_area_;
185 // The time of the first analysis which concluded the end-to-end system was
186 // under-utilized. If null, the system is not currently under-utilized. This
187 // is used to determine when a proving period has elapsed that justifies an
188 // increase in capture size.
189 base::TimeTicks start_time_of_underutilization_;
191 // The timestamp of the frame where |content_sampler_| last detected
192 // animation. This determines whether capture size increases will be
193 // aggressive (because content is not animating).
194 base::TimeTicks last_time_animation_was_detected_;
197 } // namespace media
199 #endif // MEDIA_CAPTURE_VIDEO_CAPTURE_ORACLE_H_