OLD | NEW |
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 bool enable_auto_throttling); |
| 43 |
38 virtual ~VideoCaptureOracle(); | 44 virtual ~VideoCaptureOracle(); |
39 | 45 |
| 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); |
| 50 |
40 // Record a event of type |event|, and decide whether the caller should do a | 51 // 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, | 52 // 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 | 53 // 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. | 54 // oracle's proposal, it should call RecordCapture() to indicate this. |
44 bool ObserveEventAndDecideCapture(Event event, | 55 bool ObserveEventAndDecideCapture(Event event, |
45 const gfx::Rect& damage_rect, | 56 const gfx::Rect& damage_rect, |
46 base::TimeTicks event_time); | 57 base::TimeTicks event_time); |
47 | 58 |
48 // Record the start of a capture. Returns a frame_number to be used with | 59 // Record and update internal state based on whether the frame capture will be |
49 // CompleteCapture(). | 60 // started. |pool_utilization| is a value in the range 0.0 to 1.0 to indicate |
50 int RecordCapture(); | 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); |
51 | 67 |
52 // Notify of the completion of a capture, and whether it was successful. | 68 // Notify of the completion of a capture, and whether it was successful. |
53 // Returns true iff the captured frame should be delivered. |frame_timestamp| | 69 // 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 | 70 // is set to the timestamp that should be provided to the consumer of the |
55 // frame. | 71 // frame. |
56 bool CompleteCapture(int frame_number, | 72 bool CompleteCapture(int frame_number, |
57 bool capture_was_successful, | 73 bool capture_was_successful, |
58 base::TimeTicks* frame_timestamp); | 74 base::TimeTicks* frame_timestamp); |
59 | 75 |
| 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); |
| 84 |
60 base::TimeDelta min_capture_period() const { | 85 base::TimeDelta min_capture_period() const { |
61 return smoothing_sampler_.min_capture_period(); | 86 return smoothing_sampler_.min_capture_period(); |
62 } | 87 } |
63 | 88 |
64 // Returns the oracle's estimate of the duration of the next frame. This | 89 // Returns the oracle's estimate of the duration of the next frame. This |
65 // should be called just after ObserveEventAndDecideCapture(), and will only | 90 // should be called just after ObserveEventAndDecideCapture(), and will only |
66 // be non-zero if the call returned true. | 91 // be non-zero if the call returned true. |
67 base::TimeDelta estimated_frame_duration() const { | 92 base::TimeDelta estimated_frame_duration() const { |
68 return duration_of_next_frame_; | 93 return duration_of_next_frame_; |
69 } | 94 } |
70 | 95 |
| 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_; } |
| 100 |
71 private: | 101 private: |
72 // Retrieve/Assign a frame timestamp by capture |frame_number|. | 102 // Retrieve/Assign a frame timestamp by capture |frame_number|. Only valid |
| 103 // when IsFrameInRecentHistory(frame_number) returns true. |
73 base::TimeTicks GetFrameTimestamp(int frame_number) const; | 104 base::TimeTicks GetFrameTimestamp(int frame_number) const; |
74 void SetFrameTimestamp(int frame_number, base::TimeTicks timestamp); | 105 void SetFrameTimestamp(int frame_number, base::TimeTicks timestamp); |
75 | 106 |
| 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; |
| 110 |
| 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); |
| 116 |
| 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); |
| 120 |
| 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); |
| 125 |
| 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); |
| 131 |
| 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_; |
| 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 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_; |
| 190 |
| 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_; |
106 }; | 195 }; |
107 | 196 |
108 } // namespace media | 197 } // namespace media |
109 | 198 |
110 #endif // MEDIA_CAPTURE_VIDEO_CAPTURE_ORACLE_H_ | 199 #endif // MEDIA_CAPTURE_VIDEO_CAPTURE_ORACLE_H_ |
OLD | NEW |