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_
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"
28 class SingleThreadTaskRunner
;
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
{
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.
51 const scoped_refptr
<base::SingleThreadTaskRunner
>& task_runner
,
52 VideoRendererSink
* sink
,
53 ScopedVector
<VideoDecoder
> decoders
,
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
;
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
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
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.
133 // Provides video frames to VideoRendererImpl.
134 scoped_ptr
<VideoFrameStream
> video_frame_stream_
;
136 // Flag indicating low-delay mode.
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
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_|.
161 // | Decoders initialized
164 // kFlushed <------------------ kFlushing
165 // | StartPlayingFrom() ^
168 // `---------> kPlaying --------'
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.
187 BufferingState buffering_state_
;
189 // Playback operation callbacks.
190 base::Closure flush_cb_
;
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.
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
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.
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
);
230 #endif // MEDIA_RENDERERS_VIDEO_RENDERER_IMPL_H_