Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(288)

Side by Side Diff: media/capture/video_capture_oracle.h

Issue 1199593005: Automatic resolution throttling for screen capture pipeline. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@resolution_chooser_ITEM13
Patch Set: Created 5 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #ifndef MEDIA_CAPTURE_VIDEO_CAPTURE_ORACLE_H_ 5 #ifndef MEDIA_CAPTURE_VIDEO_CAPTURE_ORACLE_H_
6 #define MEDIA_CAPTURE_VIDEO_CAPTURE_ORACLE_H_ 6 #define MEDIA_CAPTURE_VIDEO_CAPTURE_ORACLE_H_
7 7
8 #include "base/callback_forward.h" 8 #include "base/callback_forward.h"
9 #include "base/memory/scoped_ptr.h" 9 #include "base/memory/scoped_ptr.h"
10 #include "base/time/time.h" 10 #include "base/time/time.h"
11 #include "media/base/media_export.h" 11 #include "media/base/media_export.h"
12 #include "media/capture/animated_content_sampler.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"
13 #include "media/capture/smooth_event_sampler.h" 15 #include "media/capture/smooth_event_sampler.h"
14 #include "ui/gfx/geometry/rect.h" 16 #include "ui/gfx/geometry/rect.h"
15 17
16 namespace media { 18 namespace media {
17 19
18 // VideoCaptureOracle manages the producer-side throttling of captured frames 20 // VideoCaptureOracle manages the producer-side throttling of captured frames
19 // from a video capture device. It is informed of every update by the device; 21 // from a video capture device. It is informed of every update by the device;
20 // this empowers it to look into the future and decide if a particular frame 22 // this empowers it to look into the future and decide if a particular frame
21 // ought to be captured in order to achieve its target frame rate. 23 // ought to be captured in order to achieve its target frame rate.
22 class MEDIA_EXPORT VideoCaptureOracle { 24 class MEDIA_EXPORT VideoCaptureOracle {
23 public: 25 public:
24 enum Event { 26 enum Event {
25 kTimerPoll, 27 kTimerPoll,
26 kCompositorUpdate, 28 kCompositorUpdate,
27 kNumEvents, 29 kNumEvents,
28 }; 30 };
29 31
30 enum { 32 enum {
31 // The recommended minimum amount of time between calls to 33 // The recommended minimum amount of time between calls to
32 // ObserveEventAndDecideCapture() for the kTimerPoll Event type. Anything 34 // ObserveEventAndDecideCapture() for the kTimerPoll Event type. Anything
33 // lower than this isn't harmful, just wasteful. 35 // lower than this isn't harmful, just wasteful.
34 kMinTimerPollPeriodMillis = 125, // 8 FPS 36 kMinTimerPollPeriodMillis = 125, // 8 FPS
35 }; 37 };
36 38
37 explicit VideoCaptureOracle(base::TimeDelta min_capture_period); 39 VideoCaptureOracle(base::TimeDelta min_capture_period,
40 const gfx::Size& max_frame_size,
41 media::ResolutionChangePolicy resolution_change_policy);
42
38 virtual ~VideoCaptureOracle(); 43 virtual ~VideoCaptureOracle();
39 44
45 // Sets the source content size. This may not have an immediate affect on the
hubbe 2015/06/23 20:04:31 affect -> effect
miu 2015/06/25 20:48:59 Done. On a Dvorak keyboard, that's a typo. ;-)
46 // proposed capture size, as the oracle will prevent too-frequent changes from
47 // occurring.
48 void SetSourceSize(const gfx::Size& source_size);
49
40 // Record a event of type |event|, and decide whether the caller should do a 50 // Record a event of type |event|, and decide whether the caller should do a
41 // frame capture. |damage_rect| is the region of a frame about to be drawn, 51 // frame capture. |damage_rect| is the region of a frame about to be drawn,
42 // and may be an empty Rect, if this is not known. If the caller accepts the 52 // and may be an empty Rect, if this is not known. If the caller accepts the
43 // oracle's proposal, it should call RecordCapture() to indicate this. 53 // oracle's proposal, it should call RecordCapture() to indicate this.
44 bool ObserveEventAndDecideCapture(Event event, 54 bool ObserveEventAndDecideCapture(Event event,
45 const gfx::Rect& damage_rect, 55 const gfx::Rect& damage_rect,
46 base::TimeTicks event_time); 56 base::TimeTicks event_time);
47 57
48 // Record the start of a capture. Returns a frame_number to be used with 58 // Record and update internal state based on whether the frame capture will be
49 // CompleteCapture(). 59 // started. |pool_utilization| is a value in the range 0.0 to 1.0 to indicate
50 int RecordCapture(); 60 // the current buffer pool utilization relative to a sustainable maximum.
61 // This method should only be called if the last call to
62 // ObserveEventAndDecideCapture() returned true. The first method returns the
63 // |frame_number| to be used with CompleteCapture().
64 int RecordCapture(double pool_utilization);
65 void RecordWillNotCapture(double pool_utilization);
51 66
52 // Notify of the completion of a capture, and whether it was successful. 67 // Notify of the completion of a capture, and whether it was successful.
53 // Returns true iff the captured frame should be delivered. |frame_timestamp| 68 // Returns true iff the captured frame should be delivered. |frame_timestamp|
54 // is set to the timestamp that should be provided to the consumer of the 69 // is set to the timestamp that should be provided to the consumer of the
55 // frame. 70 // frame.
56 bool CompleteCapture(int frame_number, 71 bool CompleteCapture(int frame_number,
57 bool capture_was_successful, 72 bool capture_was_successful,
58 base::TimeTicks* frame_timestamp); 73 base::TimeTicks* frame_timestamp);
59 74
75 // Record the resource utilization feedback for a frame that was processed by
76 // the consumer. This allows the oracle to reduce/increase future data volume
77 // if the consumer is over/underloaded. |resource_utilization| is a value in
hubbe 2015/06/23 20:04:31 over/underloaded -> overloaded/underutilized ?
miu 2015/06/25 20:49:00 Done.
78 // the range 0.0 to 1.0 to indicate the current consumer resource utilization
79 // relative to a sustainable maximum. This method should only be called for
80 // frames where CompleteCapture() returned true.
81 void RecordConsumerFeedback(int frame_number, double resource_utilization);
82
60 base::TimeDelta min_capture_period() const { 83 base::TimeDelta min_capture_period() const {
61 return smoothing_sampler_.min_capture_period(); 84 return smoothing_sampler_.min_capture_period();
62 } 85 }
63 86
64 // Returns the oracle's estimate of the duration of the next frame. This 87 // Returns the oracle's estimate of the duration of the next frame. This
65 // should be called just after ObserveEventAndDecideCapture(), and will only 88 // should be called just after ObserveEventAndDecideCapture(), and will only
66 // be non-zero if the call returned true. 89 // be non-zero if the call returned true.
67 base::TimeDelta estimated_frame_duration() const { 90 base::TimeDelta estimated_frame_duration() const {
68 return duration_of_next_frame_; 91 return duration_of_next_frame_;
69 } 92 }
70 93
94 // Returns the capture frame size the client should use. This is updated by
95 // calls to ObserveEventAndDecideCapture(). The oracle prevents too-frequent
96 // changes to the capture size, to avoid stressing the end-to-end pipeline.
97 gfx::Size capture_size() const { return capture_size_; }
98
71 private: 99 private:
72 // Retrieve/Assign a frame timestamp by capture |frame_number|. 100 // Retrieve/Assign a frame timestamp by capture |frame_number|. Only valid
101 // when IsFrameInRecentHistory(frame_number) returns true.
73 base::TimeTicks GetFrameTimestamp(int frame_number) const; 102 base::TimeTicks GetFrameTimestamp(int frame_number) const;
74 void SetFrameTimestamp(int frame_number, base::TimeTicks timestamp); 103 void SetFrameTimestamp(int frame_number, base::TimeTicks timestamp);
75 104
105 // Returns true if the frame timestamp ring-buffer currently includes a
106 // slot for the given |frame_number|.
107 bool IsFrameInRecentHistory(int frame_number) const;
108
109 // Queries the ResolutionChooser to update |capture_size_|, and resets all the
110 // FeedbackSignalAccumulator members to stable-state starting values. The
111 // accumulators are reset such that they can only apply feedback updates for
112 // future frames (those with a timestamp after |last_frame_time|).
113 void CommitCaptureSizeAndReset(base::TimeTicks last_frame_time);
114
115 // Resets the accumulators that are used to determine when to increase the
116 // capture size. This method is called whenever something happens that should
117 // re-start the "proving period," to prove that the capture size can be safely
118 // increased.
119 void ResetPessimisticAccumulators(base::TimeTicks event_time);
120
121 // Called after a capture or no-capture decision was recorded. This analyzes
122 // current state and may result in a future change to the capture frame size.
123 void AnalyzeAndAdjust(base::TimeTicks analyze_time);
124
125 // Analyzes current feedback signal accumulators for an indication that the
126 // capture size should be decreased. Returns either a decreased area, or -1
127 // if no decrease should be made.
128 int AnalyzeForDecreasedArea(base::TimeTicks analyze_time);
129
130 // Analyzes current feedback signal accumulators for an indication that the
131 // the system has had excellent long-term performance and the capture size
132 // should be increased to improve quality. Returns either an increased area,
133 // or -1 if no increase should be made.
134 int AnalyzeForIncreasedArea(base::TimeTicks analyze_time);
135
76 // Incremented every time a paint or update event occurs. 136 // Incremented every time a paint or update event occurs.
77 int next_frame_number_; 137 int next_frame_number_;
78 138
79 // Stores the last |event_time| from the last observation/decision. Used to 139 // Stores the last |event_time| from the last observation/decision. Used to
80 // sanity-check that event times are monotonically non-decreasing. 140 // sanity-check that event times are monotonically non-decreasing.
81 base::TimeTicks last_event_time_[kNumEvents]; 141 base::TimeTicks last_event_time_[kNumEvents];
82 142
83 // Updated by the last call to ObserveEventAndDecideCapture() with the 143 // Updated by the last call to ObserveEventAndDecideCapture() with the
84 // estimated duration of the next frame to sample. This is zero if the method 144 // estimated duration of the next frame to sample. This is zero if the method
85 // returned false. 145 // returned false.
86 base::TimeDelta duration_of_next_frame_; 146 base::TimeDelta duration_of_next_frame_;
87 147
88 // Stores the frame number from the last successfully delivered frame. 148 // Stores the frame number from the last successfully delivered frame.
89 int last_successfully_delivered_frame_number_; 149 int last_successfully_delivered_frame_number_;
90 150
91 // Stores the number of pending frame captures. 151 // Stores the number of pending frame captures.
92 int num_frames_pending_; 152 int num_frames_pending_;
93 153
94 // These track present/paint history and propose whether to sample each event 154 // These track present/paint history and propose whether to sample each event
95 // for capture. |smoothing_sampler_| uses a "works for all" heuristic, while 155 // for capture. |smoothing_sampler_| uses a "works for all" heuristic, while
96 // |content_sampler_| specifically detects animated content (e.g., video 156 // |content_sampler_| specifically detects animated content (e.g., video
97 // playback) and decides which events to sample to "lock into" that content. 157 // playback) and decides which events to sample to "lock into" that content.
98 SmoothEventSampler smoothing_sampler_; 158 SmoothEventSampler smoothing_sampler_;
99 AnimatedContentSampler content_sampler_; 159 AnimatedContentSampler content_sampler_;
100 160
161 // Determines video capture frame sizes.
162 CaptureResolutionChooser resolution_chooser_;
163
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_;
169
101 // Recent history of frame timestamps proposed by VideoCaptureOracle. This is 170 // Recent history of frame timestamps proposed by VideoCaptureOracle. This is
102 // a ring-buffer, and should only be accessed by the Get/SetFrameTimestamp() 171 // a ring-buffer, and should only be accessed by the Get/SetFrameTimestamp()
103 // methods. 172 // methods.
104 enum { kMaxFrameTimestamps = 16 }; 173 enum { kMaxFrameTimestamps = 16 };
105 base::TimeTicks frame_timestamps_[kMaxFrameTimestamps]; 174 base::TimeTicks frame_timestamps_[kMaxFrameTimestamps];
175
176 // Recent average buffer pool utilization for capture.
177 FeedbackSignalAccumulator buffer_pool_utilization_;
178
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_;
184
185 // The timestamp of the frame where |content_sampler_| last detected
186 // animation. This determines whether capture size increases will be
187 // aggressive (because content is not animating).
188 base::TimeTicks last_time_animation_was_detected_;
189
190 // These accumulate the same data points as |buffer_pool_utilization_| and
191 // |estimated_capable_area_|, but accumulate a much longer-term time-weighted
192 // average. These are used to judge whether capture size increases are
193 // warranted.
194 FeedbackSignalAccumulator pessimistic_pool_utilization_;
195 FeedbackSignalAccumulator pessimistic_capable_area_;
106 }; 196 };
107 197
108 } // namespace media 198 } // namespace media
109 199
110 #endif // MEDIA_CAPTURE_VIDEO_CAPTURE_ORACLE_H_ 200 #endif // MEDIA_CAPTURE_VIDEO_CAPTURE_ORACLE_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698