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 |
38 virtual ~VideoCaptureOracle(); | 43 virtual ~VideoCaptureOracle(); |
39 | 44 |
| 45 // Sets the source content size. This may not have an immediate effect on the |
| 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 (not |
| 61 // the absolute maximum). This method should only be called if the last call |
| 62 // to ObserveEventAndDecideCapture() returned true. The first method returns |
| 63 // the |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 overloaded/under-utilized. |resource_utilization| is a |
| 78 // value in the range 0.0 to 1.0 to indicate the current consumer resource |
| 79 // utilization relative to a sustainable maximum (not the absolute maximum). |
| 80 // This method should only be called for frames where CompleteCapture() |
| 81 // returned true. |
| 82 void RecordConsumerFeedback(int frame_number, double resource_utilization); |
| 83 |
60 base::TimeDelta min_capture_period() const { | 84 base::TimeDelta min_capture_period() const { |
61 return smoothing_sampler_.min_capture_period(); | 85 return smoothing_sampler_.min_capture_period(); |
62 } | 86 } |
63 | 87 |
64 // Returns the oracle's estimate of the duration of the next frame. This | 88 // Returns the oracle's estimate of the duration of the next frame. This |
65 // should be called just after ObserveEventAndDecideCapture(), and will only | 89 // should be called just after ObserveEventAndDecideCapture(), and will only |
66 // be non-zero if the call returned true. | 90 // be non-zero if the call returned true. |
67 base::TimeDelta estimated_frame_duration() const { | 91 base::TimeDelta estimated_frame_duration() const { |
68 return duration_of_next_frame_; | 92 return duration_of_next_frame_; |
69 } | 93 } |
70 | 94 |
| 95 // Returns the capture frame size the client should use. This is updated by |
| 96 // calls to ObserveEventAndDecideCapture(). The oracle prevents too-frequent |
| 97 // changes to the capture size, to avoid stressing the end-to-end pipeline. |
| 98 gfx::Size capture_size() const { return capture_size_; } |
| 99 |
71 private: | 100 private: |
72 // Retrieve/Assign a frame timestamp by capture |frame_number|. | 101 // Retrieve/Assign a frame timestamp by capture |frame_number|. Only valid |
| 102 // when IsFrameInRecentHistory(frame_number) returns true. |
73 base::TimeTicks GetFrameTimestamp(int frame_number) const; | 103 base::TimeTicks GetFrameTimestamp(int frame_number) const; |
74 void SetFrameTimestamp(int frame_number, base::TimeTicks timestamp); | 104 void SetFrameTimestamp(int frame_number, base::TimeTicks timestamp); |
75 | 105 |
| 106 // Returns true if the frame timestamp ring-buffer currently includes a |
| 107 // slot for the given |frame_number|. |
| 108 bool IsFrameInRecentHistory(int frame_number) const; |
| 109 |
| 110 // Queries the ResolutionChooser to update |capture_size_|, and resets all the |
| 111 // FeedbackSignalAccumulator members to stable-state starting values. The |
| 112 // accumulators are reset such that they can only apply feedback updates for |
| 113 // future frames (those with a timestamp after |last_frame_time|). |
| 114 void CommitCaptureSizeAndReset(base::TimeTicks last_frame_time); |
| 115 |
| 116 // Called after a capture or no-capture decision was recorded. This analyzes |
| 117 // current state and may result in a future change to the capture frame size. |
| 118 void AnalyzeAndAdjust(base::TimeTicks analyze_time); |
| 119 |
| 120 // Analyzes current feedback signal accumulators for an indication that the |
| 121 // capture size should be decreased. Returns either a decreased area, or -1 |
| 122 // if no decrease should be made. |
| 123 int AnalyzeForDecreasedArea(base::TimeTicks analyze_time); |
| 124 |
| 125 // Analyzes current feedback signal accumulators for an indication that the |
| 126 // the system has had excellent long-term performance and the capture size |
| 127 // should be increased to improve quality. Returns either an increased area, |
| 128 // or -1 if no increase should be made. |
| 129 int AnalyzeForIncreasedArea(base::TimeTicks analyze_time); |
| 130 |
76 // Incremented every time a paint or update event occurs. | 131 // Incremented every time a paint or update event occurs. |
77 int next_frame_number_; | 132 int next_frame_number_; |
78 | 133 |
79 // Stores the last |event_time| from the last observation/decision. Used to | 134 // Stores the last |event_time| from the last observation/decision. Used to |
80 // sanity-check that event times are monotonically non-decreasing. | 135 // sanity-check that event times are monotonically non-decreasing. |
81 base::TimeTicks last_event_time_[kNumEvents]; | 136 base::TimeTicks last_event_time_[kNumEvents]; |
82 | 137 |
83 // Updated by the last call to ObserveEventAndDecideCapture() with the | 138 // Updated by the last call to ObserveEventAndDecideCapture() with the |
84 // estimated duration of the next frame to sample. This is zero if the method | 139 // estimated duration of the next frame to sample. This is zero if the method |
85 // returned false. | 140 // returned false. |
86 base::TimeDelta duration_of_next_frame_; | 141 base::TimeDelta duration_of_next_frame_; |
87 | 142 |
88 // Stores the frame number from the last successfully delivered frame. | 143 // Stores the frame number from the last successfully delivered frame. |
89 int last_successfully_delivered_frame_number_; | 144 int last_successfully_delivered_frame_number_; |
90 | 145 |
91 // Stores the number of pending frame captures. | 146 // Stores the number of pending frame captures. |
92 int num_frames_pending_; | 147 int num_frames_pending_; |
93 | 148 |
94 // These track present/paint history and propose whether to sample each event | 149 // These track present/paint history and propose whether to sample each event |
95 // for capture. |smoothing_sampler_| uses a "works for all" heuristic, while | 150 // for capture. |smoothing_sampler_| uses a "works for all" heuristic, while |
96 // |content_sampler_| specifically detects animated content (e.g., video | 151 // |content_sampler_| specifically detects animated content (e.g., video |
97 // playback) and decides which events to sample to "lock into" that content. | 152 // playback) and decides which events to sample to "lock into" that content. |
98 SmoothEventSampler smoothing_sampler_; | 153 SmoothEventSampler smoothing_sampler_; |
99 AnimatedContentSampler content_sampler_; | 154 AnimatedContentSampler content_sampler_; |
100 | 155 |
| 156 // Determines video capture frame sizes. |
| 157 CaptureResolutionChooser resolution_chooser_; |
| 158 |
| 159 // The current capture size. |resolution_chooser_| may hold an updated value |
| 160 // because the oracle prevents this size from changing too frequently. This |
| 161 // avoids over-stressing consumers (e.g., when a window is being activly |
| 162 // drag-resized) and allowing the end-to-end system time to stabilize. |
| 163 gfx::Size capture_size_; |
| 164 |
101 // Recent history of frame timestamps proposed by VideoCaptureOracle. This is | 165 // Recent history of frame timestamps proposed by VideoCaptureOracle. This is |
102 // a ring-buffer, and should only be accessed by the Get/SetFrameTimestamp() | 166 // a ring-buffer, and should only be accessed by the Get/SetFrameTimestamp() |
103 // methods. | 167 // methods. |
104 enum { kMaxFrameTimestamps = 16 }; | 168 enum { kMaxFrameTimestamps = 16 }; |
105 base::TimeTicks frame_timestamps_[kMaxFrameTimestamps]; | 169 base::TimeTicks frame_timestamps_[kMaxFrameTimestamps]; |
| 170 |
| 171 // Recent average buffer pool utilization for capture. |
| 172 FeedbackSignalAccumulator buffer_pool_utilization_; |
| 173 |
| 174 // Estimated maximum frame area that currently can be handled by the consumer, |
| 175 // in number of pixels per frame. This is used to adjust the capture size up |
| 176 // or down to a data volume the consumer can handle. Note that some consumers |
| 177 // do not provide feedback, and the analysis logic should account for that. |
| 178 FeedbackSignalAccumulator estimated_capable_area_; |
| 179 |
| 180 // The time of the first analysis which concluded the end-to-end system was |
| 181 // under-utilized. If null, the system is not currently under-utilized. This |
| 182 // is used to determine when a proving period has elapsed that justifies an |
| 183 // increase in capture size. |
| 184 base::TimeTicks start_time_of_underutilization_; |
| 185 |
| 186 // The timestamp of the frame where |content_sampler_| last detected |
| 187 // animation. This determines whether capture size increases will be |
| 188 // aggressive (because content is not animating). |
| 189 base::TimeTicks last_time_animation_was_detected_; |
106 }; | 190 }; |
107 | 191 |
108 } // namespace media | 192 } // namespace media |
109 | 193 |
110 #endif // MEDIA_CAPTURE_VIDEO_CAPTURE_ORACLE_H_ | 194 #endif // MEDIA_CAPTURE_VIDEO_CAPTURE_ORACLE_H_ |
OLD | NEW |