This source file includes following definitions.
- sampler_
- ObserveEventAndDecideCapture
- RecordCapture
- CompleteCapture
- token_bucket_
- AddEventAndConsiderSampling
- RecordSample
- IsOverdueForSamplingAt
- HasUnrecordedEvent
#include "content/browser/media/capture/video_capture_oracle.h"
#include "base/debug/trace_event.h"
namespace content {
namespace {
const int kNumRedundantCapturesOfStaticContent = 200;
}
VideoCaptureOracle::VideoCaptureOracle(base::TimeDelta capture_period,
bool events_are_reliable)
: capture_period_(capture_period),
frame_number_(0),
last_delivered_frame_number_(0),
sampler_(capture_period_,
events_are_reliable,
kNumRedundantCapturesOfStaticContent) {}
bool VideoCaptureOracle::ObserveEventAndDecideCapture(
Event event,
base::TimeTicks event_time) {
const bool content_is_dirty = (event == kCompositorUpdate ||
event == kSoftwarePaint);
bool should_sample;
if (content_is_dirty) {
frame_number_++;
should_sample = sampler_.AddEventAndConsiderSampling(event_time);
} else {
should_sample = sampler_.IsOverdueForSamplingAt(event_time);
}
return should_sample;
}
int VideoCaptureOracle::RecordCapture() {
sampler_.RecordSample();
return frame_number_;
}
bool VideoCaptureOracle::CompleteCapture(int frame_number,
base::TimeTicks timestamp) {
if (last_delivered_frame_number_ > frame_number ||
last_delivered_frame_timestamp_ == timestamp) {
LOG(ERROR) << "Frame with same timestamp or out of order delivery. "
<< "Dropping frame.";
return false;
}
if (last_delivered_frame_timestamp_ > timestamp) {
LOG(ERROR) << "Frame with past timestamp (" << timestamp.ToInternalValue()
<< ") was delivered";
}
last_delivered_frame_number_ = frame_number;
last_delivered_frame_timestamp_ = timestamp;
return true;
}
SmoothEventSampler::SmoothEventSampler(base::TimeDelta capture_period,
bool events_are_reliable,
int redundant_capture_goal)
: events_are_reliable_(events_are_reliable),
capture_period_(capture_period),
redundant_capture_goal_(redundant_capture_goal),
token_bucket_capacity_(capture_period + capture_period / 2),
overdue_sample_count_(0),
token_bucket_(token_bucket_capacity_) {
DCHECK_GT(capture_period_.InMicroseconds(), 0);
}
bool SmoothEventSampler::AddEventAndConsiderSampling(
base::TimeTicks event_time) {
DCHECK(!event_time.is_null());
if (!current_event_.is_null()) {
if (current_event_ < event_time) {
token_bucket_ += event_time - current_event_;
if (token_bucket_ > token_bucket_capacity_)
token_bucket_ = token_bucket_capacity_;
}
if (token_bucket_ < base::TimeDelta())
token_bucket_ = base::TimeDelta();
TRACE_COUNTER1("mirroring",
"MirroringTokenBucketUsec",
std::max<int64>(0, token_bucket_.InMicroseconds()));
}
current_event_ = event_time;
return token_bucket_ >= capture_period_;
}
void SmoothEventSampler::RecordSample() {
token_bucket_ -= capture_period_;
TRACE_COUNTER1("mirroring",
"MirroringTokenBucketUsec",
std::max<int64>(0, token_bucket_.InMicroseconds()));
bool was_paused = overdue_sample_count_ == redundant_capture_goal_;
if (HasUnrecordedEvent()) {
last_sample_ = current_event_;
overdue_sample_count_ = 0;
} else {
++overdue_sample_count_;
}
bool is_paused = overdue_sample_count_ == redundant_capture_goal_;
VLOG_IF(0, !was_paused && is_paused)
<< "Tab content unchanged for " << redundant_capture_goal_
<< " frames; capture will halt until content changes.";
VLOG_IF(0, was_paused && !is_paused)
<< "Content changed; capture will resume.";
}
bool SmoothEventSampler::IsOverdueForSamplingAt(base::TimeTicks event_time)
const {
DCHECK(!event_time.is_null());
if (events_are_reliable_) {
if (!HasUnrecordedEvent() &&
overdue_sample_count_ >= redundant_capture_goal_) {
return false;
}
}
if (last_sample_.is_null())
return true;
base::TimeDelta dirty_interval = event_time - last_sample_;
if (dirty_interval < capture_period_ * 4)
return false;
else
return true;
}
bool SmoothEventSampler::HasUnrecordedEvent() const {
return !current_event_.is_null() && current_event_ != last_sample_;
}
}