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/filters/decoder_stream.h"
27 class SingleThreadTaskRunner
;
33 // VideoRendererImpl creates its own thread for the sole purpose of timing frame
34 // presentation. It handles reading from the VideoFrameStream and stores the
35 // results in a queue of decoded frames and executing a callback when a frame is
36 // ready for rendering.
37 class MEDIA_EXPORT VideoRendererImpl
38 : public VideoRenderer
,
39 public base::PlatformThread::Delegate
{
41 // |decoders| contains the VideoDecoders to use when initializing.
43 // Implementors should avoid doing any sort of heavy work in this method and
44 // instead post a task to a common/worker thread to handle rendering. Slowing
45 // down the video thread may result in losing synchronization with audio.
47 // Setting |drop_frames_| to true causes the renderer to drop expired frames.
49 const scoped_refptr
<base::SingleThreadTaskRunner
>& task_runner
,
50 ScopedVector
<VideoDecoder
> decoders
,
52 const scoped_refptr
<MediaLog
>& media_log
);
53 ~VideoRendererImpl() override
;
55 // VideoRenderer implementation.
56 void Initialize(DemuxerStream
* stream
,
57 const PipelineStatusCB
& init_cb
,
58 const SetDecryptorReadyCB
& set_decryptor_ready_cb
,
59 const StatisticsCB
& statistics_cb
,
60 const BufferingStateCB
& buffering_state_cb
,
61 const PaintCB
& paint_cb
,
62 const base::Closure
& ended_cb
,
63 const PipelineStatusCB
& error_cb
,
64 const WallClockTimeCB
& wall_clock_time_cb
,
65 const base::Closure
& waiting_for_decryption_key_cb
) override
;
66 void Flush(const base::Closure
& callback
) override
;
67 void StartPlayingFrom(base::TimeDelta timestamp
) override
;
68 void OnTimeStateChanged(bool time_progressing
) override
;
70 // PlatformThread::Delegate implementation.
71 void ThreadMain() override
;
73 void SetTickClockForTesting(scoped_ptr
<base::TickClock
> tick_clock
);
76 // Creates a dedicated |thread_| for video rendering.
77 void CreateVideoThread();
79 // Callback for |video_frame_stream_| initialization.
80 void OnVideoFrameStreamInitialized(bool success
);
82 // Callback for |video_frame_stream_| to deliver decoded video frames and
83 // report video decoding status.
84 void FrameReady(VideoFrameStream::Status status
,
85 const scoped_refptr
<VideoFrame
>& frame
);
87 // Helper method for adding a frame to |ready_frames_|.
88 void AddReadyFrame_Locked(const scoped_refptr
<VideoFrame
>& frame
);
90 // Helper method that schedules an asynchronous read from the
91 // |video_frame_stream_| as long as there isn't a pending read and we have
94 void AttemptRead_Locked();
96 // Called when VideoFrameStream::Reset() completes.
97 void OnVideoFrameStreamResetDone();
99 // Runs |paint_cb_| with the next frame from |ready_frames_|.
101 // A read is scheduled to replace the frame.
102 void PaintNextReadyFrame_Locked();
104 // Drops the next frame from |ready_frames_| and runs |statistics_cb_|.
106 // A read is scheduled to replace the frame.
107 void DropNextReadyFrame_Locked();
109 // Returns true if the renderer has enough data for playback purposes.
110 // Note that having enough data may be due to reaching end of stream.
111 bool HaveEnoughData_Locked();
112 void TransitionToHaveEnough_Locked();
114 // Runs |statistics_cb_| with |frames_decoded_| and |frames_dropped_|, resets
115 // them to 0, and then waits on |frame_available_| for up to the
117 void UpdateStatsAndWait_Locked(base::TimeDelta wait_duration
);
119 scoped_refptr
<base::SingleThreadTaskRunner
> task_runner_
;
121 // Used for accessing data members.
124 // Provides video frames to VideoRendererImpl.
125 scoped_ptr
<VideoFrameStream
> video_frame_stream_
;
127 // Flag indicating low-delay mode.
130 // Queue of incoming frames yet to be painted.
131 typedef std::deque
<scoped_refptr
<VideoFrame
> > VideoFrameQueue
;
132 VideoFrameQueue ready_frames_
;
134 // Keeps track of whether we received the end of stream buffer and finished
136 bool received_end_of_stream_
;
137 bool rendered_end_of_stream_
;
139 // Used to signal |thread_| as frames are added to |frames_|. Rule of thumb:
140 // always check |state_| to see if it was set to STOPPED after waking up!
141 base::ConditionVariable frame_available_
;
143 // Important detail: being in kPlaying doesn't imply that video is being
144 // rendered. Rather, it means that the renderer is ready to go. The actual
145 // rendering of video is controlled by time advancing via |get_time_cb_|.
152 // | Decoders initialized
155 // kFlushed <------------------ kFlushing
156 // | StartPlayingFrom() ^
159 // `---------> kPlaying --------'
169 // Video thread handle.
170 base::PlatformThreadHandle thread_
;
172 // Keep track of the outstanding read on the VideoFrameStream. Flushing can
173 // only complete once the read has completed.
178 BufferingState buffering_state_
;
180 // Playback operation callbacks.
181 base::Closure flush_cb_
;
184 PipelineStatusCB init_cb_
;
185 StatisticsCB statistics_cb_
;
186 BufferingStateCB buffering_state_cb_
;
187 base::Closure ended_cb_
;
188 PipelineStatusCB error_cb_
;
189 WallClockTimeCB wall_clock_time_cb_
;
191 base::TimeDelta start_timestamp_
;
193 // Embedder callback for notifying a new frame is available for painting.
196 // The wallclock times of the last frame removed from the |ready_frames_|
197 // queue, either for calling |paint_cb_| or for dropping. Set to null during
199 base::TimeTicks last_media_time_
;
201 // Equivalent to |last_media_time_| + the estimated duration of the frame.
202 base::TimeTicks latest_possible_paint_time_
;
204 // Keeps track of the number of frames decoded and dropped since the
205 // last call to |statistics_cb_|. These must be accessed under lock.
209 bool is_shutting_down_
;
211 scoped_ptr
<base::TickClock
> tick_clock_
;
213 // NOTE: Weak pointers must be invalidated before all other member variables.
214 base::WeakPtrFactory
<VideoRendererImpl
> weak_factory_
;
216 DISALLOW_COPY_AND_ASSIGN(VideoRendererImpl
);
221 #endif // MEDIA_RENDERERS_VIDEO_RENDERER_IMPL_H_