OLD | NEW |
| (Empty) |
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. | |
4 | |
5 #ifndef MEDIA_FILTERS_VIDEO_RENDERER_IMPL_H_ | |
6 #define MEDIA_FILTERS_VIDEO_RENDERER_IMPL_H_ | |
7 | |
8 #include <deque> | |
9 | |
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" | |
25 | |
26 namespace base { | |
27 class SingleThreadTaskRunner; | |
28 } | |
29 | |
30 namespace media { | |
31 | |
32 // VideoRendererImpl creates its own thread for the sole purpose of timing frame | |
33 // presentation. It handles reading from the VideoFrameStream and stores the | |
34 // results in a queue of decoded frames and executing a callback when a frame is | |
35 // ready for rendering. | |
36 class MEDIA_EXPORT VideoRendererImpl | |
37 : public VideoRenderer, | |
38 public base::PlatformThread::Delegate { | |
39 public: | |
40 // |decoders| contains the VideoDecoders to use when initializing. | |
41 // | |
42 // Implementors should avoid doing any sort of heavy work in this method and | |
43 // instead post a task to a common/worker thread to handle rendering. Slowing | |
44 // down the video thread may result in losing synchronization with audio. | |
45 // | |
46 // Setting |drop_frames_| to true causes the renderer to drop expired frames. | |
47 VideoRendererImpl( | |
48 const scoped_refptr<base::SingleThreadTaskRunner>& task_runner, | |
49 ScopedVector<VideoDecoder> decoders, | |
50 bool drop_frames, | |
51 const scoped_refptr<MediaLog>& media_log); | |
52 ~VideoRendererImpl() override; | |
53 | |
54 // VideoRenderer implementation. | |
55 void Initialize(DemuxerStream* stream, | |
56 const PipelineStatusCB& init_cb, | |
57 const SetDecryptorReadyCB& set_decryptor_ready_cb, | |
58 const StatisticsCB& statistics_cb, | |
59 const BufferingStateCB& buffering_state_cb, | |
60 const PaintCB& paint_cb, | |
61 const base::Closure& ended_cb, | |
62 const PipelineStatusCB& error_cb, | |
63 const TimeDeltaCB& get_time_cb) override; | |
64 void Flush(const base::Closure& callback) override; | |
65 void StartPlayingFrom(base::TimeDelta timestamp) override; | |
66 | |
67 // PlatformThread::Delegate implementation. | |
68 void ThreadMain() override; | |
69 | |
70 private: | |
71 // Creates a dedicated |thread_| for video rendering. | |
72 void CreateVideoThread(); | |
73 | |
74 // Callback for |video_frame_stream_| initialization. | |
75 void OnVideoFrameStreamInitialized(bool success); | |
76 | |
77 // Callback for |video_frame_stream_| to deliver decoded video frames and | |
78 // report video decoding status. | |
79 void FrameReady(VideoFrameStream::Status status, | |
80 const scoped_refptr<VideoFrame>& frame); | |
81 | |
82 // Helper method for adding a frame to |ready_frames_|. | |
83 void AddReadyFrame_Locked(const scoped_refptr<VideoFrame>& frame); | |
84 | |
85 // Helper method that schedules an asynchronous read from the | |
86 // |video_frame_stream_| as long as there isn't a pending read and we have | |
87 // capacity. | |
88 void AttemptRead(); | |
89 void AttemptRead_Locked(); | |
90 | |
91 // Called when VideoFrameStream::Reset() completes. | |
92 void OnVideoFrameStreamResetDone(); | |
93 | |
94 // Runs |paint_cb_| with the next frame from |ready_frames_|. | |
95 // | |
96 // A read is scheduled to replace the frame. | |
97 void PaintNextReadyFrame_Locked(); | |
98 | |
99 // Drops the next frame from |ready_frames_| and runs |statistics_cb_|. | |
100 // | |
101 // A read is scheduled to replace the frame. | |
102 void DropNextReadyFrame_Locked(); | |
103 | |
104 // Returns true if the renderer has enough data for playback purposes. | |
105 // Note that having enough data may be due to reaching end of stream. | |
106 bool HaveEnoughData_Locked(); | |
107 void TransitionToHaveEnough_Locked(); | |
108 | |
109 // Runs |statistics_cb_| with |frames_decoded_| and |frames_dropped_|, resets | |
110 // them to 0, and then waits on |frame_available_| for up to the | |
111 // |wait_duration|. | |
112 void UpdateStatsAndWait_Locked(base::TimeDelta wait_duration); | |
113 | |
114 scoped_refptr<base::SingleThreadTaskRunner> task_runner_; | |
115 | |
116 // Used for accessing data members. | |
117 base::Lock lock_; | |
118 | |
119 // Provides video frames to VideoRendererImpl. | |
120 scoped_ptr<VideoFrameStream> video_frame_stream_; | |
121 | |
122 // Flag indicating low-delay mode. | |
123 bool low_delay_; | |
124 | |
125 // Queue of incoming frames yet to be painted. | |
126 typedef std::deque<scoped_refptr<VideoFrame> > VideoFrameQueue; | |
127 VideoFrameQueue ready_frames_; | |
128 | |
129 // Keeps track of whether we received the end of stream buffer and finished | |
130 // rendering. | |
131 bool received_end_of_stream_; | |
132 bool rendered_end_of_stream_; | |
133 | |
134 // Used to signal |thread_| as frames are added to |frames_|. Rule of thumb: | |
135 // always check |state_| to see if it was set to STOPPED after waking up! | |
136 base::ConditionVariable frame_available_; | |
137 | |
138 // Important detail: being in kPlaying doesn't imply that video is being | |
139 // rendered. Rather, it means that the renderer is ready to go. The actual | |
140 // rendering of video is controlled by time advancing via |get_time_cb_|. | |
141 // | |
142 // kUninitialized | |
143 // | Initialize() | |
144 // | | |
145 // V | |
146 // kInitializing | |
147 // | Decoders initialized | |
148 // | | |
149 // V Decoders reset | |
150 // kFlushed <------------------ kFlushing | |
151 // | StartPlayingFrom() ^ | |
152 // | | | |
153 // | | Flush() | |
154 // `---------> kPlaying --------' | |
155 enum State { | |
156 kUninitialized, | |
157 kInitializing, | |
158 kFlushing, | |
159 kFlushed, | |
160 kPlaying | |
161 }; | |
162 State state_; | |
163 | |
164 // Video thread handle. | |
165 base::PlatformThreadHandle thread_; | |
166 | |
167 // Keep track of the outstanding read on the VideoFrameStream. Flushing can | |
168 // only complete once the read has completed. | |
169 bool pending_read_; | |
170 | |
171 bool drop_frames_; | |
172 | |
173 BufferingState buffering_state_; | |
174 | |
175 // Playback operation callbacks. | |
176 base::Closure flush_cb_; | |
177 | |
178 // Event callbacks. | |
179 PipelineStatusCB init_cb_; | |
180 StatisticsCB statistics_cb_; | |
181 BufferingStateCB buffering_state_cb_; | |
182 base::Closure ended_cb_; | |
183 PipelineStatusCB error_cb_; | |
184 TimeDeltaCB get_time_cb_; | |
185 | |
186 base::TimeDelta start_timestamp_; | |
187 | |
188 // Embedder callback for notifying a new frame is available for painting. | |
189 PaintCB paint_cb_; | |
190 | |
191 // The timestamp of the last frame removed from the |ready_frames_| queue, | |
192 // either for calling |paint_cb_| or for dropping. Set to kNoTimestamp() | |
193 // during flushing. | |
194 base::TimeDelta last_timestamp_; | |
195 | |
196 // The timestamp of the last successfully painted frame. Set to kNoTimestamp() | |
197 // during flushing. | |
198 base::TimeDelta last_painted_timestamp_; | |
199 | |
200 // Keeps track of the number of frames decoded and dropped since the | |
201 // last call to |statistics_cb_|. These must be accessed under lock. | |
202 int frames_decoded_; | |
203 int frames_dropped_; | |
204 | |
205 bool is_shutting_down_; | |
206 | |
207 // NOTE: Weak pointers must be invalidated before all other member variables. | |
208 base::WeakPtrFactory<VideoRendererImpl> weak_factory_; | |
209 | |
210 DISALLOW_COPY_AND_ASSIGN(VideoRendererImpl); | |
211 }; | |
212 | |
213 } // namespace media | |
214 | |
215 #endif // MEDIA_FILTERS_VIDEO_RENDERER_IMPL_H_ | |
OLD | NEW |