1 // Copyright 2014 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 MEDIA_FILTERS_VIDEO_FRAME_SCHEDULER_IMPL_H_ 6 #define MEDIA_FILTERS_VIDEO_FRAME_SCHEDULER_IMPL_H_ 7 8 #include <queue> 9 10 #include "base/memory/ref_counted.h" 11 #include "base/timer/timer.h" 12 #include "media/filters/video_frame_scheduler.h" 13 14 namespace base { 15 class SingleThreadTaskRunner; 16 class TickClock; 17 } 18 19 namespace media { 20 21 // A scheduler that uses delayed tasks on a task runner for timing the display 22 // of video frames. 23 // 24 // Single threaded. Calls must be on |task_runner|. 25 class MEDIA_EXPORT VideoFrameSchedulerImpl : public VideoFrameScheduler { 26 public: 27 typedef base::Callback<void(const scoped_refptr<VideoFrame>&)> DisplayCB; 28 29 // |task_runner| is used for scheduling the delayed tasks. 30 // |display_cb| is run when a frame is to be displayed. 31 VideoFrameSchedulerImpl( 32 const scoped_refptr<base::SingleThreadTaskRunner>& task_runner, 33 const DisplayCB& display_cb); 34 virtual ~VideoFrameSchedulerImpl(); 35 36 // VideoFrameScheduler implementation. 37 virtual void ScheduleVideoFrame(const scoped_refptr<VideoFrame>& frame, 38 base::TimeTicks wall_ticks, 39 const DoneCB& done_cb) OVERRIDE; 40 virtual void Reset() OVERRIDE; 41 42 void SetTickClockForTesting(scoped_ptr<base::TickClock> tick_clock); 43 44 private: 45 void ResetTimerIfNecessary(); 46 void OnTimerFired(); 47 48 scoped_refptr<base::SingleThreadTaskRunner> task_runner_; 49 DisplayCB display_cb_; 50 scoped_ptr<base::TickClock> tick_clock_; 51 base::OneShotTimer<VideoFrameScheduler> timer_; 52 53 struct PendingFrame { 54 PendingFrame(const scoped_refptr<VideoFrame>& frame, 55 base::TimeTicks wall_ticks, 56 const DoneCB& done_cb); 57 ~PendingFrame(); 58 59 // For use with std::priority_queue<T>. 60 bool operator<(const PendingFrame& other) const; 61 62 scoped_refptr<VideoFrame> frame; 63 base::TimeTicks wall_ticks; 64 DoneCB done_cb; 65 }; 66 typedef std::priority_queue<PendingFrame> PendingFrameQueue; 67 PendingFrameQueue pending_frames_; 68 69 DISALLOW_COPY_AND_ASSIGN(VideoFrameSchedulerImpl); 70 }; 71 72 } // namespace media 73 74 #endif // MEDIA_FILTERS_VIDEO_FRAME_SCHEDULER_IMPL_H_ 75