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

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

Issue 164233005: Cap the memory usage in FFMpegDemuxer. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Use size_t instead of int. Created 6 years, 10 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 (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/decoder_buffer_queue.h" 5 #include "media/base/decoder_buffer_queue.h"
6 6
7 #include "base/logging.h" 7 #include "base/logging.h"
8 #include "base/numerics/safe_conversions.h"
8 #include "media/base/buffers.h" 9 #include "media/base/buffers.h"
9 #include "media/base/decoder_buffer.h" 10 #include "media/base/decoder_buffer.h"
10 11
11 namespace media { 12 namespace media {
12 13
13 DecoderBufferQueue::DecoderBufferQueue() 14 DecoderBufferQueue::DecoderBufferQueue()
14 : earliest_valid_timestamp_(kNoTimestamp()) { 15 : earliest_valid_timestamp_(kNoTimestamp()),
16 data_size_(0) {
15 } 17 }
16 18
17 DecoderBufferQueue::~DecoderBufferQueue() {} 19 DecoderBufferQueue::~DecoderBufferQueue() {}
18 20
19 void DecoderBufferQueue::Push(const scoped_refptr<DecoderBuffer>& buffer) { 21 void DecoderBufferQueue::Push(const scoped_refptr<DecoderBuffer>& buffer) {
20 CHECK(!buffer->end_of_stream()); 22 CHECK(!buffer->end_of_stream());
21 23
22 queue_.push_back(buffer); 24 queue_.push_back(buffer);
23 25
26 // TODO(damienv): Remove the cast here and in every place in this file
27 // when DecoderBuffer::data_size is modified to return a size_t.
28 data_size_ += base::checked_cast<size_t, int>(buffer->data_size());
29
24 // TODO(scherkus): FFmpeg returns some packets with no timestamp after 30 // TODO(scherkus): FFmpeg returns some packets with no timestamp after
25 // seeking. Fix and turn this into CHECK(). See http://crbug.com/162192 31 // seeking. Fix and turn this into CHECK(). See http://crbug.com/162192
26 if (buffer->timestamp() == kNoTimestamp()) { 32 if (buffer->timestamp() == kNoTimestamp()) {
27 DVLOG(1) << "Buffer has no timestamp"; 33 DVLOG(1) << "Buffer has no timestamp";
28 return; 34 return;
29 } 35 }
30 36
31 if (earliest_valid_timestamp_ == kNoTimestamp()) { 37 if (earliest_valid_timestamp_ == kNoTimestamp()) {
32 earliest_valid_timestamp_ = buffer->timestamp(); 38 earliest_valid_timestamp_ = buffer->timestamp();
33 } 39 }
34 40
35 if (buffer->timestamp() < earliest_valid_timestamp_) { 41 if (buffer->timestamp() < earliest_valid_timestamp_) {
36 DVLOG(1) 42 DVLOG(1)
37 << "Out of order timestamps: " 43 << "Out of order timestamps: "
38 << buffer->timestamp().InMicroseconds() 44 << buffer->timestamp().InMicroseconds()
39 << " vs. " 45 << " vs. "
40 << earliest_valid_timestamp_.InMicroseconds(); 46 << earliest_valid_timestamp_.InMicroseconds();
41 return; 47 return;
42 } 48 }
43 49
44 earliest_valid_timestamp_ = buffer->timestamp(); 50 earliest_valid_timestamp_ = buffer->timestamp();
45 in_order_queue_.push_back(buffer); 51 in_order_queue_.push_back(buffer);
46 } 52 }
47 53
48 scoped_refptr<DecoderBuffer> DecoderBufferQueue::Pop() { 54 scoped_refptr<DecoderBuffer> DecoderBufferQueue::Pop() {
49 scoped_refptr<DecoderBuffer> buffer = queue_.front(); 55 scoped_refptr<DecoderBuffer> buffer = queue_.front();
50 queue_.pop_front(); 56 queue_.pop_front();
51 57
58 size_t buffer_data_size =
59 base::checked_cast<size_t, int>(buffer->data_size());
60 DCHECK_LE(buffer_data_size, data_size_);
61 data_size_ -= buffer_data_size;
62
52 if (!in_order_queue_.empty() && 63 if (!in_order_queue_.empty() &&
53 in_order_queue_.front().get() == buffer.get()) { 64 in_order_queue_.front().get() == buffer.get()) {
54 in_order_queue_.pop_front(); 65 in_order_queue_.pop_front();
55 } 66 }
56 67
57 return buffer; 68 return buffer;
58 } 69 }
59 70
60 void DecoderBufferQueue::Clear() { 71 void DecoderBufferQueue::Clear() {
61 queue_.clear(); 72 queue_.clear();
73 data_size_ = 0;
62 in_order_queue_.clear(); 74 in_order_queue_.clear();
63 earliest_valid_timestamp_ = kNoTimestamp(); 75 earliest_valid_timestamp_ = kNoTimestamp();
64 } 76 }
65 77
66 bool DecoderBufferQueue::IsEmpty() { 78 bool DecoderBufferQueue::IsEmpty() {
67 return queue_.empty(); 79 return queue_.empty();
68 } 80 }
69 81
70 base::TimeDelta DecoderBufferQueue::Duration() { 82 base::TimeDelta DecoderBufferQueue::Duration() {
71 if (in_order_queue_.size() < 2) 83 if (in_order_queue_.size() < 2)
72 return base::TimeDelta(); 84 return base::TimeDelta();
73 85
74 base::TimeDelta start = in_order_queue_.front()->timestamp(); 86 base::TimeDelta start = in_order_queue_.front()->timestamp();
75 base::TimeDelta end = in_order_queue_.back()->timestamp(); 87 base::TimeDelta end = in_order_queue_.back()->timestamp();
76 return end - start; 88 return end - start;
77 } 89 }
78 90
79 } // namespace media 91 } // namespace media
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698