OLD | NEW |
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef CONTENT_BROWSER_RENDERER_HOST_MEDIA_VIDEO_CAPTURE_ORACLE_H_ | 5 #ifndef CONTENT_BROWSER_RENDERER_HOST_MEDIA_VIDEO_CAPTURE_ORACLE_H_ |
6 #define CONTENT_BROWSER_RENDERER_HOST_MEDIA_VIDEO_CAPTURE_ORACLE_H_ | 6 #define CONTENT_BROWSER_RENDERER_HOST_MEDIA_VIDEO_CAPTURE_ORACLE_H_ |
7 | 7 |
8 #include "base/callback_forward.h" | 8 #include "base/callback_forward.h" |
9 #include "base/memory/scoped_ptr.h" | 9 #include "base/memory/scoped_ptr.h" |
10 #include "base/time/time.h" | 10 #include "base/time/time.h" |
11 #include "content/common/content_export.h" | 11 #include "content/common/content_export.h" |
12 | 12 |
13 namespace content { | 13 namespace content { |
14 | 14 |
15 // Filters a sequence of events to achieve a target frequency. | 15 // Filters a sequence of events to achieve a target frequency. |
16 class CONTENT_EXPORT SmoothEventSampler { | 16 class CONTENT_EXPORT SmoothEventSampler { |
17 public: | 17 public: |
18 explicit SmoothEventSampler(base::TimeDelta capture_period, | 18 explicit SmoothEventSampler(base::TimeDelta capture_period, |
19 bool events_are_reliable, | 19 bool events_are_reliable, |
20 int redundant_capture_goal); | 20 int redundant_capture_goal); |
21 | 21 |
22 // Add a new event to the event history, and return whether it ought to be | 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 | 23 // sampled based on the desired |capture_period|. The event is not recorded as |
24 // a sample until RecordSample() is called. | 24 // a sample until RecordSample() is called. |
25 bool AddEventAndConsiderSampling(base::Time event_time); | 25 bool AddEventAndConsiderSampling(base::TimeTicks event_time); |
26 | 26 |
27 // Operates on the last event added by AddEventAndConsiderSampling(), marking | 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 | 28 // it as sampled. After this point we are current in the stream of events, as |
29 // we have sampled the most recent event. | 29 // we have sampled the most recent event. |
30 void RecordSample(); | 30 void RecordSample(); |
31 | 31 |
32 // Returns true if, at time |event_time|, sampling should occur because too | 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. | 33 // much time will have passed relative to the last event and/or sample. |
34 bool IsOverdueForSamplingAt(base::Time event_time) const; | 34 bool IsOverdueForSamplingAt(base::TimeTicks event_time) const; |
35 | 35 |
36 // Returns true if AddEventAndConsiderSampling() has been called since the | 36 // Returns true if AddEventAndConsiderSampling() has been called since the |
37 // last call to RecordSample(). | 37 // last call to RecordSample(). |
38 bool HasUnrecordedEvent() const; | 38 bool HasUnrecordedEvent() const; |
39 | 39 |
40 private: | 40 private: |
41 const bool events_are_reliable_; | 41 const bool events_are_reliable_; |
42 const base::TimeDelta capture_period_; | 42 const base::TimeDelta capture_period_; |
43 const int redundant_capture_goal_; | 43 const int redundant_capture_goal_; |
44 const base::TimeDelta token_bucket_capacity_; | 44 const base::TimeDelta token_bucket_capacity_; |
45 | 45 |
46 base::Time current_event_; | 46 base::TimeTicks current_event_; |
47 base::Time last_sample_; | 47 base::TimeTicks last_sample_; |
48 int overdue_sample_count_; | 48 int overdue_sample_count_; |
49 base::TimeDelta token_bucket_; | 49 base::TimeDelta token_bucket_; |
50 | 50 |
51 DISALLOW_COPY_AND_ASSIGN(SmoothEventSampler); | 51 DISALLOW_COPY_AND_ASSIGN(SmoothEventSampler); |
52 }; | 52 }; |
53 | 53 |
54 // VideoCaptureOracle manages the producer-side throttling of captured frames | 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; | 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 | 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. | 57 // ought to be captured in order to achieve its target frame rate. |
58 class CONTENT_EXPORT VideoCaptureOracle { | 58 class CONTENT_EXPORT VideoCaptureOracle { |
59 public: | 59 public: |
60 enum Event { | 60 enum Event { |
61 kTimerPoll, | 61 kTimerPoll, |
62 kCompositorUpdate, | 62 kCompositorUpdate, |
63 kSoftwarePaint, | 63 kSoftwarePaint, |
64 }; | 64 }; |
65 | 65 |
66 VideoCaptureOracle(base::TimeDelta capture_period, | 66 VideoCaptureOracle(base::TimeDelta capture_period, |
67 bool events_are_reliable); | 67 bool events_are_reliable); |
68 virtual ~VideoCaptureOracle() {} | 68 virtual ~VideoCaptureOracle() {} |
69 | 69 |
70 // Record an event of type |event|, and decide whether the caller should do a | 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 | 71 // frame capture immediately. Decisions of the oracle are final: the caller |
72 // must do what it is told. | 72 // must do what it is told. |
73 bool ObserveEventAndDecideCapture( | 73 bool ObserveEventAndDecideCapture(Event event, base::TimeTicks event_time); |
74 Event event, | |
75 base::Time event_time); | |
76 | 74 |
77 // Record the start of a capture. Returns a frame_number to be used with | 75 // Record the start of a capture. Returns a frame_number to be used with |
78 // CompleteCapture(). | 76 // CompleteCapture(). |
79 int RecordCapture(); | 77 int RecordCapture(); |
80 | 78 |
81 // Record the completion of a capture. Returns true iff the captured frame | 79 // Record the completion of a capture. Returns true iff the captured frame |
82 // should be delivered. | 80 // should be delivered. |
83 bool CompleteCapture(int frame_number, base::Time timestamp); | 81 bool CompleteCapture(int frame_number, base::TimeTicks timestamp); |
84 | 82 |
85 base::TimeDelta capture_period() const { return capture_period_; } | 83 base::TimeDelta capture_period() const { return capture_period_; } |
86 | 84 |
87 private: | 85 private: |
88 | 86 |
89 // Time between frames. | 87 // Time between frames. |
90 const base::TimeDelta capture_period_; | 88 const base::TimeDelta capture_period_; |
91 | 89 |
92 // Incremented every time a paint or update event occurs. | 90 // Incremented every time a paint or update event occurs. |
93 int frame_number_; | 91 int frame_number_; |
94 | 92 |
95 // Stores the frame number from the last delivered frame. | 93 // Stores the frame number from the last delivered frame. |
96 int last_delivered_frame_number_; | 94 int last_delivered_frame_number_; |
97 | 95 |
98 // Stores the timestamp of the last delivered frame. | 96 // Stores the timestamp of the last delivered frame. |
99 base::Time last_delivered_frame_timestamp_; | 97 base::TimeTicks last_delivered_frame_timestamp_; |
100 | 98 |
101 // Tracks present/paint history. | 99 // Tracks present/paint history. |
102 SmoothEventSampler sampler_; | 100 SmoothEventSampler sampler_; |
103 }; | 101 }; |
104 | 102 |
105 } // namespace content | 103 } // namespace content |
106 | 104 |
107 #endif // CONTENT_BROWSER_RENDERER_HOST_MEDIA_VIDEO_CAPTURE_ORACLE_H_ | 105 #endif // CONTENT_BROWSER_RENDERER_HOST_MEDIA_VIDEO_CAPTURE_ORACLE_H_ |
OLD | NEW |