Supervised user whitelists: Cleanup
[chromium-blink-merge.git] / media / renderers / video_renderer_impl.h
blob7eb9814204eb61b1bd2c04c8d02c1a1ac87e7c4f
1 // Copyright 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 MEDIA_RENDERERS_VIDEO_RENDERER_IMPL_H_
6 #define MEDIA_RENDERERS_VIDEO_RENDERER_IMPL_H_
8 #include <deque>
10 #include "base/memory/ref_counted.h"
11 #include "base/memory/scoped_ptr.h"
12 #include "base/memory/scoped_vector.h"
13 #include "base/memory/weak_ptr.h"
14 #include "base/synchronization/condition_variable.h"
15 #include "base/synchronization/lock.h"
16 #include "base/threading/platform_thread.h"
17 #include "media/base/decryptor.h"
18 #include "media/base/demuxer_stream.h"
19 #include "media/base/media_log.h"
20 #include "media/base/pipeline_status.h"
21 #include "media/base/video_decoder.h"
22 #include "media/base/video_frame.h"
23 #include "media/base/video_renderer.h"
24 #include "media/base/video_renderer_sink.h"
25 #include "media/filters/decoder_stream.h"
27 namespace base {
28 class SingleThreadTaskRunner;
29 class TickClock;
32 namespace media {
34 // VideoRendererImpl creates its own thread for the sole purpose of timing frame
35 // presentation. It handles reading from the VideoFrameStream and stores the
36 // results in a queue of decoded frames and executing a callback when a frame is
37 // ready for rendering.
38 class MEDIA_EXPORT VideoRendererImpl
39 : public VideoRenderer,
40 public NON_EXPORTED_BASE(VideoRendererSink::RenderCallback),
41 public base::PlatformThread::Delegate {
42 public:
43 // |decoders| contains the VideoDecoders to use when initializing.
45 // Implementors should avoid doing any sort of heavy work in this method and
46 // instead post a task to a common/worker thread to handle rendering. Slowing
47 // down the video thread may result in losing synchronization with audio.
49 // Setting |drop_frames_| to true causes the renderer to drop expired frames.
50 VideoRendererImpl(
51 const scoped_refptr<base::SingleThreadTaskRunner>& task_runner,
52 VideoRendererSink* sink,
53 ScopedVector<VideoDecoder> decoders,
54 bool drop_frames,
55 const scoped_refptr<MediaLog>& media_log);
56 ~VideoRendererImpl() override;
58 // VideoRenderer implementation.
59 void Initialize(DemuxerStream* stream,
60 const PipelineStatusCB& init_cb,
61 const SetDecryptorReadyCB& set_decryptor_ready_cb,
62 const StatisticsCB& statistics_cb,
63 const BufferingStateCB& buffering_state_cb,
64 const base::Closure& ended_cb,
65 const PipelineStatusCB& error_cb,
66 const WallClockTimeCB& wall_clock_time_cb,
67 const base::Closure& waiting_for_decryption_key_cb) override;
68 void Flush(const base::Closure& callback) override;
69 void StartPlayingFrom(base::TimeDelta timestamp) override;
70 void OnTimeStateChanged(bool time_progressing) override;
72 // PlatformThread::Delegate implementation.
73 void ThreadMain() override;
75 void SetTickClockForTesting(scoped_ptr<base::TickClock> tick_clock);
77 // VideoRendererSink::RenderCallback implementation.
78 scoped_refptr<VideoFrame> Render(base::TimeTicks deadline_min,
79 base::TimeTicks deadline_max) override;
80 void OnFrameDropped() override;
82 private:
83 // Creates a dedicated |thread_| for video rendering.
84 void CreateVideoThread();
86 // Callback for |video_frame_stream_| initialization.
87 void OnVideoFrameStreamInitialized(bool success);
89 // Callback for |video_frame_stream_| to deliver decoded video frames and
90 // report video decoding status.
91 void FrameReady(VideoFrameStream::Status status,
92 const scoped_refptr<VideoFrame>& frame);
94 // Helper method for adding a frame to |ready_frames_|.
95 void AddReadyFrame_Locked(const scoped_refptr<VideoFrame>& frame);
97 // Helper method that schedules an asynchronous read from the
98 // |video_frame_stream_| as long as there isn't a pending read and we have
99 // capacity.
100 void AttemptRead();
101 void AttemptRead_Locked();
103 // Called when VideoFrameStream::Reset() completes.
104 void OnVideoFrameStreamResetDone();
106 // Runs |paint_cb_| with the next frame from |ready_frames_|.
108 // A read is scheduled to replace the frame.
109 void PaintNextReadyFrame_Locked();
111 // Drops the next frame from |ready_frames_| and runs |statistics_cb_|.
113 // A read is scheduled to replace the frame.
114 void DropNextReadyFrame_Locked();
116 // Returns true if the renderer has enough data for playback purposes.
117 // Note that having enough data may be due to reaching end of stream.
118 bool HaveEnoughData_Locked();
119 void TransitionToHaveEnough_Locked();
121 // Runs |statistics_cb_| with |frames_decoded_| and |frames_dropped_|, resets
122 // them to 0, and then waits on |frame_available_| for up to the
123 // |wait_duration|.
124 void UpdateStatsAndWait_Locked(base::TimeDelta wait_duration);
126 scoped_refptr<base::SingleThreadTaskRunner> task_runner_;
128 VideoRendererSink* const sink_;
130 // Used for accessing data members.
131 base::Lock lock_;
133 // Provides video frames to VideoRendererImpl.
134 scoped_ptr<VideoFrameStream> video_frame_stream_;
136 // Flag indicating low-delay mode.
137 bool low_delay_;
139 // Queue of incoming frames yet to be painted.
140 typedef std::deque<scoped_refptr<VideoFrame> > VideoFrameQueue;
141 VideoFrameQueue ready_frames_;
143 // Keeps track of whether we received the end of stream buffer and finished
144 // rendering.
145 bool received_end_of_stream_;
146 bool rendered_end_of_stream_;
148 // Used to signal |thread_| as frames are added to |frames_|. Rule of thumb:
149 // always check |state_| to see if it was set to STOPPED after waking up!
150 base::ConditionVariable frame_available_;
152 // Important detail: being in kPlaying doesn't imply that video is being
153 // rendered. Rather, it means that the renderer is ready to go. The actual
154 // rendering of video is controlled by time advancing via |get_time_cb_|.
156 // kUninitialized
157 // | Initialize()
158 // |
159 // V
160 // kInitializing
161 // | Decoders initialized
162 // |
163 // V Decoders reset
164 // kFlushed <------------------ kFlushing
165 // | StartPlayingFrom() ^
166 // | |
167 // | | Flush()
168 // `---------> kPlaying --------'
169 enum State {
170 kUninitialized,
171 kInitializing,
172 kFlushing,
173 kFlushed,
174 kPlaying
176 State state_;
178 // Video thread handle.
179 base::PlatformThreadHandle thread_;
181 // Keep track of the outstanding read on the VideoFrameStream. Flushing can
182 // only complete once the read has completed.
183 bool pending_read_;
185 bool drop_frames_;
187 BufferingState buffering_state_;
189 // Playback operation callbacks.
190 base::Closure flush_cb_;
192 // Event callbacks.
193 PipelineStatusCB init_cb_;
194 StatisticsCB statistics_cb_;
195 BufferingStateCB buffering_state_cb_;
196 base::Closure ended_cb_;
197 PipelineStatusCB error_cb_;
198 WallClockTimeCB wall_clock_time_cb_;
200 base::TimeDelta start_timestamp_;
202 // Embedder callback for notifying a new frame is available for painting.
203 PaintCB paint_cb_;
205 // The wallclock times of the last frame removed from the |ready_frames_|
206 // queue, either for calling |paint_cb_| or for dropping. Set to null during
207 // flushing.
208 base::TimeTicks last_media_time_;
210 // Equivalent to |last_media_time_| + the estimated duration of the frame.
211 base::TimeTicks latest_possible_paint_time_;
213 // Keeps track of the number of frames decoded and dropped since the
214 // last call to |statistics_cb_|. These must be accessed under lock.
215 int frames_decoded_;
216 int frames_dropped_;
218 bool is_shutting_down_;
220 scoped_ptr<base::TickClock> tick_clock_;
222 // NOTE: Weak pointers must be invalidated before all other member variables.
223 base::WeakPtrFactory<VideoRendererImpl> weak_factory_;
225 DISALLOW_COPY_AND_ASSIGN(VideoRendererImpl);
228 } // namespace media
230 #endif // MEDIA_RENDERERS_VIDEO_RENDERER_IMPL_H_