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 #ifndef CONTENT_BROWSER_MEDIA_CAPTURE_VIDEO_CAPTURE_ORACLE_H_
6 #define CONTENT_BROWSER_MEDIA_CAPTURE_VIDEO_CAPTURE_ORACLE_H_
8 #include "base/callback_forward.h"
9 #include "base/memory/scoped_ptr.h"
10 #include "base/time/time.h"
11 #include "content/browser/media/capture/animated_content_sampler.h"
12 #include "content/browser/media/capture/smooth_event_sampler.h"
13 #include "content/common/content_export.h"
14 #include "ui/gfx/geometry/rect.h"
18 // VideoCaptureOracle manages the producer-side throttling of captured frames
19 // 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
21 // ought to be captured in order to achieve its target frame rate.
22 class CONTENT_EXPORT VideoCaptureOracle
{
30 explicit VideoCaptureOracle(base::TimeDelta min_capture_period
);
31 virtual ~VideoCaptureOracle();
33 // Record a event of type |event|, and decide whether the caller should do a
34 // frame capture. |damage_rect| is the region of a frame about to be drawn,
35 // and may be an empty Rect, if this is not known. If the caller accepts the
36 // oracle's proposal, it should call RecordCapture() to indicate this.
37 bool ObserveEventAndDecideCapture(Event event
,
38 const gfx::Rect
& damage_rect
,
39 base::TimeTicks event_time
);
41 // Record the start of a capture. Returns a frame_number to be used with
45 // Notify of the completion of a capture. Returns true iff the captured frame
46 // should be delivered. |frame_timestamp| is set to the timestamp that should
47 // be provided to the consumer of the frame.
48 bool CompleteCapture(int frame_number
, base::TimeTicks
* frame_timestamp
);
50 base::TimeDelta
min_capture_period() const {
51 return smoothing_sampler_
.min_capture_period();
55 // Retrieve/Assign a frame timestamp by capture |frame_number|.
56 base::TimeTicks
GetFrameTimestamp(int frame_number
) const;
57 void SetFrameTimestamp(int frame_number
, base::TimeTicks timestamp
);
59 // Incremented every time a paint or update event occurs.
62 // Stores the last |event_time| from the last observation/decision. Used to
63 // sanity-check that event times are monotonically non-decreasing.
64 base::TimeTicks last_event_time_
[kNumEvents
];
66 // Stores the frame number from the last delivered frame.
67 int last_delivered_frame_number_
;
69 // These track present/paint history and propose whether to sample each event
70 // for capture. |smoothing_sampler_| uses a "works for all" heuristic, while
71 // |content_sampler_| specifically detects animated content (e.g., video
72 // playback) and decides which events to sample to "lock into" that content.
73 SmoothEventSampler smoothing_sampler_
;
74 AnimatedContentSampler content_sampler_
;
76 // Recent history of frame timestamps proposed by VideoCaptureOracle. This is
77 // a ring-buffer, and should only be accessed by the Get/SetFrameTimestamp()
79 enum { kMaxFrameTimestamps
= 16 };
80 base::TimeTicks frame_timestamps_
[kMaxFrameTimestamps
];
83 } // namespace content
85 #endif // CONTENT_BROWSER_MEDIA_CAPTURE_VIDEO_CAPTURE_ORACLE_H_