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 #include "media/filters/video_renderer_impl.h" | 5 #include "media/filters/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/debug/trace_event.h" | 10 #include "base/debug/trace_event.h" |
(...skipping 12 matching lines...) Expand all Loading... |
23 } | 23 } |
24 | 24 |
25 VideoRendererImpl::VideoRendererImpl( | 25 VideoRendererImpl::VideoRendererImpl( |
26 const scoped_refptr<base::SingleThreadTaskRunner>& task_runner, | 26 const scoped_refptr<base::SingleThreadTaskRunner>& task_runner, |
27 ScopedVector<VideoDecoder> decoders, | 27 ScopedVector<VideoDecoder> decoders, |
28 const SetDecryptorReadyCB& set_decryptor_ready_cb, | 28 const SetDecryptorReadyCB& set_decryptor_ready_cb, |
29 const PaintCB& paint_cb, | 29 const PaintCB& paint_cb, |
30 const SetOpaqueCB& set_opaque_cb, | 30 const SetOpaqueCB& set_opaque_cb, |
31 bool drop_frames) | 31 bool drop_frames) |
32 : task_runner_(task_runner), | 32 : task_runner_(task_runner), |
33 weak_factory_(this), | 33 video_frame_stream_(task_runner, decoders.Pass(), set_decryptor_ready_cb), |
34 video_frame_stream_( | |
35 task_runner, decoders.Pass(), set_decryptor_ready_cb), | |
36 received_end_of_stream_(false), | 34 received_end_of_stream_(false), |
37 frame_available_(&lock_), | 35 frame_available_(&lock_), |
38 state_(kUninitialized), | 36 state_(kUninitialized), |
39 thread_(), | 37 thread_(), |
40 pending_read_(false), | 38 pending_read_(false), |
41 drop_frames_(drop_frames), | 39 drop_frames_(drop_frames), |
42 playback_rate_(0), | 40 playback_rate_(0), |
43 paint_cb_(paint_cb), | 41 paint_cb_(paint_cb), |
44 set_opaque_cb_(set_opaque_cb), | 42 set_opaque_cb_(set_opaque_cb), |
45 last_timestamp_(kNoTimestamp()), | 43 last_timestamp_(kNoTimestamp()), |
46 frames_decoded_(0), | 44 frames_decoded_(0), |
47 frames_dropped_(0) { | 45 frames_dropped_(0), |
| 46 weak_factory_(this) { |
48 DCHECK(!paint_cb_.is_null()); | 47 DCHECK(!paint_cb_.is_null()); |
49 } | 48 } |
50 | 49 |
51 VideoRendererImpl::~VideoRendererImpl() { | 50 VideoRendererImpl::~VideoRendererImpl() { |
52 base::AutoLock auto_lock(lock_); | 51 base::AutoLock auto_lock(lock_); |
53 CHECK(state_ == kStopped || state_ == kUninitialized) << state_; | 52 CHECK(state_ == kStopped || state_ == kUninitialized) << state_; |
54 CHECK(thread_.is_null()); | 53 CHECK(thread_.is_null()); |
55 } | 54 } |
56 | 55 |
57 void VideoRendererImpl::Play(const base::Closure& callback) { | 56 void VideoRendererImpl::Play(const base::Closure& callback) { |
(...skipping 16 matching lines...) Expand all Loading... |
74 DCHECK(task_runner_->BelongsToCurrentThread()); | 73 DCHECK(task_runner_->BelongsToCurrentThread()); |
75 base::AutoLock auto_lock(lock_); | 74 base::AutoLock auto_lock(lock_); |
76 DCHECK_EQ(state_, kPaused); | 75 DCHECK_EQ(state_, kPaused); |
77 flush_cb_ = callback; | 76 flush_cb_ = callback; |
78 state_ = kFlushing; | 77 state_ = kFlushing; |
79 | 78 |
80 // This is necessary if the |video_frame_stream_| has already seen an end of | 79 // This is necessary if the |video_frame_stream_| has already seen an end of |
81 // stream and needs to drain it before flushing it. | 80 // stream and needs to drain it before flushing it. |
82 ready_frames_.clear(); | 81 ready_frames_.clear(); |
83 received_end_of_stream_ = false; | 82 received_end_of_stream_ = false; |
84 video_frame_stream_.Reset(base::Bind( | 83 video_frame_stream_.Reset( |
85 &VideoRendererImpl::OnVideoFrameStreamResetDone, weak_this_)); | 84 base::Bind(&VideoRendererImpl::OnVideoFrameStreamResetDone, |
| 85 weak_factory_.GetWeakPtr())); |
86 } | 86 } |
87 | 87 |
88 void VideoRendererImpl::Stop(const base::Closure& callback) { | 88 void VideoRendererImpl::Stop(const base::Closure& callback) { |
89 DCHECK(task_runner_->BelongsToCurrentThread()); | 89 DCHECK(task_runner_->BelongsToCurrentThread()); |
90 base::AutoLock auto_lock(lock_); | 90 base::AutoLock auto_lock(lock_); |
91 if (state_ == kUninitialized || state_ == kStopped) { | 91 if (state_ == kUninitialized || state_ == kStopped) { |
92 callback.Run(); | 92 callback.Run(); |
93 return; | 93 return; |
94 } | 94 } |
95 | 95 |
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
166 DCHECK(stream); | 166 DCHECK(stream); |
167 DCHECK_EQ(stream->type(), DemuxerStream::VIDEO); | 167 DCHECK_EQ(stream->type(), DemuxerStream::VIDEO); |
168 DCHECK(!init_cb.is_null()); | 168 DCHECK(!init_cb.is_null()); |
169 DCHECK(!statistics_cb.is_null()); | 169 DCHECK(!statistics_cb.is_null()); |
170 DCHECK(!max_time_cb.is_null()); | 170 DCHECK(!max_time_cb.is_null()); |
171 DCHECK(!ended_cb.is_null()); | 171 DCHECK(!ended_cb.is_null()); |
172 DCHECK(!get_time_cb.is_null()); | 172 DCHECK(!get_time_cb.is_null()); |
173 DCHECK(!get_duration_cb.is_null()); | 173 DCHECK(!get_duration_cb.is_null()); |
174 DCHECK_EQ(kUninitialized, state_); | 174 DCHECK_EQ(kUninitialized, state_); |
175 | 175 |
176 weak_this_ = weak_factory_.GetWeakPtr(); | |
177 init_cb_ = init_cb; | 176 init_cb_ = init_cb; |
178 statistics_cb_ = statistics_cb; | 177 statistics_cb_ = statistics_cb; |
179 max_time_cb_ = max_time_cb; | 178 max_time_cb_ = max_time_cb; |
180 ended_cb_ = ended_cb; | 179 ended_cb_ = ended_cb; |
181 error_cb_ = error_cb; | 180 error_cb_ = error_cb; |
182 get_time_cb_ = get_time_cb; | 181 get_time_cb_ = get_time_cb; |
183 get_duration_cb_ = get_duration_cb; | 182 get_duration_cb_ = get_duration_cb; |
184 state_ = kInitializing; | 183 state_ = kInitializing; |
185 | 184 |
186 video_frame_stream_.Initialize( | 185 video_frame_stream_.Initialize( |
187 stream, | 186 stream, |
188 statistics_cb, | 187 statistics_cb, |
189 base::Bind(&VideoRendererImpl::OnVideoFrameStreamInitialized, | 188 base::Bind(&VideoRendererImpl::OnVideoFrameStreamInitialized, |
190 weak_this_)); | 189 weak_factory_.GetWeakPtr())); |
191 } | 190 } |
192 | 191 |
193 void VideoRendererImpl::OnVideoFrameStreamInitialized(bool success, | 192 void VideoRendererImpl::OnVideoFrameStreamInitialized(bool success, |
194 bool has_alpha) { | 193 bool has_alpha) { |
195 DCHECK(task_runner_->BelongsToCurrentThread()); | 194 DCHECK(task_runner_->BelongsToCurrentThread()); |
196 base::AutoLock auto_lock(lock_); | 195 base::AutoLock auto_lock(lock_); |
197 | 196 |
198 if (state_ == kStopped) | 197 if (state_ == kStopped) |
199 return; | 198 return; |
200 | 199 |
(...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
316 lock_.AssertAcquired(); | 315 lock_.AssertAcquired(); |
317 | 316 |
318 scoped_refptr<VideoFrame> next_frame = ready_frames_.front(); | 317 scoped_refptr<VideoFrame> next_frame = ready_frames_.front(); |
319 ready_frames_.pop_front(); | 318 ready_frames_.pop_front(); |
320 frames_decoded_++; | 319 frames_decoded_++; |
321 | 320 |
322 last_timestamp_ = next_frame->GetTimestamp(); | 321 last_timestamp_ = next_frame->GetTimestamp(); |
323 | 322 |
324 paint_cb_.Run(next_frame); | 323 paint_cb_.Run(next_frame); |
325 | 324 |
326 task_runner_->PostTask(FROM_HERE, base::Bind( | 325 task_runner_->PostTask( |
327 &VideoRendererImpl::AttemptRead, weak_this_)); | 326 FROM_HERE, |
| 327 base::Bind(&VideoRendererImpl::AttemptRead, weak_factory_.GetWeakPtr())); |
328 } | 328 } |
329 | 329 |
330 void VideoRendererImpl::DropNextReadyFrame_Locked() { | 330 void VideoRendererImpl::DropNextReadyFrame_Locked() { |
331 TRACE_EVENT0("media", "VideoRendererImpl:frameDropped"); | 331 TRACE_EVENT0("media", "VideoRendererImpl:frameDropped"); |
332 | 332 |
333 lock_.AssertAcquired(); | 333 lock_.AssertAcquired(); |
334 | 334 |
335 last_timestamp_ = ready_frames_.front()->GetTimestamp(); | 335 last_timestamp_ = ready_frames_.front()->GetTimestamp(); |
336 ready_frames_.pop_front(); | 336 ready_frames_.pop_front(); |
337 frames_decoded_++; | 337 frames_decoded_++; |
338 frames_dropped_++; | 338 frames_dropped_++; |
339 | 339 |
340 task_runner_->PostTask(FROM_HERE, base::Bind( | 340 task_runner_->PostTask( |
341 &VideoRendererImpl::AttemptRead, weak_this_)); | 341 FROM_HERE, |
| 342 base::Bind(&VideoRendererImpl::AttemptRead, weak_factory_.GetWeakPtr())); |
342 } | 343 } |
343 | 344 |
344 void VideoRendererImpl::FrameReady(VideoFrameStream::Status status, | 345 void VideoRendererImpl::FrameReady(VideoFrameStream::Status status, |
345 const scoped_refptr<VideoFrame>& frame) { | 346 const scoped_refptr<VideoFrame>& frame) { |
346 base::AutoLock auto_lock(lock_); | 347 base::AutoLock auto_lock(lock_); |
347 DCHECK_NE(state_, kUninitialized); | 348 DCHECK_NE(state_, kUninitialized); |
348 DCHECK_NE(state_, kFlushed); | 349 DCHECK_NE(state_, kFlushed); |
349 | 350 |
350 CHECK(pending_read_); | 351 CHECK(pending_read_); |
351 pending_read_ = false; | 352 pending_read_ = false; |
(...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
456 ready_frames_.size() == static_cast<size_t>(limits::kMaxVideoFrames)) { | 457 ready_frames_.size() == static_cast<size_t>(limits::kMaxVideoFrames)) { |
457 return; | 458 return; |
458 } | 459 } |
459 | 460 |
460 switch (state_) { | 461 switch (state_) { |
461 case kPaused: | 462 case kPaused: |
462 case kPrerolling: | 463 case kPrerolling: |
463 case kPlaying: | 464 case kPlaying: |
464 pending_read_ = true; | 465 pending_read_ = true; |
465 video_frame_stream_.Read(base::Bind(&VideoRendererImpl::FrameReady, | 466 video_frame_stream_.Read(base::Bind(&VideoRendererImpl::FrameReady, |
466 weak_this_)); | 467 weak_factory_.GetWeakPtr())); |
467 return; | 468 return; |
468 | 469 |
469 case kUninitialized: | 470 case kUninitialized: |
470 case kInitializing: | 471 case kInitializing: |
471 case kPrerolled: | 472 case kPrerolled: |
472 case kFlushing: | 473 case kFlushing: |
473 case kFlushed: | 474 case kFlushed: |
474 case kEnded: | 475 case kEnded: |
475 case kStopped: | 476 case kStopped: |
476 case kError: | 477 case kError: |
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
540 statistics_cb_.Run(statistics); | 541 statistics_cb_.Run(statistics); |
541 | 542 |
542 frames_decoded_ = 0; | 543 frames_decoded_ = 0; |
543 frames_dropped_ = 0; | 544 frames_dropped_ = 0; |
544 } | 545 } |
545 | 546 |
546 frame_available_.TimedWait(wait_duration); | 547 frame_available_.TimedWait(wait_duration); |
547 } | 548 } |
548 | 549 |
549 } // namespace media | 550 } // namespace media |
OLD | NEW |