OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef MEDIA_RENDERERS_VIDEO_RENDERER_IMPL_H_ | 5 #ifndef MEDIA_RENDERERS_VIDEO_RENDERER_IMPL_H_ |
6 #define MEDIA_RENDERERS_VIDEO_RENDERER_IMPL_H_ | 6 #define MEDIA_RENDERERS_VIDEO_RENDERER_IMPL_H_ |
7 | 7 |
8 #include <deque> | 8 #include <deque> |
9 | 9 |
10 #include "base/memory/ref_counted.h" | 10 #include "base/memory/ref_counted.h" |
11 #include "base/memory/scoped_ptr.h" | 11 #include "base/memory/scoped_ptr.h" |
12 #include "base/memory/scoped_vector.h" | 12 #include "base/memory/scoped_vector.h" |
13 #include "base/memory/weak_ptr.h" | 13 #include "base/memory/weak_ptr.h" |
14 #include "base/synchronization/condition_variable.h" | 14 #include "base/synchronization/condition_variable.h" |
15 #include "base/synchronization/lock.h" | 15 #include "base/synchronization/lock.h" |
16 #include "base/threading/platform_thread.h" | 16 #include "base/threading/platform_thread.h" |
| 17 #include "base/timer/timer.h" |
17 #include "media/base/decryptor.h" | 18 #include "media/base/decryptor.h" |
18 #include "media/base/demuxer_stream.h" | 19 #include "media/base/demuxer_stream.h" |
19 #include "media/base/media_log.h" | 20 #include "media/base/media_log.h" |
20 #include "media/base/pipeline_status.h" | 21 #include "media/base/pipeline_status.h" |
21 #include "media/base/video_decoder.h" | 22 #include "media/base/video_decoder.h" |
22 #include "media/base/video_frame.h" | 23 #include "media/base/video_frame.h" |
23 #include "media/base/video_renderer.h" | 24 #include "media/base/video_renderer.h" |
24 #include "media/base/video_renderer_sink.h" | 25 #include "media/base/video_renderer_sink.h" |
25 #include "media/filters/decoder_stream.h" | 26 #include "media/filters/decoder_stream.h" |
| 27 #include "media/filters/video_renderer_algorithm.h" |
26 | 28 |
27 namespace base { | 29 namespace base { |
28 class SingleThreadTaskRunner; | 30 class SingleThreadTaskRunner; |
29 class TickClock; | 31 class TickClock; |
30 } | 32 } |
31 | 33 |
32 namespace media { | 34 namespace media { |
33 | 35 |
34 // VideoRendererImpl creates its own thread for the sole purpose of timing frame | 36 // VideoRendererImpl creates its own thread for the sole purpose of timing frame |
35 // presentation. It handles reading from the VideoFrameStream and stores the | 37 // presentation. It handles reading from the VideoFrameStream and stores the |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
72 // PlatformThread::Delegate implementation. | 74 // PlatformThread::Delegate implementation. |
73 void ThreadMain() override; | 75 void ThreadMain() override; |
74 | 76 |
75 void SetTickClockForTesting(scoped_ptr<base::TickClock> tick_clock); | 77 void SetTickClockForTesting(scoped_ptr<base::TickClock> tick_clock); |
76 | 78 |
77 // VideoRendererSink::RenderCallback implementation. | 79 // VideoRendererSink::RenderCallback implementation. |
78 scoped_refptr<VideoFrame> Render(base::TimeTicks deadline_min, | 80 scoped_refptr<VideoFrame> Render(base::TimeTicks deadline_min, |
79 base::TimeTicks deadline_max) override; | 81 base::TimeTicks deadline_max) override; |
80 void OnFrameDropped() override; | 82 void OnFrameDropped() override; |
81 | 83 |
| 84 void enable_new_video_renderer_for_testing() { |
| 85 use_new_video_renderering_path_ = true; |
| 86 } |
| 87 |
| 88 void SetBackgroundRenderingForTesting(bool enabled, base::TimeDelta timeout); |
| 89 |
82 private: | 90 private: |
83 // Creates a dedicated |thread_| for video rendering. | 91 // Creates a dedicated |thread_| for video rendering. |
84 void CreateVideoThread(); | 92 void CreateVideoThread(); |
85 | 93 |
86 // Callback for |video_frame_stream_| initialization. | 94 // Callback for |video_frame_stream_| initialization. |
87 void OnVideoFrameStreamInitialized(bool success); | 95 void OnVideoFrameStreamInitialized(bool success); |
88 | 96 |
89 // Callback for |video_frame_stream_| to deliver decoded video frames and | 97 // Callback for |video_frame_stream_| to deliver decoded video frames and |
90 // report video decoding status. | 98 // report video decoding status. |
91 void FrameReady(VideoFrameStream::Status status, | 99 void FrameReady(VideoFrameStream::Status status, |
(...skipping 24 matching lines...) Expand all Loading... |
116 // Returns true if the renderer has enough data for playback purposes. | 124 // 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. | 125 // Note that having enough data may be due to reaching end of stream. |
118 bool HaveEnoughData_Locked(); | 126 bool HaveEnoughData_Locked(); |
119 void TransitionToHaveEnough_Locked(); | 127 void TransitionToHaveEnough_Locked(); |
120 | 128 |
121 // Runs |statistics_cb_| with |frames_decoded_| and |frames_dropped_|, resets | 129 // Runs |statistics_cb_| with |frames_decoded_| and |frames_dropped_|, resets |
122 // them to 0, and then waits on |frame_available_| for up to the | 130 // them to 0, and then waits on |frame_available_| for up to the |
123 // |wait_duration|. | 131 // |wait_duration|. |
124 void UpdateStatsAndWait_Locked(base::TimeDelta wait_duration); | 132 void UpdateStatsAndWait_Locked(base::TimeDelta wait_duration); |
125 | 133 |
| 134 // Called after we've painted the first frame. If |time_progressing_| is |
| 135 // false it Stop() on |sink_|. |
| 136 void MaybeStopSinkAfterFirstPaint(); |
| 137 |
| 138 // Resets and primes the |background_rendering_timer_|, when the timer fires |
| 139 // it calls the BackgroundRender() method below. |
| 140 void RestartBackgroundRenderTimer(); |
| 141 |
| 142 // Called by |background_rendering_timer_| when enough time elapses where we |
| 143 // haven't seen a Render() call. |
| 144 void BackgroundRender(); |
| 145 void BackgroundRender_Locked(); |
| 146 |
| 147 // Return true if there is no more room for additional buffered frames. |
| 148 bool HaveReachedBufferingCap(); |
| 149 |
| 150 // Starts or stops |sink_| respectively. Do not call while |lock_| is held. |
| 151 void StartSink(); |
| 152 void StopSink(); |
| 153 |
| 154 // Fires |ended_cb_| if there are no remaining usable frames and |
| 155 // |received_end_of_stream_| is true. Sets |rendered_end_of_stream_| if it |
| 156 // does so. Returns algorithm_->EffectiveFramesQueued(). |
| 157 size_t MaybeFireEndedCallback(); |
| 158 |
126 scoped_refptr<base::SingleThreadTaskRunner> task_runner_; | 159 scoped_refptr<base::SingleThreadTaskRunner> task_runner_; |
127 | 160 |
| 161 // Enables the use of VideoRendererAlgorithm and VideoRendererSink for frame |
| 162 // rendering instead of using a thread in a sleep-loop. Set via the command |
| 163 // line flag kEnableNewVideoRenderer or via test methods. |
| 164 bool use_new_video_renderering_path_; |
| 165 |
| 166 // Sink which calls into VideoRendererImpl via Render() for video frames. Do |
| 167 // not call any methods on the sink while |lock_| is held or the two threads |
| 168 // might deadlock. Do not call Start() or Stop() on the sink directly, use |
| 169 // StartSink() and StopSink() to ensure background rendering is started. |
128 VideoRendererSink* const sink_; | 170 VideoRendererSink* const sink_; |
| 171 bool sink_started_; |
129 | 172 |
130 // Used for accessing data members. | 173 // Used for accessing data members. |
131 base::Lock lock_; | 174 base::Lock lock_; |
132 | 175 |
133 // Provides video frames to VideoRendererImpl. | 176 // Provides video frames to VideoRendererImpl. |
134 scoped_ptr<VideoFrameStream> video_frame_stream_; | 177 scoped_ptr<VideoFrameStream> video_frame_stream_; |
135 | 178 |
136 // Flag indicating low-delay mode. | 179 // Flag indicating low-delay mode. |
137 bool low_delay_; | 180 bool low_delay_; |
138 | 181 |
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
212 | 255 |
213 // Keeps track of the number of frames decoded and dropped since the | 256 // Keeps track of the number of frames decoded and dropped since the |
214 // last call to |statistics_cb_|. These must be accessed under lock. | 257 // last call to |statistics_cb_|. These must be accessed under lock. |
215 int frames_decoded_; | 258 int frames_decoded_; |
216 int frames_dropped_; | 259 int frames_dropped_; |
217 | 260 |
218 bool is_shutting_down_; | 261 bool is_shutting_down_; |
219 | 262 |
220 scoped_ptr<base::TickClock> tick_clock_; | 263 scoped_ptr<base::TickClock> tick_clock_; |
221 | 264 |
| 265 // Algorithm for selecting which frame to render; manages frames and all |
| 266 // timing related information. |
| 267 scoped_ptr<VideoRendererAlgorithm> algorithm_; |
| 268 |
| 269 // Indicates that Render() callbacks from |sink_| have timed out, so we've |
| 270 // entered a background rendering mode where dropped frames are not counted. |
| 271 bool is_background_rendering_; |
| 272 |
| 273 // Allows tests to disable the background rendering task. |
| 274 bool should_use_background_renderering_; |
| 275 |
| 276 // Manages expiration of stale video frames if Render() callbacks timeout. Do |
| 277 // not access from the thread Render() is called on. Must only be used on |
| 278 // |task_runner_|. |
| 279 base::OneShotTimer<VideoRendererImpl> background_rendering_timer_; |
| 280 |
| 281 // Indicates whether or not media time is currently progressing or not. |
| 282 bool time_progressing_; |
| 283 |
| 284 // Indicates that Render() should only render the first frame and then request |
| 285 // that the sink be stopped. |
| 286 bool render_first_frame_and_stop_; |
| 287 |
| 288 // The time to wait for Render() before firing BackgroundRender(). |
| 289 base::TimeDelta background_rendering_timeout_; |
| 290 |
222 // NOTE: Weak pointers must be invalidated before all other member variables. | 291 // NOTE: Weak pointers must be invalidated before all other member variables. |
223 base::WeakPtrFactory<VideoRendererImpl> weak_factory_; | 292 base::WeakPtrFactory<VideoRendererImpl> weak_factory_; |
224 | 293 |
225 DISALLOW_COPY_AND_ASSIGN(VideoRendererImpl); | 294 DISALLOW_COPY_AND_ASSIGN(VideoRendererImpl); |
226 }; | 295 }; |
227 | 296 |
228 } // namespace media | 297 } // namespace media |
229 | 298 |
230 #endif // MEDIA_RENDERERS_VIDEO_RENDERER_IMPL_H_ | 299 #endif // MEDIA_RENDERERS_VIDEO_RENDERER_IMPL_H_ |
OLD | NEW |