Save errno for logging before potentially overwriting it.
[chromium-blink-merge.git] / content / browser / renderer_host / media / video_capture_oracle.h
blob0c2286c951ea1521b0eddf3c9ba9e4d3ebb20b7f
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_RENDERER_HOST_MEDIA_VIDEO_CAPTURE_ORACLE_H_
6 #define CONTENT_BROWSER_RENDERER_HOST_MEDIA_VIDEO_CAPTURE_ORACLE_H_
8 #include "base/callback_forward.h"
9 #include "base/memory/scoped_ptr.h"
10 #include "base/time.h"
11 #include "content/common/content_export.h"
13 namespace content {
15 // Filters a sequence of events to achieve a target frequency.
16 class CONTENT_EXPORT SmoothEventSampler {
17 public:
18 explicit SmoothEventSampler(base::TimeDelta capture_period,
19 bool events_are_reliable,
20 int redundant_capture_goal);
22 // Add a new event to the event history, and return whether it ought to be
23 // sampled based on the desired |capture_period|. The event is not recorded as
24 // a sample until RecordSample() is called.
25 bool AddEventAndConsiderSampling(base::Time event_time);
27 // Operates on the last event added by AddEventAndConsiderSampling(), marking
28 // it as sampled. After this point we are current in the stream of events, as
29 // we have sampled the most recent event.
30 void RecordSample();
32 // Returns true if, at time |event_time|, sampling should occur because too
33 // much time will have passed relative to the last event and/or sample.
34 bool IsOverdueForSamplingAt(base::Time event_time) const;
36 // Returns true if AddEventAndConsiderSampling() has been called since the
37 // last call to RecordSample().
38 bool HasUnrecordedEvent() const;
40 private:
41 const bool events_are_reliable_;
42 const base::TimeDelta capture_period_;
43 const int redundant_capture_goal_;
44 const base::TimeDelta token_bucket_capacity_;
46 base::Time current_event_;
47 base::Time last_sample_;
48 int overdue_sample_count_;
49 base::TimeDelta token_bucket_;
51 DISALLOW_COPY_AND_ASSIGN(SmoothEventSampler);
54 // VideoCaptureOracle manages the producer-side throttling of captured frames
55 // from a video capture device. It is informed of every update by the device;
56 // this empowers it to look into the future and decide if a particular frame
57 // ought to be captured in order to achieve its target frame rate.
58 class CONTENT_EXPORT VideoCaptureOracle {
59 public:
60 enum Event {
61 kTimerPoll,
62 kCompositorUpdate,
63 kSoftwarePaint,
66 VideoCaptureOracle(base::TimeDelta capture_period,
67 bool events_are_reliable);
68 virtual ~VideoCaptureOracle() {}
70 // Record an event of type |event|, and decide whether the caller should do a
71 // frame capture immediately. Decisions of the oracle are final: the caller
72 // must do what it is told.
73 bool ObserveEventAndDecideCapture(
74 Event event,
75 base::Time event_time);
77 // Record the start of a capture. Returns a frame_number to be used with
78 // CompleteCapture().
79 int RecordCapture();
81 // Record the completion of a capture. Returns true iff the captured frame
82 // should be delivered.
83 bool CompleteCapture(int frame_number, base::Time timestamp);
85 base::TimeDelta capture_period() const { return capture_period_; }
87 private:
89 // Time between frames.
90 const base::TimeDelta capture_period_;
92 // Incremented every time a paint or update event occurs.
93 int frame_number_;
95 // Stores the frame number from the last delivered frame.
96 int last_delivered_frame_number_;
98 // Stores the timestamp of the last delivered frame.
99 base::Time last_delivered_frame_timestamp_;
101 // Tracks present/paint history.
102 SmoothEventSampler sampler_;
105 } // namespace content
107 #endif // CONTENT_BROWSER_RENDERER_HOST_MEDIA_VIDEO_CAPTURE_ORACLE_H_