Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(140)

Side by Side Diff: media/renderers/video_renderer_impl.cc

Issue 1027553002: Change the TimeSource interface to return wallclock time for video. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Remove underflow changes. Created 5 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « media/renderers/video_renderer_impl.h ('k') | media/renderers/video_renderer_impl_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 #include "media/renderers/video_renderer_impl.h" 5 #include "media/renderers/video_renderer_impl.h"
6 6
7 #include "base/bind.h" 7 #include "base/bind.h"
8 #include "base/callback.h" 8 #include "base/callback.h"
9 #include "base/callback_helpers.h" 9 #include "base/callback_helpers.h"
10 #include "base/location.h" 10 #include "base/location.h"
11 #include "base/single_thread_task_runner.h" 11 #include "base/single_thread_task_runner.h"
12 #include "base/threading/platform_thread.h" 12 #include "base/threading/platform_thread.h"
13 #include "base/time/default_tick_clock.h"
13 #include "base/trace_event/trace_event.h" 14 #include "base/trace_event/trace_event.h"
14 #include "media/base/bind_to_current_loop.h" 15 #include "media/base/bind_to_current_loop.h"
15 #include "media/base/buffers.h" 16 #include "media/base/buffers.h"
16 #include "media/base/limits.h" 17 #include "media/base/limits.h"
17 #include "media/base/pipeline.h" 18 #include "media/base/pipeline.h"
18 #include "media/base/video_frame.h" 19 #include "media/base/video_frame.h"
19 20
20 namespace media { 21 namespace media {
21 22
22 VideoRendererImpl::VideoRendererImpl( 23 VideoRendererImpl::VideoRendererImpl(
23 const scoped_refptr<base::SingleThreadTaskRunner>& task_runner, 24 const scoped_refptr<base::SingleThreadTaskRunner>& task_runner,
24 ScopedVector<VideoDecoder> decoders, 25 ScopedVector<VideoDecoder> decoders,
25 bool drop_frames, 26 bool drop_frames,
26 const scoped_refptr<MediaLog>& media_log) 27 const scoped_refptr<MediaLog>& media_log)
27 : task_runner_(task_runner), 28 : task_runner_(task_runner),
28 video_frame_stream_( 29 video_frame_stream_(
29 new VideoFrameStream(task_runner, decoders.Pass(), media_log)), 30 new VideoFrameStream(task_runner, decoders.Pass(), media_log)),
30 low_delay_(false), 31 low_delay_(false),
31 received_end_of_stream_(false), 32 received_end_of_stream_(false),
32 rendered_end_of_stream_(false), 33 rendered_end_of_stream_(false),
33 frame_available_(&lock_), 34 frame_available_(&lock_),
34 state_(kUninitialized), 35 state_(kUninitialized),
35 thread_(), 36 thread_(),
36 pending_read_(false), 37 pending_read_(false),
37 drop_frames_(drop_frames), 38 drop_frames_(drop_frames),
38 buffering_state_(BUFFERING_HAVE_NOTHING), 39 buffering_state_(BUFFERING_HAVE_NOTHING),
39 last_timestamp_(kNoTimestamp()),
40 last_painted_timestamp_(kNoTimestamp()),
41 frames_decoded_(0), 40 frames_decoded_(0),
42 frames_dropped_(0), 41 frames_dropped_(0),
43 is_shutting_down_(false), 42 is_shutting_down_(false),
43 tick_clock_(new base::DefaultTickClock()),
44 weak_factory_(this) { 44 weak_factory_(this) {
45 } 45 }
46 46
47 VideoRendererImpl::~VideoRendererImpl() { 47 VideoRendererImpl::~VideoRendererImpl() {
48 DCHECK(task_runner_->BelongsToCurrentThread()); 48 DCHECK(task_runner_->BelongsToCurrentThread());
49 49
50 { 50 {
51 base::AutoLock auto_lock(lock_); 51 base::AutoLock auto_lock(lock_);
52 is_shutting_down_ = true; 52 is_shutting_down_ = true;
53 frame_available_.Signal(); 53 frame_available_.Signal();
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after
102 102
103 void VideoRendererImpl::Initialize( 103 void VideoRendererImpl::Initialize(
104 DemuxerStream* stream, 104 DemuxerStream* stream,
105 const PipelineStatusCB& init_cb, 105 const PipelineStatusCB& init_cb,
106 const SetDecryptorReadyCB& set_decryptor_ready_cb, 106 const SetDecryptorReadyCB& set_decryptor_ready_cb,
107 const StatisticsCB& statistics_cb, 107 const StatisticsCB& statistics_cb,
108 const BufferingStateCB& buffering_state_cb, 108 const BufferingStateCB& buffering_state_cb,
109 const PaintCB& paint_cb, 109 const PaintCB& paint_cb,
110 const base::Closure& ended_cb, 110 const base::Closure& ended_cb,
111 const PipelineStatusCB& error_cb, 111 const PipelineStatusCB& error_cb,
112 const TimeDeltaCB& get_time_cb, 112 const WallClockTimeCB& wall_clock_time_cb,
113 const base::Closure& waiting_for_decryption_key_cb) { 113 const base::Closure& waiting_for_decryption_key_cb) {
114 DCHECK(task_runner_->BelongsToCurrentThread()); 114 DCHECK(task_runner_->BelongsToCurrentThread());
115 base::AutoLock auto_lock(lock_); 115 base::AutoLock auto_lock(lock_);
116 DCHECK(stream); 116 DCHECK(stream);
117 DCHECK_EQ(stream->type(), DemuxerStream::VIDEO); 117 DCHECK_EQ(stream->type(), DemuxerStream::VIDEO);
118 DCHECK(!init_cb.is_null()); 118 DCHECK(!init_cb.is_null());
119 DCHECK(!statistics_cb.is_null()); 119 DCHECK(!statistics_cb.is_null());
120 DCHECK(!buffering_state_cb.is_null()); 120 DCHECK(!buffering_state_cb.is_null());
121 DCHECK(!paint_cb.is_null()); 121 DCHECK(!paint_cb.is_null());
122 DCHECK(!ended_cb.is_null()); 122 DCHECK(!ended_cb.is_null());
123 DCHECK(!get_time_cb.is_null()); 123 DCHECK(!wall_clock_time_cb.is_null());
124 DCHECK_EQ(kUninitialized, state_); 124 DCHECK_EQ(kUninitialized, state_);
125 125
126 low_delay_ = (stream->liveness() == DemuxerStream::LIVENESS_LIVE); 126 low_delay_ = (stream->liveness() == DemuxerStream::LIVENESS_LIVE);
127 127
128 // Always post |init_cb_| because |this| could be destroyed if initialization 128 // Always post |init_cb_| because |this| could be destroyed if initialization
129 // failed. 129 // failed.
130 init_cb_ = BindToCurrentLoop(init_cb); 130 init_cb_ = BindToCurrentLoop(init_cb);
131 131
132 statistics_cb_ = statistics_cb; 132 statistics_cb_ = statistics_cb;
133 buffering_state_cb_ = buffering_state_cb; 133 buffering_state_cb_ = buffering_state_cb;
134 paint_cb_ = paint_cb, 134 paint_cb_ = paint_cb,
135 ended_cb_ = ended_cb; 135 ended_cb_ = ended_cb;
136 error_cb_ = error_cb; 136 error_cb_ = error_cb;
137 get_time_cb_ = get_time_cb; 137 wall_clock_time_cb_ = wall_clock_time_cb;
138 state_ = kInitializing; 138 state_ = kInitializing;
139 139
140 video_frame_stream_->Initialize( 140 video_frame_stream_->Initialize(
141 stream, base::Bind(&VideoRendererImpl::OnVideoFrameStreamInitialized, 141 stream, base::Bind(&VideoRendererImpl::OnVideoFrameStreamInitialized,
142 weak_factory_.GetWeakPtr()), 142 weak_factory_.GetWeakPtr()),
143 set_decryptor_ready_cb, statistics_cb, waiting_for_decryption_key_cb); 143 set_decryptor_ready_cb, statistics_cb, waiting_for_decryption_key_cb);
144 } 144 }
145 145
146 void VideoRendererImpl::CreateVideoThread() { 146 void VideoRendererImpl::CreateVideoThread() {
147 // This may fail and cause a crash if there are too many threads created in 147 // This may fail and cause a crash if there are too many threads created in
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after
201 // Thread exit condition. 201 // Thread exit condition.
202 if (is_shutting_down_) 202 if (is_shutting_down_)
203 return; 203 return;
204 204
205 // Remain idle as long as we're not playing. 205 // Remain idle as long as we're not playing.
206 if (state_ != kPlaying || buffering_state_ != BUFFERING_HAVE_ENOUGH) { 206 if (state_ != kPlaying || buffering_state_ != BUFFERING_HAVE_ENOUGH) {
207 UpdateStatsAndWait_Locked(kIdleTimeDelta); 207 UpdateStatsAndWait_Locked(kIdleTimeDelta);
208 continue; 208 continue;
209 } 209 }
210 210
211 base::TimeDelta now = get_time_cb_.Run(); 211 base::TimeTicks now = tick_clock_->NowTicks();
212 212
213 // Remain idle until we have the next frame ready for rendering. 213 // Remain idle until we have the next frame ready for rendering.
214 if (ready_frames_.empty()) { 214 if (ready_frames_.empty()) {
215 if (received_end_of_stream_) { 215 if (received_end_of_stream_) {
216 if (!rendered_end_of_stream_) { 216 if (!rendered_end_of_stream_) {
217 rendered_end_of_stream_ = true; 217 rendered_end_of_stream_ = true;
218 task_runner_->PostTask(FROM_HERE, ended_cb_); 218 task_runner_->PostTask(FROM_HERE, ended_cb_);
219 } 219 }
220 } else if (last_painted_timestamp_ != kNoTimestamp() && 220 } else if (!last_painted_time_.is_null() &&
221 now - last_painted_timestamp_ >= kTimeToDeclareHaveNothing) { 221 now - last_painted_time_ >= kTimeToDeclareHaveNothing) {
222 buffering_state_ = BUFFERING_HAVE_NOTHING; 222 buffering_state_ = BUFFERING_HAVE_NOTHING;
223 task_runner_->PostTask( 223 task_runner_->PostTask(
224 FROM_HERE, base::Bind(buffering_state_cb_, BUFFERING_HAVE_NOTHING)); 224 FROM_HERE, base::Bind(buffering_state_cb_, BUFFERING_HAVE_NOTHING));
225 } 225 }
226 226
227 UpdateStatsAndWait_Locked(kIdleTimeDelta); 227 UpdateStatsAndWait_Locked(kIdleTimeDelta);
228 continue; 228 continue;
229 } 229 }
230 230
231 base::TimeDelta target_paint_timestamp = ready_frames_.front()->timestamp(); 231 base::TimeTicks target_paint_time =
232 base::TimeDelta latest_paint_timestamp; 232 wall_clock_time_cb_.Run(ready_frames_.front()->timestamp());
233
234 // If media time has stopped, don't attempt to paint any more frames.
235 if (target_paint_time.is_null()) {
236 UpdateStatsAndWait_Locked(kIdleTimeDelta);
237 continue;
238 }
239
240 base::TimeTicks latest_possible_paint_time;
233 241
234 // Deadline is defined as the duration between this frame and the next 242 // Deadline is defined as the duration between this frame and the next
235 // frame, using the delta between this frame and the previous frame as the 243 // frame, using the delta between this frame and the previous frame as the
236 // assumption for frame duration. 244 // assumption for frame duration.
237 // 245 //
238 // TODO(scherkus): This can be vastly improved. Use a histogram to measure 246 // TODO(scherkus): This can be vastly improved. Use a histogram to measure
239 // the accuracy of our frame timing code. http://crbug.com/149829 247 // the accuracy of our frame timing code. http://crbug.com/149829
240 if (last_timestamp_ == kNoTimestamp()) { 248 if (last_media_time_.is_null()) {
241 latest_paint_timestamp = base::TimeDelta::Max(); 249 latest_possible_paint_time = now;
242 } else { 250 } else {
243 base::TimeDelta duration = target_paint_timestamp - last_timestamp_; 251 base::TimeDelta duration = target_paint_time - last_media_time_;
244 latest_paint_timestamp = target_paint_timestamp + duration; 252 latest_possible_paint_time = target_paint_time + duration;
245 } 253 }
246 254
247 // Remain idle until we've reached our target paint window. 255 // Remain idle until we've reached our target paint window.
248 if (now < target_paint_timestamp) { 256 if (now < target_paint_time) {
249 UpdateStatsAndWait_Locked( 257 UpdateStatsAndWait_Locked(
250 std::min(target_paint_timestamp - now, kIdleTimeDelta)); 258 std::min(target_paint_time - now, kIdleTimeDelta));
251 continue; 259 continue;
252 } 260 }
253 261
254 if (ready_frames_.size() > 1 && now > latest_paint_timestamp && 262 if (ready_frames_.size() > 1 && now > latest_possible_paint_time &&
255 drop_frames_) { 263 drop_frames_) {
256 DropNextReadyFrame_Locked(); 264 DropNextReadyFrame_Locked();
257 continue; 265 continue;
258 } 266 }
259 267
260 // Congratulations! You've made it past the video frame timing gauntlet. 268 // Congratulations! You've made it past the video frame timing gauntlet.
261 // 269 //
262 // At this point enough time has passed that the next frame that ready for 270 // At this point enough time has passed that the next frame that ready for
263 // rendering. 271 // rendering.
264 PaintNextReadyFrame_Locked(); 272 PaintNextReadyFrame_Locked();
265 } 273 }
266 } 274 }
267 275
276 void VideoRendererImpl::SetTickClockForTesting(
277 scoped_ptr<base::TickClock> tick_clock) {
278 tick_clock_.swap(tick_clock);
279 }
280
268 void VideoRendererImpl::PaintNextReadyFrame_Locked() { 281 void VideoRendererImpl::PaintNextReadyFrame_Locked() {
269 lock_.AssertAcquired(); 282 lock_.AssertAcquired();
270 283
271 scoped_refptr<VideoFrame> next_frame = ready_frames_.front(); 284 scoped_refptr<VideoFrame> next_frame = ready_frames_.front();
272 ready_frames_.pop_front(); 285 ready_frames_.pop_front();
273 frames_decoded_++; 286 frames_decoded_++;
274 287
275 last_timestamp_ = next_frame->timestamp(); 288 last_media_time_ = last_painted_time_ =
276 last_painted_timestamp_ = next_frame->timestamp(); 289 wall_clock_time_cb_.Run(next_frame->timestamp());
277 290
278 paint_cb_.Run(next_frame); 291 paint_cb_.Run(next_frame);
279 292
280 task_runner_->PostTask( 293 task_runner_->PostTask(
281 FROM_HERE, 294 FROM_HERE,
282 base::Bind(&VideoRendererImpl::AttemptRead, weak_factory_.GetWeakPtr())); 295 base::Bind(&VideoRendererImpl::AttemptRead, weak_factory_.GetWeakPtr()));
283 } 296 }
284 297
285 void VideoRendererImpl::DropNextReadyFrame_Locked() { 298 void VideoRendererImpl::DropNextReadyFrame_Locked() {
286 TRACE_EVENT0("media", "VideoRendererImpl:frameDropped"); 299 TRACE_EVENT0("media", "VideoRendererImpl:frameDropped");
287 300
288 lock_.AssertAcquired(); 301 lock_.AssertAcquired();
289 302
290 last_timestamp_ = ready_frames_.front()->timestamp(); 303 last_media_time_ =
304 wall_clock_time_cb_.Run(ready_frames_.front()->timestamp());
305
291 ready_frames_.pop_front(); 306 ready_frames_.pop_front();
292 frames_decoded_++; 307 frames_decoded_++;
293 frames_dropped_++; 308 frames_dropped_++;
294 309
295 task_runner_->PostTask( 310 task_runner_->PostTask(
296 FROM_HERE, 311 FROM_HERE,
297 base::Bind(&VideoRendererImpl::AttemptRead, weak_factory_.GetWeakPtr())); 312 base::Bind(&VideoRendererImpl::AttemptRead, weak_factory_.GetWeakPtr()));
298 } 313 }
299 314
300 void VideoRendererImpl::FrameReady(VideoFrameStream::Status status, 315 void VideoRendererImpl::FrameReady(VideoFrameStream::Status status,
(...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after
421 void VideoRendererImpl::OnVideoFrameStreamResetDone() { 436 void VideoRendererImpl::OnVideoFrameStreamResetDone() {
422 base::AutoLock auto_lock(lock_); 437 base::AutoLock auto_lock(lock_);
423 DCHECK_EQ(kFlushing, state_); 438 DCHECK_EQ(kFlushing, state_);
424 DCHECK(!pending_read_); 439 DCHECK(!pending_read_);
425 DCHECK(ready_frames_.empty()); 440 DCHECK(ready_frames_.empty());
426 DCHECK(!received_end_of_stream_); 441 DCHECK(!received_end_of_stream_);
427 DCHECK(!rendered_end_of_stream_); 442 DCHECK(!rendered_end_of_stream_);
428 DCHECK_EQ(buffering_state_, BUFFERING_HAVE_NOTHING); 443 DCHECK_EQ(buffering_state_, BUFFERING_HAVE_NOTHING);
429 444
430 state_ = kFlushed; 445 state_ = kFlushed;
431 last_timestamp_ = kNoTimestamp(); 446 last_media_time_ = last_painted_time_ = base::TimeTicks();
432 last_painted_timestamp_ = kNoTimestamp();
433 base::ResetAndReturn(&flush_cb_).Run(); 447 base::ResetAndReturn(&flush_cb_).Run();
434 } 448 }
435 449
436 void VideoRendererImpl::UpdateStatsAndWait_Locked( 450 void VideoRendererImpl::UpdateStatsAndWait_Locked(
437 base::TimeDelta wait_duration) { 451 base::TimeDelta wait_duration) {
438 lock_.AssertAcquired(); 452 lock_.AssertAcquired();
439 DCHECK_GE(frames_decoded_, 0); 453 DCHECK_GE(frames_decoded_, 0);
440 DCHECK_LE(frames_dropped_, frames_decoded_); 454 DCHECK_LE(frames_dropped_, frames_decoded_);
441 455
442 if (frames_decoded_) { 456 if (frames_decoded_) {
443 PipelineStatistics statistics; 457 PipelineStatistics statistics;
444 statistics.video_frames_decoded = frames_decoded_; 458 statistics.video_frames_decoded = frames_decoded_;
445 statistics.video_frames_dropped = frames_dropped_; 459 statistics.video_frames_dropped = frames_dropped_;
446 task_runner_->PostTask(FROM_HERE, base::Bind(statistics_cb_, statistics)); 460 task_runner_->PostTask(FROM_HERE, base::Bind(statistics_cb_, statistics));
447 461
448 frames_decoded_ = 0; 462 frames_decoded_ = 0;
449 frames_dropped_ = 0; 463 frames_dropped_ = 0;
450 } 464 }
451 465
452 frame_available_.TimedWait(wait_duration); 466 frame_available_.TimedWait(wait_duration);
453 } 467 }
454 468
455 } // namespace media 469 } // namespace media
OLDNEW
« no previous file with comments | « media/renderers/video_renderer_impl.h ('k') | media/renderers/video_renderer_impl_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698