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

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

Issue 1736012: Merging SeekableBuffer and BufferQueue (Closed)
Patch Set: + 1 TODO Created 10 years, 7 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) 2009 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 #include "media/base/seekable_buffer.h" 5 #include "media/base/seekable_buffer.h"
6 6
7 #include <algorithm> 7 #include <algorithm>
8 8
9 #include "base/logging.h" 9 #include "base/logging.h"
10 #include "base/stl_util-inl.h" 10 #include "media/base/data_buffer.h"
11 11
12 namespace media { 12 namespace media {
13 13
14 SeekableBuffer::SeekableBuffer(size_t backward_capacity, 14 SeekableBuffer::SeekableBuffer(size_t backward_capacity,
15 size_t forward_capacity) 15 size_t forward_capacity)
16 : current_buffer_offset_(0), 16 : current_buffer_offset_(0),
17 backward_capacity_(backward_capacity), 17 backward_capacity_(backward_capacity),
18 backward_bytes_(0), 18 backward_bytes_(0),
19 forward_capacity_(forward_capacity), 19 forward_capacity_(forward_capacity),
20 forward_bytes_(0) { 20 forward_bytes_(0) {
21 current_buffer_ = buffers_.begin(); 21 current_buffer_ = buffers_.begin();
22 } 22 }
23 23
24 SeekableBuffer::~SeekableBuffer() { 24 SeekableBuffer::~SeekableBuffer() {
25 STLDeleteElements(&buffers_);
26 } 25 }
27 26
28 size_t SeekableBuffer::Read(size_t size, uint8* data) { 27 void SeekableBuffer::Clear() {
29 DCHECK(data); 28 buffers_.clear();
30 return InternalRead(size, data); 29 current_buffer_ = buffers_.begin();
30 current_buffer_offset_ = 0;
31 backward_bytes_ = 0;
32 forward_bytes_ = 0;
33 current_time_ = base::TimeDelta();
31 } 34 }
32 35
33 bool SeekableBuffer::Append(size_t size, const uint8* data) { 36
37 size_t SeekableBuffer::Read(uint8* data, size_t size) {
38 DCHECK(data);
39 return InternalRead(data, size, true);
40 }
41
42 size_t SeekableBuffer::Peek(uint8* data, size_t size) {
43 DCHECK(data);
44 return InternalRead(data, size, false);
45 }
46
47 bool SeekableBuffer::Append(Buffer* buffer_in) {
48 if (buffers_.empty() && buffer_in->GetTimestamp().InMicroseconds() > 0) {
49 current_time_ = buffer_in->GetTimestamp();
50 }
51
34 // Since the forward capacity is only used to check the criteria for buffer 52 // Since the forward capacity is only used to check the criteria for buffer
35 // full, we always append data to the buffer. 53 // full, we always append data to the buffer.
36 Buffer* buffer = new Buffer(size); 54 buffers_.push_back(scoped_refptr<Buffer>(buffer_in));
37 memcpy(buffer->data.get(), data, size);
38 buffers_.push_back(buffer);
39 55
40 // After we have written the first buffer, update |current_buffer_| to point 56 // After we have written the first buffer, update |current_buffer_| to point
41 // to it. 57 // to it.
42 if (current_buffer_ == buffers_.end()) { 58 if (current_buffer_ == buffers_.end()) {
43 DCHECK_EQ(0u, forward_bytes_); 59 DCHECK_EQ(0u, forward_bytes_);
44 current_buffer_ = buffers_.begin(); 60 current_buffer_ = buffers_.begin();
45 } 61 }
46 62
47 // Update the |forward_bytes_| counter since we have more bytes. 63 // Update the |forward_bytes_| counter since we have more bytes.
48 forward_bytes_ += size; 64 forward_bytes_ += buffer_in->GetDataSize();
49 65
50 // Advise the user to stop append if the amount of forward bytes exceeds 66 // Advise the user to stop append if the amount of forward bytes exceeds
51 // the forward capacity. A false return value means the user should stop 67 // the forward capacity. A false return value means the user should stop
52 // appending more data to this buffer. 68 // appending more data to this buffer.
53 if (forward_bytes_ >= forward_capacity_) 69 if (forward_bytes_ >= forward_capacity_)
54 return false; 70 return false;
55 return true; 71 return true;
56 } 72 }
57 73
74 bool SeekableBuffer::Append(const uint8* data, size_t size) {
75 DataBuffer* data_buffer = new DataBuffer(size);
76 memcpy(data_buffer->GetWritableData(), data, size);
77 data_buffer->SetDataSize(size);
78 return Append(data_buffer);
79 }
80
58 bool SeekableBuffer::Seek(int32 offset) { 81 bool SeekableBuffer::Seek(int32 offset) {
59 if (offset > 0) 82 if (offset > 0)
60 return SeekForward(offset); 83 return SeekForward(offset);
61 else if (offset < 0) 84 else if (offset < 0)
62 return SeekBackward(-offset); 85 return SeekBackward(-offset);
63 return true; 86 return true;
64 } 87 }
65 88
66 bool SeekableBuffer::SeekForward(size_t size) { 89 bool SeekableBuffer::SeekForward(size_t size) {
67 // Perform seeking forward only if we have enough bytes in the queue. 90 // Perform seeking forward only if we have enough bytes in the queue.
68 if (size > forward_bytes_) 91 if (size > forward_bytes_)
69 return false; 92 return false;
70 93
71 // Do a read of |size| bytes. 94 // Do a read of |size| bytes.
72 size_t taken = InternalRead(size, NULL); 95 size_t taken = InternalRead(NULL, size, true);
73 DCHECK_EQ(taken, size); 96 DCHECK_EQ(taken, size);
74 return true; 97 return true;
75 } 98 }
76 99
77 bool SeekableBuffer::SeekBackward(size_t size) { 100 bool SeekableBuffer::SeekBackward(size_t size) {
78 if (size > backward_bytes_) 101 if (size > backward_bytes_)
79 return false; 102 return false;
80 // Record the number of bytes taken. 103 // Record the number of bytes taken.
81 size_t taken = 0; 104 size_t taken = 0;
82 // Loop until we taken enough bytes and rewind by the desired |size|. 105 // Loop until we taken enough bytes and rewind by the desired |size|.
(...skipping 24 matching lines...) Expand all
107 130
108 // The current buffer pointed by current iterator has been consumed. Move 131 // The current buffer pointed by current iterator has been consumed. Move
109 // the iterator backward so it points to the previous buffer. 132 // the iterator backward so it points to the previous buffer.
110 if (current_buffer_offset_ == 0) { 133 if (current_buffer_offset_ == 0) {
111 if (current_buffer_ == buffers_.begin()) 134 if (current_buffer_ == buffers_.begin())
112 break; 135 break;
113 // Move the iterator backward. 136 // Move the iterator backward.
114 --current_buffer_; 137 --current_buffer_;
115 // Set the offset into the current buffer to be the buffer size as we 138 // Set the offset into the current buffer to be the buffer size as we
116 // are preparing for rewind for next iteration. 139 // are preparing for rewind for next iteration.
117 current_buffer_offset_ = (*current_buffer_)->size; 140 current_buffer_offset_ = (*current_buffer_)->GetDataSize();
118 } 141 }
119 } 142 }
143
144 UpdateCurrentTime(current_buffer_, current_buffer_offset_);
145
120 DCHECK_EQ(taken, size); 146 DCHECK_EQ(taken, size);
121 return true; 147 return true;
122 } 148 }
123 149
124 void SeekableBuffer::EvictBackwardBuffers() { 150 void SeekableBuffer::EvictBackwardBuffers() {
125 // Advances the iterator until we hit the current pointer. 151 // Advances the iterator until we hit the current pointer.
126 while (backward_bytes_ > backward_capacity_) { 152 while (backward_bytes_ > backward_capacity_) {
127 BufferQueue::iterator i = buffers_.begin(); 153 BufferQueue::iterator i = buffers_.begin();
128 if (i == current_buffer_) 154 if (i == current_buffer_)
129 break; 155 break;
130 Buffer* buffer = *i; 156 scoped_refptr<Buffer> buffer = *i;
131 backward_bytes_ -= buffer->size; 157 backward_bytes_ -= buffer->GetDataSize();
132 DCHECK_GE(backward_bytes_, 0u); 158 DCHECK_GE(backward_bytes_, 0u);
133 159
134 delete buffer;
135 buffers_.erase(i); 160 buffers_.erase(i);
136 } 161 }
137 } 162 }
138 163
139 size_t SeekableBuffer::InternalRead(size_t size, uint8* data) { 164 size_t SeekableBuffer::InternalRead(uint8* data, size_t size,
165 bool advance_position) {
140 // Counts how many bytes are actually read from the buffer queue. 166 // Counts how many bytes are actually read from the buffer queue.
141 size_t taken = 0; 167 size_t taken = 0;
142 168
169 BufferQueue::iterator current_buffer = current_buffer_;
170 size_t current_buffer_offset = current_buffer_offset_;
171
143 while (taken < size) { 172 while (taken < size) {
144 // |current_buffer_| is valid since the first time this buffer is appended 173 // |current_buffer| is valid since the first time this buffer is appended
145 // with data. 174 // with data.
146 if (current_buffer_ == buffers_.end()) { 175 if (current_buffer == buffers_.end())
147 DCHECK_EQ(0u, forward_bytes_);
148 break; 176 break;
149 } 177
150 Buffer* buffer = *current_buffer_; 178 scoped_refptr<Buffer> buffer = *current_buffer;
151 179
152 // Find the right amount to copy from the current buffer referenced by 180 // Find the right amount to copy from the current buffer referenced by
153 // |buffer|. We shall copy no more than |size| bytes in total and each 181 // |buffer|. We shall copy no more than |size| bytes in total and each
154 // single step copied no more than the current buffer size. 182 // single step copied no more than the current buffer size.
155 size_t copied = std::min(size - taken, 183 size_t copied = std::min(size - taken,
156 buffer->size - current_buffer_offset_); 184 buffer->GetDataSize() - current_buffer_offset);
157 185
158 // |data| is NULL if we are seeking forward, so there's no need to copy. 186 // |data| is NULL if we are seeking forward, so there's no need to copy.
159 if (data) 187 if (data)
160 memcpy(data + taken, buffer->data.get() + current_buffer_offset_, copied); 188 memcpy(data + taken, buffer->GetData() + current_buffer_offset, copied);
161 189
162 // Increase total number of bytes copied, which regulates when to end this 190 // Increase total number of bytes copied, which regulates when to end this
163 // loop. 191 // loop.
164 taken += copied; 192 taken += copied;
165 193
166 // We have read |copied| bytes from the current buffer. Advances the offset. 194 // We have read |copied| bytes from the current buffer. Advances the offset.
167 current_buffer_offset_ += copied; 195 current_buffer_offset += copied;
168
169 // We have less forward bytes and more backward bytes. Updates these
170 // counters by |copied|.
171 forward_bytes_ -= copied;
172 backward_bytes_ += copied;
173 DCHECK_GE(forward_bytes_, 0u);
174 196
175 // The buffer has been consumed. 197 // The buffer has been consumed.
176 if (current_buffer_offset_ == buffer->size) { 198 if (current_buffer_offset == buffer->GetDataSize()) {
177 BufferQueue::iterator next = current_buffer_; 199 if (advance_position) {
200 // Next buffer may not have timestamp, so we need to update current
201 // timestamp before switching to the next buffer.
202 UpdateCurrentTime(current_buffer, current_buffer_offset);
203 }
204
205 BufferQueue::iterator next = current_buffer;
178 ++next; 206 ++next;
179 // If we are at the last buffer, don't advance. 207 // If we are at the last buffer, don't advance.
180 if (next == buffers_.end()) 208 if (next == buffers_.end())
181 break; 209 break;
182 210
183 // Advances the iterator. 211 // Advances the iterator.
184 current_buffer_ = next; 212 current_buffer = next;
185 current_buffer_offset_ = 0; 213 current_buffer_offset = 0;
186 } 214 }
187 } 215 }
188 EvictBackwardBuffers(); 216
217 if (advance_position) {
218 // We have less forward bytes and more backward bytes. Updates these
219 // counters by |taken|.
220 forward_bytes_ -= taken;
221 backward_bytes_ += taken;
222 DCHECK_GE(forward_bytes_, 0u);
223 DCHECK(current_buffer_ != buffers_.end() || forward_bytes_ == 0u);
224
225 current_buffer_ = current_buffer;
226 current_buffer_offset_ = current_buffer_offset;
227
228 UpdateCurrentTime(current_buffer_, current_buffer_offset_);
229 EvictBackwardBuffers();
230 }
231
189 return taken; 232 return taken;
190 } 233 }
191 234
235 void SeekableBuffer::UpdateCurrentTime(BufferQueue::iterator buffer,
236 size_t offset) {
237 // Garbage values are unavoidable, so this check will remain.
238 if (buffer != buffers_.end() &&
239 (*buffer)->GetTimestamp().InMicroseconds() > 0) {
240 int64 time_offset = ((*buffer)->GetDuration().InMicroseconds() *
241 offset) / (*buffer)->GetDataSize();
242
243 current_time_ = (*buffer)->GetTimestamp() +
244 base::TimeDelta::FromMicroseconds(time_offset);
245 }
246 }
247
192 } // namespace media 248 } // namespace media
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698