Report errors from ChromiumEnv::GetChildren in Posix.
[chromium-blink-merge.git] / media / filters / video_renderer_base.h
blob41e5e3968c1c15b14f1ce445a954315001cf61e2
1 // Copyright (c) 2012 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_FILTERS_VIDEO_RENDERER_BASE_H_
6 #define MEDIA_FILTERS_VIDEO_RENDERER_BASE_H_
8 #include <deque>
10 #include "base/memory/ref_counted.h"
11 #include "base/memory/scoped_vector.h"
12 #include "base/memory/weak_ptr.h"
13 #include "base/synchronization/condition_variable.h"
14 #include "base/synchronization/lock.h"
15 #include "base/threading/platform_thread.h"
16 #include "media/base/decryptor.h"
17 #include "media/base/demuxer_stream.h"
18 #include "media/base/pipeline_status.h"
19 #include "media/base/video_decoder.h"
20 #include "media/base/video_frame.h"
21 #include "media/base/video_renderer.h"
22 #include "media/filters/video_frame_stream.h"
24 namespace base {
25 class MessageLoopProxy;
28 namespace media {
30 // VideoRendererBase creates its own thread for the sole purpose of timing frame
31 // presentation. It handles reading from the VideoFrameStream and stores the
32 // results in a queue of decoded frames and executing a callback when a frame is
33 // ready for rendering.
34 class MEDIA_EXPORT VideoRendererBase
35 : public VideoRenderer,
36 public base::PlatformThread::Delegate {
37 public:
38 typedef base::Callback<void(const scoped_refptr<VideoFrame>&)> PaintCB;
39 typedef base::Callback<void(bool)> SetOpaqueCB;
41 // Maximum duration of the last frame.
42 static base::TimeDelta kMaxLastFrameDuration();
44 // |decoders| contains the VideoDecoders to use when initializing.
46 // |paint_cb| is executed on the video frame timing thread whenever a new
47 // frame is available for painting.
49 // |set_opaque_cb| is executed when the renderer is initialized to inform
50 // the player whether the decoded output will be opaque or not.
52 // Implementors should avoid doing any sort of heavy work in this method and
53 // instead post a task to a common/worker thread to handle rendering. Slowing
54 // down the video thread may result in losing synchronization with audio.
56 // Setting |drop_frames_| to true causes the renderer to drop expired frames.
57 VideoRendererBase(const scoped_refptr<base::MessageLoopProxy>& message_loop,
58 ScopedVector<VideoDecoder> decoders,
59 const SetDecryptorReadyCB& set_decryptor_ready_cb,
60 const PaintCB& paint_cb,
61 const SetOpaqueCB& set_opaque_cb,
62 bool drop_frames);
63 virtual ~VideoRendererBase();
65 // VideoRenderer implementation.
66 virtual void Initialize(DemuxerStream* stream,
67 const PipelineStatusCB& init_cb,
68 const StatisticsCB& statistics_cb,
69 const TimeCB& max_time_cb,
70 const NaturalSizeChangedCB& size_changed_cb,
71 const base::Closure& ended_cb,
72 const PipelineStatusCB& error_cb,
73 const TimeDeltaCB& get_time_cb,
74 const TimeDeltaCB& get_duration_cb) OVERRIDE;
75 virtual void Play(const base::Closure& callback) OVERRIDE;
76 virtual void Pause(const base::Closure& callback) OVERRIDE;
77 virtual void Flush(const base::Closure& callback) OVERRIDE;
78 virtual void Preroll(base::TimeDelta time,
79 const PipelineStatusCB& cb) OVERRIDE;
80 virtual void Stop(const base::Closure& callback) OVERRIDE;
81 virtual void SetPlaybackRate(float playback_rate) OVERRIDE;
83 // PlatformThread::Delegate implementation.
84 virtual void ThreadMain() OVERRIDE;
86 private:
87 // Callback for |video_frame_stream_| initialization.
88 void OnVideoFrameStreamInitialized(bool success, bool has_alpha);
90 // Callback for |video_frame_stream_| to deliver decoded video frames and
91 // report video decoding status.
92 void FrameReady(VideoFrameStream::Status status,
93 const scoped_refptr<VideoFrame>& frame);
95 // Helper method for adding a frame to |ready_frames_|.
96 void AddReadyFrame_Locked(const scoped_refptr<VideoFrame>& frame);
98 // Helper method that schedules an asynchronous read from the
99 // |video_frame_stream_| as long as there isn't a pending read and we have
100 // capacity.
101 void AttemptRead();
102 void AttemptRead_Locked();
104 // Called when VideoFrameStream::Reset() completes.
105 void OnVideoFrameStreamResetDone();
107 // Calculates the duration to sleep for based on |last_timestamp_|,
108 // the next frame timestamp (may be NULL), and the provided playback rate.
110 // We don't use |playback_rate_| to avoid locking.
111 base::TimeDelta CalculateSleepDuration(
112 const scoped_refptr<VideoFrame>& next_frame,
113 float playback_rate);
115 // Helper function that flushes the buffers when a Stop() or error occurs.
116 void DoStopOrError_Locked();
118 // Runs |paint_cb_| with the next frame from |ready_frames_|, updating
119 // |last_natural_size_| and running |size_changed_cb_| if the natural size
120 // changes.
122 // A read is scheduled to replace the frame.
123 void PaintNextReadyFrame_Locked();
125 // Drops the next frame from |ready_frames_| and runs |statistics_cb_|.
127 // A read is scheduled to replace the frame.
128 void DropNextReadyFrame_Locked();
130 void TransitionToPrerolled_Locked();
132 // Runs |statistics_cb_| with |frames_decoded_| and |frames_dropped_|, resets
133 // them to 0, and then waits on |frame_available_| for up to the
134 // |wait_duration|.
135 void UpdateStatsAndWait_Locked(base::TimeDelta wait_duration);
137 scoped_refptr<base::MessageLoopProxy> message_loop_;
138 base::WeakPtrFactory<VideoRendererBase> weak_factory_;
139 base::WeakPtr<VideoRendererBase> weak_this_;
141 // Used for accessing data members.
142 base::Lock lock_;
144 // Provides video frames to VideoRendererBase.
145 VideoFrameStream video_frame_stream_;
147 // Queue of incoming frames yet to be painted.
148 typedef std::deque<scoped_refptr<VideoFrame> > VideoFrameQueue;
149 VideoFrameQueue ready_frames_;
151 // Keeps track of whether we received the end of stream buffer.
152 bool received_end_of_stream_;
154 // Used to signal |thread_| as frames are added to |frames_|. Rule of thumb:
155 // always check |state_| to see if it was set to STOPPED after waking up!
156 base::ConditionVariable frame_available_;
158 // State transition Diagram of this class:
159 // [kUninitialized] -------> [kError]
160 // |
161 // | Initialize()
162 // [kInitializing]
163 // |
164 // V
165 // +------[kFlushed]<---------------OnVideoFrameStreamResetDone()
166 // | | Preroll() or upon ^
167 // | V got first frame [kFlushing]
168 // | [kPrerolling] ^
169 // | | | Flush()
170 // | V Got enough frames |
171 // | [kPrerolled]---------------------->[kPaused]
172 // | | Pause() ^
173 // | V Play() |
174 // | [kPlaying]---------------------------|
175 // | | Pause() ^
176 // | V Receive EOF frame. | Pause()
177 // | [kEnded]-----------------------------+
178 // | ^
179 // | |
180 // +-----> [kStopped] [Any state other than]
181 // [kUninitialized/kError]
183 // Simple state tracking variable.
184 enum State {
185 kUninitialized,
186 kInitializing,
187 kPrerolled,
188 kPaused,
189 kFlushing,
190 kFlushed,
191 kPrerolling,
192 kPlaying,
193 kEnded,
194 kStopped,
195 kError,
197 State state_;
199 // Video thread handle.
200 base::PlatformThreadHandle thread_;
202 // Keep track of the outstanding read on the VideoFrameStream. Flushing can
203 // only complete once the read has completed.
204 bool pending_read_;
206 bool drop_frames_;
208 float playback_rate_;
210 // Playback operation callbacks.
211 base::Closure flush_cb_;
212 PipelineStatusCB preroll_cb_;
214 // Event callbacks.
215 PipelineStatusCB init_cb_;
216 StatisticsCB statistics_cb_;
217 TimeCB max_time_cb_;
218 NaturalSizeChangedCB size_changed_cb_;
219 base::Closure ended_cb_;
220 PipelineStatusCB error_cb_;
221 TimeDeltaCB get_time_cb_;
222 TimeDeltaCB get_duration_cb_;
224 base::TimeDelta preroll_timestamp_;
226 // Embedder callback for notifying a new frame is available for painting.
227 PaintCB paint_cb_;
229 // Callback to execute to inform the player if the decoded output is opaque.
230 SetOpaqueCB set_opaque_cb_;
232 // The last natural size |size_changed_cb_| was called with.
234 // TODO(scherkus): WebMediaPlayerImpl should track this instead of plumbing
235 // this through Pipeline. The one tricky bit might be guaranteeing we deliver
236 // the size information before we reach HAVE_METADATA.
237 gfx::Size last_natural_size_;
239 // The timestamp of the last frame removed from the |ready_frames_| queue,
240 // either for calling |paint_cb_| or for dropping. Set to kNoTimestamp()
241 // during flushing.
242 base::TimeDelta last_timestamp_;
244 // Keeps track of the number of frames decoded and dropped since the
245 // last call to |statistics_cb_|. These must be accessed under lock.
246 int frames_decoded_;
247 int frames_dropped_;
249 DISALLOW_COPY_AND_ASSIGN(VideoRendererBase);
252 } // namespace media
254 #endif // MEDIA_FILTERS_VIDEO_RENDERER_BASE_H_