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

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: Fix comment. 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
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 TimeConverterCB& time_converter_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(!time_converter_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 time_converter_cb_ = time_converter_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 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
183 183
184 // The number of milliseconds to idle when we do not have anything to do. 184 // The number of milliseconds to idle when we do not have anything to do.
185 // Nothing special about the value, other than we're being more OS-friendly 185 // Nothing special about the value, other than we're being more OS-friendly
186 // than sleeping for 1 millisecond. 186 // than sleeping for 1 millisecond.
187 // 187 //
188 // TODO(scherkus): switch to pure event-driven frame timing instead of this 188 // TODO(scherkus): switch to pure event-driven frame timing instead of this
189 // kIdleTimeDelta business http://crbug.com/106874 189 // kIdleTimeDelta business http://crbug.com/106874
190 const base::TimeDelta kIdleTimeDelta = 190 const base::TimeDelta kIdleTimeDelta =
191 base::TimeDelta::FromMilliseconds(10); 191 base::TimeDelta::FromMilliseconds(10);
192 192
193 // If we have no frames and haven't painted any frame for certain amount of
194 // time, declare BUFFERING_HAVE_NOTHING.
195 const base::TimeDelta kTimeToDeclareHaveNothing =
196 base::TimeDelta::FromSeconds(3);
197
198 for (;;) { 193 for (;;) {
199 base::AutoLock auto_lock(lock_); 194 base::AutoLock auto_lock(lock_);
200 195
201 // Thread exit condition. 196 // Thread exit condition.
202 if (is_shutting_down_) 197 if (is_shutting_down_)
203 return; 198 return;
204 199
205 // Remain idle as long as we're not playing. 200 // Remain idle as long as we're not playing.
206 if (state_ != kPlaying || buffering_state_ != BUFFERING_HAVE_ENOUGH) { 201 if (state_ != kPlaying || buffering_state_ != BUFFERING_HAVE_ENOUGH) {
207 UpdateStatsAndWait_Locked(kIdleTimeDelta); 202 UpdateStatsAndWait_Locked(kIdleTimeDelta);
208 continue; 203 continue;
209 } 204 }
210 205
211 base::TimeDelta now = get_time_cb_.Run(); 206 base::TimeTicks now = tick_clock_->NowTicks();
212 207
213 // Remain idle until we have the next frame ready for rendering. 208 // Remain idle until we have the next frame ready for rendering.
214 if (ready_frames_.empty()) { 209 if (ready_frames_.empty()) {
215 if (received_end_of_stream_) { 210 if (received_end_of_stream_) {
216 if (!rendered_end_of_stream_) { 211 if (!rendered_end_of_stream_) {
217 rendered_end_of_stream_ = true; 212 rendered_end_of_stream_ = true;
218 task_runner_->PostTask(FROM_HERE, ended_cb_); 213 task_runner_->PostTask(FROM_HERE, ended_cb_);
219 } 214 }
220 } else if (last_painted_timestamp_ != kNoTimestamp() && 215 } else {
221 now - last_painted_timestamp_ >= kTimeToDeclareHaveNothing) {
222 buffering_state_ = BUFFERING_HAVE_NOTHING; 216 buffering_state_ = BUFFERING_HAVE_NOTHING;
223 task_runner_->PostTask( 217 task_runner_->PostTask(
224 FROM_HERE, base::Bind(buffering_state_cb_, BUFFERING_HAVE_NOTHING)); 218 FROM_HERE, base::Bind(buffering_state_cb_, BUFFERING_HAVE_NOTHING));
225 } 219 }
226 220
227 UpdateStatsAndWait_Locked(kIdleTimeDelta); 221 UpdateStatsAndWait_Locked(kIdleTimeDelta);
228 continue; 222 continue;
229 } 223 }
230 224
231 base::TimeDelta target_paint_timestamp = ready_frames_.front()->timestamp(); 225 base::TimeTicks target_paint_timestamp =
232 base::TimeDelta latest_paint_timestamp; 226 time_converter_cb_.Run(ready_frames_.front()->timestamp());
227
228 // If media time has stopped, don't attempt to paint any more frames.
229 if (target_paint_timestamp.is_null()) {
230 UpdateStatsAndWait_Locked(kIdleTimeDelta);
231 continue;
232 }
233
234 base::TimeTicks latest_paint_timestamp;
xhwang 2015/03/23 22:20:14 bikesheddings not related to your change: |latest
DaleCurtis 2015/03/25 00:31:45 Done; great suggestion!
233 235
234 // Deadline is defined as the duration between this frame and the next 236 // 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 237 // frame, using the delta between this frame and the previous frame as the
236 // assumption for frame duration. 238 // assumption for frame duration.
237 // 239 //
238 // TODO(scherkus): This can be vastly improved. Use a histogram to measure 240 // TODO(scherkus): This can be vastly improved. Use a histogram to measure
239 // the accuracy of our frame timing code. http://crbug.com/149829 241 // the accuracy of our frame timing code. http://crbug.com/149829
240 if (last_timestamp_ == kNoTimestamp()) { 242 if (last_timestamp_.is_null()) {
241 latest_paint_timestamp = base::TimeDelta::Max(); 243 latest_paint_timestamp = now;
242 } else { 244 } else {
243 base::TimeDelta duration = target_paint_timestamp - last_timestamp_; 245 base::TimeDelta duration = target_paint_timestamp - last_timestamp_;
244 latest_paint_timestamp = target_paint_timestamp + duration; 246 latest_paint_timestamp = target_paint_timestamp + duration;
245 } 247 }
246 248
247 // Remain idle until we've reached our target paint window. 249 // Remain idle until we've reached our target paint window.
248 if (now < target_paint_timestamp) { 250 if (now < target_paint_timestamp) {
249 UpdateStatsAndWait_Locked(kIdleTimeDelta); 251 UpdateStatsAndWait_Locked(kIdleTimeDelta);
250 continue; 252 continue;
251 } 253 }
252 254
253 if (now > latest_paint_timestamp && drop_frames_) { 255 if (now > latest_paint_timestamp && drop_frames_) {
254 DropNextReadyFrame_Locked(); 256 DropNextReadyFrame_Locked();
255 continue; 257 continue;
256 } 258 }
257 259
258 // Congratulations! You've made it past the video frame timing gauntlet. 260 // Congratulations! You've made it past the video frame timing gauntlet.
259 // 261 //
260 // At this point enough time has passed that the next frame that ready for 262 // At this point enough time has passed that the next frame that ready for
261 // rendering. 263 // rendering.
262 PaintNextReadyFrame_Locked(); 264 PaintNextReadyFrame_Locked();
263 } 265 }
264 } 266 }
265 267
268 void VideoRendererImpl::SetTickClockForTesting(
269 scoped_ptr<base::TickClock> tick_clock) {
270 tick_clock_.swap(tick_clock);
271 }
272
266 void VideoRendererImpl::PaintNextReadyFrame_Locked() { 273 void VideoRendererImpl::PaintNextReadyFrame_Locked() {
267 lock_.AssertAcquired(); 274 lock_.AssertAcquired();
268 275
269 scoped_refptr<VideoFrame> next_frame = ready_frames_.front(); 276 scoped_refptr<VideoFrame> next_frame = ready_frames_.front();
270 ready_frames_.pop_front(); 277 ready_frames_.pop_front();
271 frames_decoded_++; 278 frames_decoded_++;
272 279
273 last_timestamp_ = next_frame->timestamp(); 280 last_timestamp_ = last_painted_timestamp_ =
274 last_painted_timestamp_ = next_frame->timestamp(); 281 time_converter_cb_.Run(next_frame->timestamp());
xhwang 2015/03/23 22:20:14 hmm, do we allow chained assignment? I don't see i
DaleCurtis 2015/03/25 00:31:45 Hmm, I tend to use it in the audio pathways, but I
275 282
276 paint_cb_.Run(next_frame); 283 paint_cb_.Run(next_frame);
277 284
278 task_runner_->PostTask( 285 task_runner_->PostTask(
279 FROM_HERE, 286 FROM_HERE,
280 base::Bind(&VideoRendererImpl::AttemptRead, weak_factory_.GetWeakPtr())); 287 base::Bind(&VideoRendererImpl::AttemptRead, weak_factory_.GetWeakPtr()));
281 } 288 }
282 289
283 void VideoRendererImpl::DropNextReadyFrame_Locked() { 290 void VideoRendererImpl::DropNextReadyFrame_Locked() {
284 TRACE_EVENT0("media", "VideoRendererImpl:frameDropped"); 291 TRACE_EVENT0("media", "VideoRendererImpl:frameDropped");
285 292
286 lock_.AssertAcquired(); 293 lock_.AssertAcquired();
287 294
288 last_timestamp_ = ready_frames_.front()->timestamp(); 295 last_timestamp_ = time_converter_cb_.Run(ready_frames_.front()->timestamp());
296
289 ready_frames_.pop_front(); 297 ready_frames_.pop_front();
290 frames_decoded_++; 298 frames_decoded_++;
291 frames_dropped_++; 299 frames_dropped_++;
292 300
293 task_runner_->PostTask( 301 task_runner_->PostTask(
294 FROM_HERE, 302 FROM_HERE,
295 base::Bind(&VideoRendererImpl::AttemptRead, weak_factory_.GetWeakPtr())); 303 base::Bind(&VideoRendererImpl::AttemptRead, weak_factory_.GetWeakPtr()));
296 } 304 }
297 305
298 void VideoRendererImpl::FrameReady(VideoFrameStream::Status status, 306 void VideoRendererImpl::FrameReady(VideoFrameStream::Status status,
(...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after
419 void VideoRendererImpl::OnVideoFrameStreamResetDone() { 427 void VideoRendererImpl::OnVideoFrameStreamResetDone() {
420 base::AutoLock auto_lock(lock_); 428 base::AutoLock auto_lock(lock_);
421 DCHECK_EQ(kFlushing, state_); 429 DCHECK_EQ(kFlushing, state_);
422 DCHECK(!pending_read_); 430 DCHECK(!pending_read_);
423 DCHECK(ready_frames_.empty()); 431 DCHECK(ready_frames_.empty());
424 DCHECK(!received_end_of_stream_); 432 DCHECK(!received_end_of_stream_);
425 DCHECK(!rendered_end_of_stream_); 433 DCHECK(!rendered_end_of_stream_);
426 DCHECK_EQ(buffering_state_, BUFFERING_HAVE_NOTHING); 434 DCHECK_EQ(buffering_state_, BUFFERING_HAVE_NOTHING);
427 435
428 state_ = kFlushed; 436 state_ = kFlushed;
429 last_timestamp_ = kNoTimestamp(); 437 last_timestamp_ = last_painted_timestamp_ = base::TimeTicks();
xhwang 2015/03/23 22:20:14 ditto
DaleCurtis 2015/03/25 00:31:45 Acknowledged.
430 last_painted_timestamp_ = kNoTimestamp();
431 base::ResetAndReturn(&flush_cb_).Run(); 438 base::ResetAndReturn(&flush_cb_).Run();
432 } 439 }
433 440
434 void VideoRendererImpl::UpdateStatsAndWait_Locked( 441 void VideoRendererImpl::UpdateStatsAndWait_Locked(
435 base::TimeDelta wait_duration) { 442 base::TimeDelta wait_duration) {
436 lock_.AssertAcquired(); 443 lock_.AssertAcquired();
437 DCHECK_GE(frames_decoded_, 0); 444 DCHECK_GE(frames_decoded_, 0);
438 DCHECK_LE(frames_dropped_, frames_decoded_); 445 DCHECK_LE(frames_dropped_, frames_decoded_);
439 446
440 if (frames_decoded_) { 447 if (frames_decoded_) {
441 PipelineStatistics statistics; 448 PipelineStatistics statistics;
442 statistics.video_frames_decoded = frames_decoded_; 449 statistics.video_frames_decoded = frames_decoded_;
443 statistics.video_frames_dropped = frames_dropped_; 450 statistics.video_frames_dropped = frames_dropped_;
444 task_runner_->PostTask(FROM_HERE, base::Bind(statistics_cb_, statistics)); 451 task_runner_->PostTask(FROM_HERE, base::Bind(statistics_cb_, statistics));
445 452
446 frames_decoded_ = 0; 453 frames_decoded_ = 0;
447 frames_dropped_ = 0; 454 frames_dropped_ = 0;
448 } 455 }
449 456
450 frame_available_.TimedWait(wait_duration); 457 frame_available_.TimedWait(wait_duration);
451 } 458 }
452 459
453 } // namespace media 460 } // namespace media
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698