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 #include "content/browser/media/capture/video_capture_oracle.h"
7 #include "base/debug/trace_event.h"
13 // This value controls how many redundant, timer-base captures occur when the
14 // content is static. Redundantly capturing the same frame allows iterative
15 // quality enhancement, and also allows the buffer to fill in "buffered mode".
17 // TODO(nick): Controlling this here is a hack and a layering violation, since
18 // it's a strategy specific to the WebRTC consumer, and probably just papers
19 // over some frame dropping and quality bugs. It should either be controlled at
20 // a higher level, or else redundant frame generation should be pushed down
21 // further into the WebRTC encoding stack.
22 const int kNumRedundantCapturesOfStaticContent
= 200;
24 } // anonymous namespace
26 VideoCaptureOracle::VideoCaptureOracle(base::TimeDelta capture_period
,
27 bool events_are_reliable
)
28 : capture_period_(capture_period
),
30 last_delivered_frame_number_(0),
31 sampler_(capture_period_
,
33 kNumRedundantCapturesOfStaticContent
) {}
35 bool VideoCaptureOracle::ObserveEventAndDecideCapture(
37 base::TimeTicks event_time
) {
38 // Record |event| and decide whether it's a good time to capture.
39 const bool content_is_dirty
= (event
== kCompositorUpdate
||
40 event
== kSoftwarePaint
);
42 if (content_is_dirty
) {
44 should_sample
= sampler_
.AddEventAndConsiderSampling(event_time
);
46 should_sample
= sampler_
.IsOverdueForSamplingAt(event_time
);
51 int VideoCaptureOracle::RecordCapture() {
52 sampler_
.RecordSample();
56 bool VideoCaptureOracle::CompleteCapture(int frame_number
,
57 base::TimeTicks timestamp
) {
58 // Drop frame if previous frame number is higher or we're trying to deliver
59 // a frame with the same timestamp.
60 if (last_delivered_frame_number_
> frame_number
||
61 last_delivered_frame_timestamp_
== timestamp
) {
62 LOG(ERROR
) << "Frame with same timestamp or out of order delivery. "
67 if (last_delivered_frame_timestamp_
> timestamp
) {
68 // We should not get here unless time was adjusted backwards.
69 LOG(ERROR
) << "Frame with past timestamp (" << timestamp
.ToInternalValue()
73 last_delivered_frame_number_
= frame_number
;
74 last_delivered_frame_timestamp_
= timestamp
;
79 SmoothEventSampler::SmoothEventSampler(base::TimeDelta capture_period
,
80 bool events_are_reliable
,
81 int redundant_capture_goal
)
82 : events_are_reliable_(events_are_reliable
),
83 capture_period_(capture_period
),
84 redundant_capture_goal_(redundant_capture_goal
),
85 token_bucket_capacity_(capture_period
+ capture_period
/ 2),
86 overdue_sample_count_(0),
87 token_bucket_(token_bucket_capacity_
) {
88 DCHECK_GT(capture_period_
.InMicroseconds(), 0);
91 bool SmoothEventSampler::AddEventAndConsiderSampling(
92 base::TimeTicks event_time
) {
93 DCHECK(!event_time
.is_null());
95 // Add tokens to the bucket based on advancement in time. Then, re-bound the
96 // number of tokens in the bucket. Overflow occurs when there is too much
97 // time between events (a common case), or when RecordSample() is not being
98 // called often enough (a bug). On the other hand, if RecordSample() is being
99 // called too often (e.g., as a reaction to IsOverdueForSamplingAt()), the
100 // bucket will underflow.
101 if (!current_event_
.is_null()) {
102 if (current_event_
< event_time
) {
103 token_bucket_
+= event_time
- current_event_
;
104 if (token_bucket_
> token_bucket_capacity_
)
105 token_bucket_
= token_bucket_capacity_
;
107 // Side note: If the system clock is reset, causing |current_event_| to be
108 // greater than |event_time|, everything here will simply gracefully adjust.
109 if (token_bucket_
< base::TimeDelta())
110 token_bucket_
= base::TimeDelta();
111 TRACE_COUNTER1("mirroring",
112 "MirroringTokenBucketUsec",
113 std::max
<int64
>(0, token_bucket_
.InMicroseconds()));
115 current_event_
= event_time
;
117 // Return true if one capture period's worth of tokens are in the bucket.
118 return token_bucket_
>= capture_period_
;
121 void SmoothEventSampler::RecordSample() {
122 token_bucket_
-= capture_period_
;
123 TRACE_COUNTER1("mirroring",
124 "MirroringTokenBucketUsec",
125 std::max
<int64
>(0, token_bucket_
.InMicroseconds()));
127 bool was_paused
= overdue_sample_count_
== redundant_capture_goal_
;
128 if (HasUnrecordedEvent()) {
129 last_sample_
= current_event_
;
130 overdue_sample_count_
= 0;
132 ++overdue_sample_count_
;
134 bool is_paused
= overdue_sample_count_
== redundant_capture_goal_
;
136 VLOG_IF(0, !was_paused
&& is_paused
)
137 << "Tab content unchanged for " << redundant_capture_goal_
138 << " frames; capture will halt until content changes.";
139 VLOG_IF(0, was_paused
&& !is_paused
)
140 << "Content changed; capture will resume.";
143 bool SmoothEventSampler::IsOverdueForSamplingAt(base::TimeTicks event_time
)
145 DCHECK(!event_time
.is_null());
147 // If we don't get events on compositor updates on this platform, then we
148 // don't reliably know whether we're dirty.
149 if (events_are_reliable_
) {
150 if (!HasUnrecordedEvent() &&
151 overdue_sample_count_
>= redundant_capture_goal_
) {
152 return false; // Not dirty.
156 if (last_sample_
.is_null())
159 // If we're dirty but not yet old, then we've recently gotten updates, so we
160 // won't request a sample just yet.
161 base::TimeDelta dirty_interval
= event_time
- last_sample_
;
162 if (dirty_interval
< capture_period_
* 4)
168 bool SmoothEventSampler::HasUnrecordedEvent() const {
169 return !current_event_
.is_null() && current_event_
!= last_sample_
;
172 } // namespace content