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

Side by Side Diff: media/base/pipeline_impl.cc

Issue 2782004: Added checks to GetBufferedTime() to cap at the media's duration (Closed)
Patch Set: Fixed nits from code review Created 10 years, 6 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/base/pipeline_impl.h ('k') | media/base/pipeline_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 (c) 2010 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2010 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 // TODO(scherkus): clean up PipelineImpl... too many crazy function names, 5 // TODO(scherkus): clean up PipelineImpl... too many crazy function names,
6 // potential deadlocks, etc... 6 // potential deadlocks, etc...
7 7
8 #include "base/callback.h" 8 #include "base/callback.h"
9 #include "base/compiler_specific.h" 9 #include "base/compiler_specific.h"
10 #include "base/condition_variable.h" 10 #include "base/condition_variable.h"
(...skipping 203 matching lines...) Expand 10 before | Expand all | Expand 10 after
214 // is set/get under the lock, because this is breaching the contract that 214 // is set/get under the lock, because this is breaching the contract that
215 // |state_| is only accessed on |message_loop_|. 215 // |state_| is only accessed on |message_loop_|.
216 AutoLock auto_lock(lock_); 216 AutoLock auto_lock(lock_);
217 base::TimeDelta elapsed = clock_.Elapsed(); 217 base::TimeDelta elapsed = clock_.Elapsed();
218 if (state_ == kEnded || elapsed > duration_) { 218 if (state_ == kEnded || elapsed > duration_) {
219 return duration_; 219 return duration_;
220 } 220 }
221 return elapsed; 221 return elapsed;
222 } 222 }
223 223
224 base::TimeDelta PipelineImpl::GetBufferedTime() {
225 AutoLock auto_lock(lock_);
224 226
225 base::TimeDelta PipelineImpl::GetBufferedTime() { 227 // If media is fully loaded, then return duration.
226 DCHECK(buffered_bytes_ >= current_bytes_); 228 if (loaded_)
227 AutoLock auto_lock(lock_); 229 return duration_;
228 230
229 // If buffered time was set, we report that value directly. 231 // If buffered time was set, we report that value directly.
230 if (buffered_time_.ToInternalValue() > 0) 232 if (buffered_time_.ToInternalValue() > 0)
231 return buffered_time_; 233 return buffered_time_;
232 234
233 if (total_bytes_ == 0 || current_bytes_ == 0) 235 if (total_bytes_ == 0 || current_bytes_ == 0)
234 return base::TimeDelta(); 236 return base::TimeDelta();
235 237
236 // If buffered time was not set, we use current time, current bytes, and 238 // If buffered time was not set, we use current time, current bytes, and
237 // buffered bytes to estimate the buffered time. 239 // buffered bytes to estimate the buffered time.
238 double current_time = static_cast<double>(current_bytes_) / total_bytes_ * 240 double current_time = static_cast<double>(current_bytes_) / total_bytes_ *
239 duration_.InMilliseconds(); 241 duration_.InMilliseconds();
240 double rate = current_time / current_bytes_; 242 double rate = current_time / current_bytes_;
241 double buffered_time = rate * (buffered_bytes_ - current_bytes_) + 243 DCHECK_GE(buffered_bytes_, current_bytes_);
242 current_time; 244 base::TimeDelta buffered_time = base::TimeDelta::FromMilliseconds(
245 static_cast<int64>(rate * (buffered_bytes_ - current_bytes_) +
246 current_time));
247
248 // Cap approximated buffered time at the length of the video.
249 buffered_time = std::min(buffered_time, duration_);
243 250
244 // Only print the max buffered time for smooth buffering. 251 // Only print the max buffered time for smooth buffering.
245 if (buffered_time > max_buffered_time_) 252 max_buffered_time_ = std::max(buffered_time, max_buffered_time_);
246 max_buffered_time_ = buffered_time;
247 253
248 return base::TimeDelta::FromMilliseconds( 254 return max_buffered_time_;
249 static_cast<int64>(max_buffered_time_));
250 } 255 }
251 256
252 base::TimeDelta PipelineImpl::GetMediaDuration() const { 257 base::TimeDelta PipelineImpl::GetMediaDuration() const {
253 AutoLock auto_lock(lock_); 258 AutoLock auto_lock(lock_);
254 return duration_; 259 return duration_;
255 } 260 }
256 261
257 int64 PipelineImpl::GetBufferedBytes() const { 262 int64 PipelineImpl::GetBufferedBytes() const {
258 AutoLock auto_lock(lock_); 263 AutoLock auto_lock(lock_);
259 return buffered_bytes_; 264 return buffered_bytes_;
(...skipping 760 matching lines...) Expand 10 before | Expand all | Expand 10 after
1020 filters_.front()->Stop(NewCallback( 1025 filters_.front()->Stop(NewCallback(
1021 this, &PipelineImpl::OnFilterStateTransition)); 1026 this, &PipelineImpl::OnFilterStateTransition));
1022 } else { 1027 } else {
1023 state_ = kStopped; 1028 state_ = kStopped;
1024 message_loop_->PostTask(FROM_HERE, 1029 message_loop_->PostTask(FROM_HERE,
1025 NewRunnableMethod(this, &PipelineImpl::FinishDestroyingFiltersTask)); 1030 NewRunnableMethod(this, &PipelineImpl::FinishDestroyingFiltersTask));
1026 } 1031 }
1027 } 1032 }
1028 1033
1029 } // namespace media 1034 } // namespace media
OLDNEW
« no previous file with comments | « media/base/pipeline_impl.h ('k') | media/base/pipeline_impl_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698