Clean up check for dependency_info.
[chromium-blink-merge.git] / media / renderers / video_renderer_impl.h
blob3800638a18e3202ea953118543cf196340e2900b
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/timer/timer.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"
26 #include "media/filters/video_renderer_algorithm.h"
27 #include "media/renderers/gpu_video_accelerator_factories.h"
28 #include "media/video/gpu_memory_buffer_video_frame_pool.h"
30 namespace base {
31 class SingleThreadTaskRunner;
32 class TickClock;
35 namespace media {
37 // VideoRendererImpl creates its own thread for the sole purpose of timing frame
38 // presentation. It handles reading from the VideoFrameStream and stores the
39 // results in a queue of decoded frames and executing a callback when a frame is
40 // ready for rendering.
41 class MEDIA_EXPORT VideoRendererImpl
42 : public VideoRenderer,
43 public NON_EXPORTED_BASE(VideoRendererSink::RenderCallback) {
44 public:
45 // |decoders| contains the VideoDecoders to use when initializing.
47 // Implementors should avoid doing any sort of heavy work in this method and
48 // instead post a task to a common/worker thread to handle rendering. Slowing
49 // down the video thread may result in losing synchronization with audio.
51 // Setting |drop_frames_| to true causes the renderer to drop expired frames.
52 VideoRendererImpl(
53 const scoped_refptr<base::SingleThreadTaskRunner>& media_task_runner,
54 const scoped_refptr<base::TaskRunner>& worker_task_runner,
55 VideoRendererSink* sink,
56 ScopedVector<VideoDecoder> decoders,
57 bool drop_frames,
58 const scoped_refptr<GpuVideoAcceleratorFactories>& gpu_factories,
59 const scoped_refptr<MediaLog>& media_log);
60 ~VideoRendererImpl() override;
62 // VideoRenderer implementation.
63 void Initialize(DemuxerStream* stream,
64 const PipelineStatusCB& init_cb,
65 const SetDecryptorReadyCB& set_decryptor_ready_cb,
66 const StatisticsCB& statistics_cb,
67 const BufferingStateCB& buffering_state_cb,
68 const base::Closure& ended_cb,
69 const PipelineStatusCB& error_cb,
70 const TimeSource::WallClockTimeCB& wall_clock_time_cb,
71 const base::Closure& waiting_for_decryption_key_cb) override;
72 void Flush(const base::Closure& callback) override;
73 void StartPlayingFrom(base::TimeDelta timestamp) override;
74 void OnTimeStateChanged(bool time_progressing) override;
76 void SetTickClockForTesting(scoped_ptr<base::TickClock> tick_clock);
77 void SetGpuMemoryBufferVideoForTesting(
78 scoped_ptr<GpuMemoryBufferVideoFramePool> gpu_memory_buffer_pool);
80 // VideoRendererSink::RenderCallback implementation.
81 scoped_refptr<VideoFrame> Render(base::TimeTicks deadline_min,
82 base::TimeTicks deadline_max,
83 bool background_rendering) override;
84 void OnFrameDropped() override;
86 private:
87 // Callback for |video_frame_stream_| initialization.
88 void OnVideoFrameStreamInitialized(bool success);
90 // Callback for |video_frame_stream_| to deliver decoded video frames and
91 // report video decoding status. If a frame is available the planes will be
92 // copied asynchronously and FrameReady will be called once finished copying.
93 void FrameReadyForCopyingToGpuMemoryBuffers(
94 VideoFrameStream::Status status,
95 const scoped_refptr<VideoFrame>& frame);
97 // Callback for |video_frame_stream_| to deliver decoded video frames and
98 // report video decoding status.
99 void FrameReady(uint32_t sequence_token,
100 VideoFrameStream::Status status,
101 const scoped_refptr<VideoFrame>& frame);
103 // Helper method for enqueueing a frame to |alogorithm_|.
104 void AddReadyFrame_Locked(const scoped_refptr<VideoFrame>& frame);
106 // Helper method that schedules an asynchronous read from the
107 // |video_frame_stream_| as long as there isn't a pending read and we have
108 // capacity.
109 void AttemptRead();
110 void AttemptRead_Locked();
112 // Called when VideoFrameStream::Reset() completes.
113 void OnVideoFrameStreamResetDone();
115 // Returns true if the renderer has enough data for playback purposes.
116 // Note that having enough data may be due to reaching end of stream.
117 bool HaveEnoughData_Locked();
118 void TransitionToHaveEnough_Locked();
119 void TransitionToHaveNothing();
121 // Runs |statistics_cb_| with |frames_decoded_| and |frames_dropped_|, resets
122 // them to 0.
123 void UpdateStats_Locked();
125 // Called after we've painted the first frame. If |time_progressing_| is
126 // false it Stop() on |sink_|.
127 void MaybeStopSinkAfterFirstPaint();
129 // Returns true if there is no more room for additional buffered frames.
130 bool HaveReachedBufferingCap();
132 // Starts or stops |sink_| respectively. Do not call while |lock_| is held.
133 void StartSink();
134 void StopSink();
136 // Fires |ended_cb_| if there are no remaining usable frames and
137 // |received_end_of_stream_| is true. Sets |rendered_end_of_stream_| if it
138 // does so.
140 // When called from the media thread, |time_progressing| should reflect the
141 // value of |time_progressing_|. When called from Render() on the sink
142 // callback thread, the inverse of |render_first_frame_and_stop_| should be
143 // used as a proxy for |time_progressing_|.
145 // Returns algorithm_->EffectiveFramesQueued().
146 size_t MaybeFireEndedCallback_Locked(bool time_progressing);
148 // Helper method for converting a single media timestamp to wall clock time.
149 base::TimeTicks ConvertMediaTimestamp(base::TimeDelta media_timestamp);
151 scoped_refptr<base::SingleThreadTaskRunner> task_runner_;
153 // Sink which calls into VideoRendererImpl via Render() for video frames. Do
154 // not call any methods on the sink while |lock_| is held or the two threads
155 // might deadlock. Do not call Start() or Stop() on the sink directly, use
156 // StartSink() and StopSink() to ensure background rendering is started. Only
157 // access these values on |task_runner_|.
158 VideoRendererSink* const sink_;
159 bool sink_started_;
161 // Used for accessing data members.
162 base::Lock lock_;
164 // Provides video frames to VideoRendererImpl.
165 scoped_ptr<VideoFrameStream> video_frame_stream_;
167 // Pool of GpuMemoryBuffers and resources used to create hardware frames.
168 scoped_ptr<GpuMemoryBufferVideoFramePool> gpu_memory_buffer_pool_;
170 scoped_refptr<MediaLog> media_log_;
172 // Flag indicating low-delay mode.
173 bool low_delay_;
175 // Keeps track of whether we received the end of stream buffer and finished
176 // rendering.
177 bool received_end_of_stream_;
178 bool rendered_end_of_stream_;
180 // Important detail: being in kPlaying doesn't imply that video is being
181 // rendered. Rather, it means that the renderer is ready to go. The actual
182 // rendering of video is controlled by time advancing via |get_time_cb_|.
184 // kUninitialized
185 // | Initialize()
186 // |
187 // V
188 // kInitializing
189 // | Decoders initialized
190 // |
191 // V Decoders reset
192 // kFlushed <------------------ kFlushing
193 // | StartPlayingFrom() ^
194 // | |
195 // | | Flush()
196 // `---------> kPlaying --------'
197 enum State {
198 kUninitialized,
199 kInitializing,
200 kFlushing,
201 kFlushed,
202 kPlaying
204 State state_;
206 // An integer that represents how many times the video frame stream has been
207 // reset. This is useful when doing video frame copies asynchronously since we
208 // want to discard video frames that might be received after the stream has
209 // been reset.
210 uint32_t sequence_token_;
212 // Keep track of the outstanding read on the VideoFrameStream. Flushing can
213 // only complete once the read has completed.
214 bool pending_read_;
216 bool drop_frames_;
218 BufferingState buffering_state_;
220 // Playback operation callbacks.
221 base::Closure flush_cb_;
223 // Event callbacks.
224 PipelineStatusCB init_cb_;
225 StatisticsCB statistics_cb_;
226 BufferingStateCB buffering_state_cb_;
227 base::Closure ended_cb_;
228 PipelineStatusCB error_cb_;
229 TimeSource::WallClockTimeCB wall_clock_time_cb_;
231 base::TimeDelta start_timestamp_;
233 // Keeps track of the number of frames decoded and dropped since the
234 // last call to |statistics_cb_|. These must be accessed under lock.
235 int frames_decoded_;
236 int frames_dropped_;
238 scoped_ptr<base::TickClock> tick_clock_;
240 // Algorithm for selecting which frame to render; manages frames and all
241 // timing related information.
242 scoped_ptr<VideoRendererAlgorithm> algorithm_;
244 // Indicates that Render() was called with |background_rendering| set to true,
245 // so we've entered a background rendering mode where dropped frames are not
246 // counted. Must be accessed under |lock_| once |sink_| is started.
247 bool was_background_rendering_;
249 // Indicates whether or not media time is currently progressing or not. Must
250 // only be accessed from |task_runner_|.
251 bool time_progressing_;
253 // Indicates that Render() should only render the first frame and then request
254 // that the sink be stopped. |posted_maybe_stop_after_first_paint_| is used
255 // to avoid repeated task posts.
256 bool render_first_frame_and_stop_;
257 bool posted_maybe_stop_after_first_paint_;
259 // NOTE: Weak pointers must be invalidated before all other member variables.
260 base::WeakPtrFactory<VideoRendererImpl> weak_factory_;
262 DISALLOW_COPY_AND_ASSIGN(VideoRendererImpl);
265 } // namespace media
267 #endif // MEDIA_RENDERERS_VIDEO_RENDERER_IMPL_H_