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. 4 5 #ifndef CONTENT_BROWSER_RENDERER_HOST_MEDIA_VIDEO_CAPTURE_ORACLE_H_ 6 #define CONTENT_BROWSER_RENDERER_HOST_MEDIA_VIDEO_CAPTURE_ORACLE_H_ 7 8 #include "base/callback_forward.h" 9 #include "base/memory/scoped_ptr.h" 10 #include "base/time/time.h" 11 #include "content/common/content_export.h" 12 13 namespace content { 14 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); 21 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); 26 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(); 31 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; 35 36 // Returns true if AddEventAndConsiderSampling() has been called since the 37 // last call to RecordSample(). 38 bool HasUnrecordedEvent() const; 39 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_; 45 46 base::Time current_event_; 47 base::Time last_sample_; 48 int overdue_sample_count_; 49 base::TimeDelta token_bucket_; 50 51 DISALLOW_COPY_AND_ASSIGN(SmoothEventSampler); 52 }; 53 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, 64 }; 65 66 VideoCaptureOracle(base::TimeDelta capture_period, 67 bool events_are_reliable); 68 virtual ~VideoCaptureOracle() {} 69 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); 76 77 // Record the start of a capture. Returns a frame_number to be used with 78 // CompleteCapture(). 79 int RecordCapture(); 80 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); 84 85 base::TimeDelta capture_period() const { return capture_period_; } 86 87 private: 88 89 // Time between frames. 90 const base::TimeDelta capture_period_; 91 92 // Incremented every time a paint or update event occurs. 93 int frame_number_; 94 95 // Stores the frame number from the last delivered frame. 96 int last_delivered_frame_number_; 97 98 // Stores the timestamp of the last delivered frame. 99 base::Time last_delivered_frame_timestamp_; 100 101 // Tracks present/paint history. 102 SmoothEventSampler sampler_; 103 }; 104 105 } // namespace content 106 107 #endif // CONTENT_BROWSER_RENDERER_HOST_MEDIA_VIDEO_CAPTURE_ORACLE_H_ 108