Remove PlatformFile from profile_browsertest
[chromium-blink-merge.git] / content / browser / media / capture / video_capture_oracle.cc
blobc41e5e7e0b121770578d4285e613249c513227c4
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"
9 namespace content {
11 namespace {
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),
29 frame_number_(0),
30 last_delivered_frame_number_(0),
31 sampler_(capture_period_,
32 events_are_reliable,
33 kNumRedundantCapturesOfStaticContent) {}
35 bool VideoCaptureOracle::ObserveEventAndDecideCapture(
36 Event event,
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);
41 bool should_sample;
42 if (content_is_dirty) {
43 frame_number_++;
44 should_sample = sampler_.AddEventAndConsiderSampling(event_time);
45 } else {
46 should_sample = sampler_.IsOverdueForSamplingAt(event_time);
48 return should_sample;
51 int VideoCaptureOracle::RecordCapture() {
52 sampler_.RecordSample();
53 return frame_number_;
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. "
63 << "Dropping frame.";
64 return false;
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()
70 << ") was delivered";
73 last_delivered_frame_number_ = frame_number;
74 last_delivered_frame_timestamp_ = timestamp;
76 return true;
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;
131 } else {
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)
144 const {
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())
157 return true;
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)
163 return false;
164 else
165 return true;
168 bool SmoothEventSampler::HasUnrecordedEvent() const {
169 return !current_event_.is_null() && current_event_ != last_sample_;
172 } // namespace content