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