Fix some potential after free errors on TestCompletionCallback
[chromium-blink-merge.git] / media / filters / video_renderer_base.h
blobc5a6143947374df569833ea132104f9727238dca
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 class DecryptingDemuxerStream;
32 // VideoRendererBase creates its own thread for the sole purpose of timing frame
33 // presentation. It handles reading from the decoder and stores the results in
34 // a queue of decoded frames and executing a callback when a frame is ready for
35 // rendering.
36 class MEDIA_EXPORT VideoRendererBase
37 : public VideoRenderer,
38 public base::PlatformThread::Delegate {
39 public:
40 typedef base::Callback<void(const scoped_refptr<VideoFrame>&)> PaintCB;
41 typedef base::Callback<void(bool)> SetOpaqueCB;
43 // Maximum duration of the last frame.
44 static base::TimeDelta kMaxLastFrameDuration();
46 // |decoders| contains the VideoDecoders to use when initializing.
48 // |paint_cb| is executed on the video frame timing thread whenever a new
49 // frame is available for painting.
51 // |set_opaque_cb| is executed when the renderer is initialized to inform
52 // the player whether the decoder's output will be opaque or not.
54 // Implementors should avoid doing any sort of heavy work in this method and
55 // instead post a task to a common/worker thread to handle rendering. Slowing
56 // down the video thread may result in losing synchronization with audio.
58 // Setting |drop_frames_| to true causes the renderer to drop expired frames.
59 VideoRendererBase(const scoped_refptr<base::MessageLoopProxy>& message_loop,
60 ScopedVector<VideoDecoder> decoders,
61 const SetDecryptorReadyCB& set_decryptor_ready_cb,
62 const PaintCB& paint_cb,
63 const SetOpaqueCB& set_opaque_cb,
64 bool drop_frames);
65 virtual ~VideoRendererBase();
67 // VideoRenderer implementation.
68 virtual void Initialize(DemuxerStream* stream,
69 const PipelineStatusCB& init_cb,
70 const StatisticsCB& statistics_cb,
71 const TimeCB& max_time_cb,
72 const NaturalSizeChangedCB& size_changed_cb,
73 const base::Closure& ended_cb,
74 const PipelineStatusCB& error_cb,
75 const TimeDeltaCB& get_time_cb,
76 const TimeDeltaCB& get_duration_cb) OVERRIDE;
77 virtual void Play(const base::Closure& callback) OVERRIDE;
78 virtual void Pause(const base::Closure& callback) OVERRIDE;
79 virtual void Flush(const base::Closure& callback) OVERRIDE;
80 virtual void Preroll(base::TimeDelta time,
81 const PipelineStatusCB& cb) OVERRIDE;
82 virtual void Stop(const base::Closure& callback) OVERRIDE;
83 virtual void SetPlaybackRate(float playback_rate) OVERRIDE;
85 // PlatformThread::Delegate implementation.
86 virtual void ThreadMain() OVERRIDE;
88 private:
89 // Callback for |video_frame_stream_| initialization.
90 void OnVideoFrameStreamInitialized(bool success, bool has_alpha);
92 // Callback from the video decoder delivering decoded video frames and
93 // reporting video decoder status.
94 void FrameReady(VideoDecoder::Status status,
95 const scoped_refptr<VideoFrame>& frame);
97 // Helper method for adding a frame to |ready_frames_|.
98 void AddReadyFrame_Locked(const scoped_refptr<VideoFrame>& frame);
100 // Helper method that schedules an asynchronous read from the decoder as long
101 // as there isn't a pending read and we have capacity.
102 void AttemptRead();
103 void AttemptRead_Locked();
105 // Called when VideoFrameStream::Reset() completes.
106 void OnVideoFrameStreamResetDone();
108 // Attempts to complete flushing and transition into the flushed state.
109 void AttemptFlush_Locked();
111 // Calculates the duration to sleep for based on |last_timestamp_|,
112 // the next frame timestamp (may be NULL), and the provided playback rate.
114 // We don't use |playback_rate_| to avoid locking.
115 base::TimeDelta CalculateSleepDuration(
116 const scoped_refptr<VideoFrame>& next_frame,
117 float playback_rate);
119 // Helper function that flushes the buffers when a Stop() or error occurs.
120 void DoStopOrError_Locked();
122 // Runs |paint_cb_| with the next frame from |ready_frames_|, updating
123 // |last_natural_size_| and running |size_changed_cb_| if the natural size
124 // changes.
126 // A read is scheduled to replace the frame.
127 void PaintNextReadyFrame_Locked();
129 // Drops the next frame from |ready_frames_| and runs |statistics_cb_|.
131 // A read is scheduled to replace the frame.
132 void DropNextReadyFrame_Locked();
134 void ResetDecoder();
135 void StopDecoder(const base::Closure& callback);
137 void TransitionToPrerolled_Locked();
139 scoped_refptr<base::MessageLoopProxy> message_loop_;
140 base::WeakPtrFactory<VideoRendererBase> weak_factory_;
141 base::WeakPtr<VideoRendererBase> weak_this_;
143 // Used for accessing data members.
144 base::Lock lock_;
146 // Provides video frames to VideoRendererBase.
147 VideoFrameStream video_frame_stream_;
149 // Queue of incoming frames yet to be painted.
150 typedef std::deque<scoped_refptr<VideoFrame> > VideoFrameQueue;
151 VideoFrameQueue ready_frames_;
153 // Keeps track of whether we received the end of stream buffer.
154 bool received_end_of_stream_;
156 // Used to signal |thread_| as frames are added to |frames_|. Rule of thumb:
157 // always check |state_| to see if it was set to STOPPED after waking up!
158 base::ConditionVariable frame_available_;
160 // State transition Diagram of this class:
161 // [kUninitialized] -------> [kError]
162 // |
163 // | Initialize()
164 // [kInitializing]
165 // |
166 // V All frames returned
167 // +------[kFlushed]<-----[kFlushing]<--- OnDecoderResetDone()
168 // | | Preroll() or upon ^
169 // | V got first frame [kFlushingDecoder]
170 // | [kPrerolling] ^
171 // | | | Flush()
172 // | V Got enough frames |
173 // | [kPrerolled]---------------------->[kPaused]
174 // | | Pause() ^
175 // | V Play() |
176 // | [kPlaying]---------------------------|
177 // | | Pause() ^
178 // | V Receive EOF frame. | Pause()
179 // | [kEnded]-----------------------------+
180 // | ^
181 // | |
182 // +-----> [kStopped] [Any state other than]
183 // [kUninitialized/kError]
185 // Simple state tracking variable.
186 enum State {
187 kUninitialized,
188 kInitializing,
189 kPrerolled,
190 kPaused,
191 kFlushingDecoder,
192 kFlushing,
193 kFlushed,
194 kPrerolling,
195 kPlaying,
196 kEnded,
197 kStopped,
198 kError,
200 State state_;
202 // Video thread handle.
203 base::PlatformThreadHandle thread_;
205 // Keep track of outstanding reads on the video decoder. Flushing can only
206 // complete once reads have completed.
207 bool pending_read_;
209 bool drop_frames_;
211 float playback_rate_;
213 // Playback operation callbacks.
214 base::Closure flush_cb_;
215 PipelineStatusCB preroll_cb_;
217 // Event callbacks.
218 PipelineStatusCB init_cb_;
219 StatisticsCB statistics_cb_;
220 TimeCB max_time_cb_;
221 NaturalSizeChangedCB size_changed_cb_;
222 base::Closure ended_cb_;
223 PipelineStatusCB error_cb_;
224 TimeDeltaCB get_time_cb_;
225 TimeDeltaCB get_duration_cb_;
227 base::TimeDelta preroll_timestamp_;
229 // Embedder callback for notifying a new frame is available for painting.
230 PaintCB paint_cb_;
232 // Callback to execute to inform the player if the video decoder's output is
233 // opaque.
234 SetOpaqueCB set_opaque_cb_;
236 // The last natural size |size_changed_cb_| was called with.
238 // TODO(scherkus): WebMediaPlayerImpl should track this instead of plumbing
239 // this through Pipeline. The one tricky bit might be guaranteeing we deliver
240 // the size information before we reach HAVE_METADATA.
241 gfx::Size last_natural_size_;
243 // The timestamp of the last frame removed from the |ready_frames_| queue,
244 // either for calling |paint_cb_| or for dropping. Set to kNoTimestamp()
245 // during flushing.
246 base::TimeDelta last_timestamp_;
248 DISALLOW_COPY_AND_ASSIGN(VideoRendererBase);
251 } // namespace media
253 #endif // MEDIA_FILTERS_VIDEO_RENDERER_BASE_H_