OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/base/pipeline.h" | 5 #include "media/base/pipeline.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/callback.h" | 10 #include "base/callback.h" |
11 #include "base/callback_helpers.h" | 11 #include "base/callback_helpers.h" |
12 #include "base/compiler_specific.h" | 12 #include "base/compiler_specific.h" |
13 #include "base/metrics/histogram.h" | 13 #include "base/metrics/histogram.h" |
14 #include "base/message_loop.h" | 14 #include "base/message_loop.h" |
15 #include "base/stl_util.h" | 15 #include "base/stl_util.h" |
16 #include "base/string_util.h" | 16 #include "base/string_util.h" |
17 #include "base/synchronization/condition_variable.h" | 17 #include "base/synchronization/condition_variable.h" |
18 #include "media/base/audio_decoder.h" | 18 #include "media/base/audio_decoder.h" |
19 #include "media/base/audio_renderer.h" | 19 #include "media/base/audio_renderer.h" |
| 20 #include "media/base/buffers.h" |
20 #include "media/base/clock.h" | 21 #include "media/base/clock.h" |
21 #include "media/base/filter_collection.h" | 22 #include "media/base/filter_collection.h" |
22 #include "media/base/media_log.h" | 23 #include "media/base/media_log.h" |
23 #include "media/base/video_decoder.h" | 24 #include "media/base/video_decoder.h" |
24 #include "media/base/video_renderer.h" | 25 #include "media/base/video_renderer.h" |
25 | 26 |
26 using base::TimeDelta; | 27 using base::TimeDelta; |
27 | 28 |
28 namespace media { | 29 namespace media { |
29 | 30 |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
61 } | 62 } |
62 | 63 |
63 struct Pipeline::PipelineInitState { | 64 struct Pipeline::PipelineInitState { |
64 scoped_refptr<AudioDecoder> audio_decoder; | 65 scoped_refptr<AudioDecoder> audio_decoder; |
65 scoped_refptr<VideoDecoder> video_decoder; | 66 scoped_refptr<VideoDecoder> video_decoder; |
66 }; | 67 }; |
67 | 68 |
68 Pipeline::Pipeline(MessageLoop* message_loop, MediaLog* media_log) | 69 Pipeline::Pipeline(MessageLoop* message_loop, MediaLog* media_log) |
69 : message_loop_(message_loop->message_loop_proxy()), | 70 : message_loop_(message_loop->message_loop_proxy()), |
70 media_log_(media_log), | 71 media_log_(media_log), |
| 72 running_(false), |
| 73 seek_pending_(false), |
| 74 stop_pending_(false), |
| 75 tearing_down_(false), |
| 76 error_caused_teardown_(false), |
| 77 playback_rate_change_pending_(false), |
| 78 did_loading_progress_(false), |
| 79 total_bytes_(0), |
| 80 natural_size_(0, 0), |
| 81 volume_(1.0f), |
| 82 playback_rate_(0.0f), |
| 83 pending_playback_rate_(0.0f), |
71 clock_(new Clock(&base::Time::Now)), | 84 clock_(new Clock(&base::Time::Now)), |
72 waiting_for_clock_update_(false), | 85 waiting_for_clock_update_(false), |
| 86 status_(PIPELINE_OK), |
| 87 has_audio_(false), |
| 88 has_video_(false), |
73 state_(kCreated), | 89 state_(kCreated), |
| 90 seek_timestamp_(kNoTimestamp()), |
| 91 audio_disabled_(false), |
74 creation_time_(base::Time::Now()) { | 92 creation_time_(base::Time::Now()) { |
75 media_log_->AddEvent(media_log_->CreatePipelineStateChangedEvent(kCreated)); | 93 media_log_->AddEvent(media_log_->CreatePipelineStateChangedEvent(kCreated)); |
76 ResetState(); | |
77 media_log_->AddEvent( | 94 media_log_->AddEvent( |
78 media_log_->CreateEvent(MediaLogEvent::PIPELINE_CREATED)); | 95 media_log_->CreateEvent(MediaLogEvent::PIPELINE_CREATED)); |
79 } | 96 } |
80 | 97 |
81 Pipeline::~Pipeline() { | 98 Pipeline::~Pipeline() { |
82 base::AutoLock auto_lock(lock_); | 99 base::AutoLock auto_lock(lock_); |
83 DCHECK(!running_) << "Stop() must complete before destroying object"; | 100 DCHECK(!running_) << "Stop() must complete before destroying object"; |
84 DCHECK(!stop_pending_); | 101 DCHECK(!stop_pending_); |
85 DCHECK(!seek_pending_); | 102 DCHECK(!seek_pending_); |
86 | 103 |
(...skipping 148 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
235 | 252 |
236 PipelineStatistics Pipeline::GetStatistics() const { | 253 PipelineStatistics Pipeline::GetStatistics() const { |
237 base::AutoLock auto_lock(lock_); | 254 base::AutoLock auto_lock(lock_); |
238 return statistics_; | 255 return statistics_; |
239 } | 256 } |
240 | 257 |
241 void Pipeline::SetClockForTesting(Clock* clock) { | 258 void Pipeline::SetClockForTesting(Clock* clock) { |
242 clock_.reset(clock); | 259 clock_.reset(clock); |
243 } | 260 } |
244 | 261 |
245 void Pipeline::ResetState() { | |
246 base::AutoLock auto_lock(lock_); | |
247 const TimeDelta kZero; | |
248 running_ = false; | |
249 stop_pending_ = false; | |
250 seek_pending_ = false; | |
251 tearing_down_ = false; | |
252 error_caused_teardown_ = false; | |
253 playback_rate_change_pending_ = false; | |
254 buffered_byte_ranges_.clear(); | |
255 did_loading_progress_ = false; | |
256 total_bytes_ = 0; | |
257 natural_size_.SetSize(0, 0); | |
258 volume_ = 1.0f; | |
259 playback_rate_ = 0.0f; | |
260 pending_playback_rate_ = 0.0f; | |
261 status_ = PIPELINE_OK; | |
262 has_audio_ = false; | |
263 has_video_ = false; | |
264 waiting_for_clock_update_ = false; | |
265 audio_disabled_ = false; | |
266 clock_->Reset(); | |
267 } | |
268 | |
269 void Pipeline::SetState(State next_state) { | 262 void Pipeline::SetState(State next_state) { |
270 if (state_ != kStarted && next_state == kStarted && | 263 if (state_ != kStarted && next_state == kStarted && |
271 !creation_time_.is_null()) { | 264 !creation_time_.is_null()) { |
272 UMA_HISTOGRAM_TIMES( | 265 UMA_HISTOGRAM_TIMES( |
273 "Media.TimeToPipelineStarted", base::Time::Now() - creation_time_); | 266 "Media.TimeToPipelineStarted", base::Time::Now() - creation_time_); |
274 creation_time_ = base::Time(); | 267 creation_time_ = base::Time(); |
275 } | 268 } |
276 state_ = next_state; | 269 state_ = next_state; |
277 media_log_->AddEvent(media_log_->CreatePipelineStateChangedEvent(next_state)); | 270 media_log_->AddEvent(media_log_->CreatePipelineStateChangedEvent(next_state)); |
278 } | 271 } |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
315 // Prevent double-reporting of errors to clients. | 308 // Prevent double-reporting of errors to clients. |
316 if (status != PIPELINE_OK) | 309 if (status != PIPELINE_OK) |
317 error_cb_.Reset(); | 310 error_cb_.Reset(); |
318 } | 311 } |
319 | 312 |
320 void Pipeline::FinishInitialization() { | 313 void Pipeline::FinishInitialization() { |
321 DCHECK(message_loop_->BelongsToCurrentThread()); | 314 DCHECK(message_loop_->BelongsToCurrentThread()); |
322 // Execute the seek callback, if present. Note that this might be the | 315 // Execute the seek callback, if present. Note that this might be the |
323 // initial callback passed into Start(). | 316 // initial callback passed into Start(). |
324 ReportStatus(seek_cb_, status_); | 317 ReportStatus(seek_cb_, status_); |
| 318 seek_timestamp_ = kNoTimestamp(); |
325 seek_cb_.Reset(); | 319 seek_cb_.Reset(); |
326 } | 320 } |
327 | 321 |
328 // static | 322 // static |
329 bool Pipeline::TransientState(State state) { | 323 bool Pipeline::TransientState(State state) { |
330 return state == kPausing || | 324 return state == kPausing || |
331 state == kFlushing || | 325 state == kFlushing || |
332 state == kSeeking || | 326 state == kSeeking || |
333 state == kStarting || | 327 state == kStarting || |
334 state == kStopping; | 328 state == kStopping; |
(...skipping 572 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
907 NOTREACHED() << "Invalid current state: " << state_; | 901 NOTREACHED() << "Invalid current state: " << state_; |
908 SetError(PIPELINE_ERROR_ABORT); | 902 SetError(PIPELINE_ERROR_ABORT); |
909 return; | 903 return; |
910 } | 904 } |
911 | 905 |
912 // Decrement the number of remaining transitions, making sure to transition | 906 // Decrement the number of remaining transitions, making sure to transition |
913 // to the next state if needed. | 907 // to the next state if needed. |
914 SetState(FindNextState(state_)); | 908 SetState(FindNextState(state_)); |
915 if (state_ == kSeeking) { | 909 if (state_ == kSeeking) { |
916 base::AutoLock auto_lock(lock_); | 910 base::AutoLock auto_lock(lock_); |
| 911 DCHECK(seek_timestamp_ != kNoTimestamp()); |
917 clock_->SetTime(seek_timestamp_, seek_timestamp_); | 912 clock_->SetTime(seek_timestamp_, seek_timestamp_); |
918 } | 913 } |
919 | 914 |
920 // Carry out the action for the current state. | 915 // Carry out the action for the current state. |
921 if (TransientState(state_)) { | 916 if (TransientState(state_)) { |
922 if (state_ == kPausing) { | 917 if (state_ == kPausing) { |
923 DoPause(base::Bind(&Pipeline::OnFilterStateTransition, this)); | 918 DoPause(base::Bind(&Pipeline::OnFilterStateTransition, this)); |
924 } else if (state_ == kFlushing) { | 919 } else if (state_ == kFlushing) { |
925 DoFlush(base::Bind(&Pipeline::OnFilterStateTransition, this)); | 920 DoFlush(base::Bind(&Pipeline::OnFilterStateTransition, this)); |
926 } else if (state_ == kSeeking) { | 921 } else if (state_ == kSeeking) { |
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1009 | 1004 |
1010 audio_renderer_ = NULL; | 1005 audio_renderer_ = NULL; |
1011 video_renderer_ = NULL; | 1006 video_renderer_ = NULL; |
1012 demuxer_ = NULL; | 1007 demuxer_ = NULL; |
1013 | 1008 |
1014 if (error_caused_teardown_ && !IsPipelineOk() && !error_cb_.is_null()) | 1009 if (error_caused_teardown_ && !IsPipelineOk() && !error_cb_.is_null()) |
1015 error_cb_.Run(status_); | 1010 error_cb_.Run(status_); |
1016 | 1011 |
1017 if (stop_pending_) { | 1012 if (stop_pending_) { |
1018 stop_pending_ = false; | 1013 stop_pending_ = false; |
1019 ResetState(); | 1014 { |
| 1015 base::AutoLock l(lock_); |
| 1016 running_ = false; |
| 1017 } |
| 1018 |
1020 // Notify the client that stopping has finished. | 1019 // Notify the client that stopping has finished. |
1021 base::ResetAndReturn(&stop_cb_).Run(); | 1020 base::ResetAndReturn(&stop_cb_).Run(); |
1022 } | 1021 } |
1023 | 1022 |
1024 tearing_down_ = false; | 1023 tearing_down_ = false; |
1025 error_caused_teardown_ = false; | 1024 error_caused_teardown_ = false; |
1026 } | 1025 } |
1027 | 1026 |
1028 void Pipeline::InitializeDemuxer() { | 1027 void Pipeline::InitializeDemuxer() { |
1029 DCHECK(message_loop_->BelongsToCurrentThread()); | 1028 DCHECK(message_loop_->BelongsToCurrentThread()); |
(...skipping 246 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1276 void Pipeline::StartClockIfWaitingForTimeUpdate_Locked() { | 1275 void Pipeline::StartClockIfWaitingForTimeUpdate_Locked() { |
1277 lock_.AssertAcquired(); | 1276 lock_.AssertAcquired(); |
1278 if (!waiting_for_clock_update_) | 1277 if (!waiting_for_clock_update_) |
1279 return; | 1278 return; |
1280 | 1279 |
1281 waiting_for_clock_update_ = false; | 1280 waiting_for_clock_update_ = false; |
1282 clock_->Play(); | 1281 clock_->Play(); |
1283 } | 1282 } |
1284 | 1283 |
1285 } // namespace media | 1284 } // namespace media |
OLD | NEW |