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

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

Issue 17315021: Refactored DataBuffer to use unix_hacker style methods. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Undid overzealous clang-format Created 7 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
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/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 "media/base/data_buffer.h" 10 #include "media/base/data_buffer.h"
11 11
12 namespace media { 12 namespace media {
13 13
14 SeekableBuffer::SeekableBuffer(int backward_capacity, 14 SeekableBuffer::SeekableBuffer(int backward_capacity, int forward_capacity)
15 int forward_capacity)
16 : current_buffer_offset_(0), 15 : current_buffer_offset_(0),
17 backward_capacity_(backward_capacity), 16 backward_capacity_(backward_capacity),
18 backward_bytes_(0), 17 backward_bytes_(0),
19 forward_capacity_(forward_capacity), 18 forward_capacity_(forward_capacity),
20 forward_bytes_(0), 19 forward_bytes_(0),
21 current_time_(kNoTimestamp()) { 20 current_time_(kNoTimestamp()) {
22 current_buffer_ = buffers_.begin(); 21 current_buffer_ = buffers_.begin();
23 } 22 }
24 23
25 SeekableBuffer::~SeekableBuffer() { 24 SeekableBuffer::~SeekableBuffer() {}
brettw 2013/06/24 21:18:29 I like the old way better.
26 }
27 25
28 void SeekableBuffer::Clear() { 26 void SeekableBuffer::Clear() {
29 buffers_.clear(); 27 buffers_.clear();
30 current_buffer_ = buffers_.begin(); 28 current_buffer_ = buffers_.begin();
31 current_buffer_offset_ = 0; 29 current_buffer_offset_ = 0;
32 backward_bytes_ = 0; 30 backward_bytes_ = 0;
33 forward_bytes_ = 0; 31 forward_bytes_ = 0;
34 current_time_ = kNoTimestamp(); 32 current_time_ = kNoTimestamp();
35 } 33 }
36 34
37 int SeekableBuffer::Read(uint8* data, int size) { 35 int SeekableBuffer::Read(uint8* data, int size) {
38 DCHECK(data); 36 DCHECK(data);
39 return InternalRead(data, size, true, 0); 37 return InternalRead(data, size, true, 0);
40 } 38 }
41 39
42 int SeekableBuffer::Peek(uint8* data, int size, int forward_offset) { 40 int SeekableBuffer::Peek(uint8* data, int size, int forward_offset) {
43 DCHECK(data); 41 DCHECK(data);
44 return InternalRead(data, size, false, forward_offset); 42 return InternalRead(data, size, false, forward_offset);
45 } 43 }
46 44
47 bool SeekableBuffer::GetCurrentChunk(const uint8** data, int* size) const { 45 bool SeekableBuffer::GetCurrentChunk(const uint8** data, int* size) const {
48 BufferQueue::iterator current_buffer = current_buffer_; 46 BufferQueue::iterator current_buffer = current_buffer_;
49 int current_buffer_offset = current_buffer_offset_; 47 int current_buffer_offset = current_buffer_offset_;
50 // Advance position if we are in the end of the current buffer. 48 // Advance position if we are in the end of the current buffer.
51 while (current_buffer != buffers_.end() && 49 while (current_buffer != buffers_.end() &&
52 current_buffer_offset >= (*current_buffer)->GetDataSize()) { 50 current_buffer_offset >= (*current_buffer)->data_size()) {
53 ++current_buffer; 51 ++current_buffer;
54 current_buffer_offset = 0; 52 current_buffer_offset = 0;
55 } 53 }
56 if (current_buffer == buffers_.end()) 54 if (current_buffer == buffers_.end())
57 return false; 55 return false;
58 *data = (*current_buffer)->GetData() + current_buffer_offset; 56 *data = (*current_buffer)->data() + current_buffer_offset;
59 *size = (*current_buffer)->GetDataSize() - current_buffer_offset; 57 *size = (*current_buffer)->data_size() - current_buffer_offset;
60 return true; 58 return true;
61 } 59 }
62 60
63 bool SeekableBuffer::Append(const scoped_refptr<DataBuffer>& buffer_in) { 61 bool SeekableBuffer::Append(const scoped_refptr<DataBuffer>& buffer_in) {
64 if (buffers_.empty() && buffer_in->GetTimestamp() != kNoTimestamp()) { 62 if (buffers_.empty() && buffer_in->timestamp() != kNoTimestamp()) {
65 current_time_ = buffer_in->GetTimestamp(); 63 current_time_ = buffer_in->timestamp();
66 } 64 }
67 65
68 // Since the forward capacity is only used to check the criteria for buffer 66 // Since the forward capacity is only used to check the criteria for buffer
69 // full, we always append data to the buffer. 67 // full, we always append data to the buffer.
70 buffers_.push_back(buffer_in); 68 buffers_.push_back(buffer_in);
71 69
72 // After we have written the first buffer, update |current_buffer_| to point 70 // After we have written the first buffer, update |current_buffer_| to point
73 // to it. 71 // to it.
74 if (current_buffer_ == buffers_.end()) { 72 if (current_buffer_ == buffers_.end()) {
75 DCHECK_EQ(0, forward_bytes_); 73 DCHECK_EQ(0, forward_bytes_);
76 current_buffer_ = buffers_.begin(); 74 current_buffer_ = buffers_.begin();
77 } 75 }
78 76
79 // Update the |forward_bytes_| counter since we have more bytes. 77 // Update the |forward_bytes_| counter since we have more bytes.
80 forward_bytes_ += buffer_in->GetDataSize(); 78 forward_bytes_ += buffer_in->data_size();
81 79
82 // Advise the user to stop append if the amount of forward bytes exceeds 80 // Advise the user to stop append if the amount of forward bytes exceeds
83 // the forward capacity. A false return value means the user should stop 81 // the forward capacity. A false return value means the user should stop
84 // appending more data to this buffer. 82 // appending more data to this buffer.
85 if (forward_bytes_ >= forward_capacity_) 83 if (forward_bytes_ >= forward_capacity_)
86 return false; 84 return false;
87 return true; 85 return true;
88 } 86 }
89 87
90 bool SeekableBuffer::Append(const uint8* data, int size) { 88 bool SeekableBuffer::Append(const uint8* data, int size) {
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after
149 147
150 // The current buffer pointed by current iterator has been consumed. Move 148 // The current buffer pointed by current iterator has been consumed. Move
151 // the iterator backward so it points to the previous buffer. 149 // the iterator backward so it points to the previous buffer.
152 if (current_buffer_offset_ == 0) { 150 if (current_buffer_offset_ == 0) {
153 if (current_buffer_ == buffers_.begin()) 151 if (current_buffer_ == buffers_.begin())
154 break; 152 break;
155 // Move the iterator backward. 153 // Move the iterator backward.
156 --current_buffer_; 154 --current_buffer_;
157 // Set the offset into the current buffer to be the buffer size as we 155 // Set the offset into the current buffer to be the buffer size as we
158 // are preparing for rewind for next iteration. 156 // are preparing for rewind for next iteration.
159 current_buffer_offset_ = (*current_buffer_)->GetDataSize(); 157 current_buffer_offset_ = (*current_buffer_)->data_size();
160 } 158 }
161 } 159 }
162 160
163 UpdateCurrentTime(current_buffer_, current_buffer_offset_); 161 UpdateCurrentTime(current_buffer_, current_buffer_offset_);
164 162
165 DCHECK_EQ(taken, size); 163 DCHECK_EQ(taken, size);
166 return true; 164 return true;
167 } 165 }
168 166
169 void SeekableBuffer::EvictBackwardBuffers() { 167 void SeekableBuffer::EvictBackwardBuffers() {
170 // Advances the iterator until we hit the current pointer. 168 // Advances the iterator until we hit the current pointer.
171 while (backward_bytes_ > backward_capacity_) { 169 while (backward_bytes_ > backward_capacity_) {
172 BufferQueue::iterator i = buffers_.begin(); 170 BufferQueue::iterator i = buffers_.begin();
173 if (i == current_buffer_) 171 if (i == current_buffer_)
174 break; 172 break;
175 scoped_refptr<DataBuffer> buffer = *i; 173 scoped_refptr<DataBuffer> buffer = *i;
176 backward_bytes_ -= buffer->GetDataSize(); 174 backward_bytes_ -= buffer->data_size();
177 DCHECK_GE(backward_bytes_, 0); 175 DCHECK_GE(backward_bytes_, 0);
178 176
179 buffers_.erase(i); 177 buffers_.erase(i);
180 } 178 }
181 } 179 }
182 180
183 int SeekableBuffer::InternalRead(uint8* data, int size, 181 int SeekableBuffer::InternalRead(uint8* data, int size, bool advance_position,
184 bool advance_position,
185 int forward_offset) { 182 int forward_offset) {
186 // Counts how many bytes are actually read from the buffer queue. 183 // Counts how many bytes are actually read from the buffer queue.
187 int taken = 0; 184 int taken = 0;
188 185
189 BufferQueue::iterator current_buffer = current_buffer_; 186 BufferQueue::iterator current_buffer = current_buffer_;
190 int current_buffer_offset = current_buffer_offset_; 187 int current_buffer_offset = current_buffer_offset_;
191 188
192 int bytes_to_skip = forward_offset; 189 int bytes_to_skip = forward_offset;
193 while (taken < size) { 190 while (taken < size) {
194 // |current_buffer| is valid since the first time this buffer is appended 191 // |current_buffer| is valid since the first time this buffer is appended
195 // with data. 192 // with data.
196 if (current_buffer == buffers_.end()) 193 if (current_buffer == buffers_.end()) break;
197 break;
198 194
199 scoped_refptr<DataBuffer> buffer = *current_buffer; 195 scoped_refptr<DataBuffer> buffer = *current_buffer;
200 196
201 int remaining_bytes_in_buffer = 197 int remaining_bytes_in_buffer =
202 buffer->GetDataSize() - current_buffer_offset; 198 buffer->data_size() - current_buffer_offset;
203 199
204 if (bytes_to_skip == 0) { 200 if (bytes_to_skip == 0) {
205 // Find the right amount to copy from the current buffer referenced by 201 // Find the right amount to copy from the current buffer referenced by
206 // |buffer|. We shall copy no more than |size| bytes in total and each 202 // |buffer|. We shall copy no more than |size| bytes in total and each
207 // single step copied no more than the current buffer size. 203 // single step copied no more than the current buffer size.
208 int copied = std::min(size - taken, remaining_bytes_in_buffer); 204 int copied = std::min(size - taken, remaining_bytes_in_buffer);
209 205
210 // |data| is NULL if we are seeking forward, so there's no need to copy. 206 // |data| is NULL if we are seeking forward, so there's no need to copy.
211 if (data) 207 if (data)
212 memcpy(data + taken, buffer->GetData() + current_buffer_offset, copied); 208 memcpy(data + taken, buffer->data() + current_buffer_offset, copied);
213 209
214 // Increase total number of bytes copied, which regulates when to end this 210 // Increase total number of bytes copied, which regulates when to end this
215 // loop. 211 // loop.
216 taken += copied; 212 taken += copied;
217 213
218 // We have read |copied| bytes from the current buffer. Advances the 214 // We have read |copied| bytes from the current buffer. Advances the
219 // offset. 215 // offset.
220 current_buffer_offset += copied; 216 current_buffer_offset += copied;
221 } else { 217 } else {
222 int skipped = std::min(remaining_bytes_in_buffer, bytes_to_skip); 218 int skipped = std::min(remaining_bytes_in_buffer, bytes_to_skip);
223 current_buffer_offset += skipped; 219 current_buffer_offset += skipped;
224 bytes_to_skip -= skipped; 220 bytes_to_skip -= skipped;
225 } 221 }
226 222
227 // The buffer has been consumed. 223 // The buffer has been consumed.
228 if (current_buffer_offset == buffer->GetDataSize()) { 224 if (current_buffer_offset == buffer->data_size()) {
229 if (advance_position) { 225 if (advance_position) {
230 // Next buffer may not have timestamp, so we need to update current 226 // Next buffer may not have timestamp, so we need to update current
231 // timestamp before switching to the next buffer. 227 // timestamp before switching to the next buffer.
232 UpdateCurrentTime(current_buffer, current_buffer_offset); 228 UpdateCurrentTime(current_buffer, current_buffer_offset);
233 } 229 }
234 230
235 BufferQueue::iterator next = current_buffer; 231 BufferQueue::iterator next = current_buffer;
236 ++next; 232 ++next;
237 // If we are at the last buffer, don't advance. 233 // If we are at the last buffer, don't advance.
238 if (next == buffers_.end()) 234 if (next == buffers_.end())
(...skipping 19 matching lines...) Expand all
258 UpdateCurrentTime(current_buffer_, current_buffer_offset_); 254 UpdateCurrentTime(current_buffer_, current_buffer_offset_);
259 EvictBackwardBuffers(); 255 EvictBackwardBuffers();
260 } 256 }
261 257
262 return taken; 258 return taken;
263 } 259 }
264 260
265 void SeekableBuffer::UpdateCurrentTime(BufferQueue::iterator buffer, 261 void SeekableBuffer::UpdateCurrentTime(BufferQueue::iterator buffer,
266 int offset) { 262 int offset) {
267 // Garbage values are unavoidable, so this check will remain. 263 // Garbage values are unavoidable, so this check will remain.
268 if (buffer != buffers_.end() && (*buffer)->GetTimestamp() != kNoTimestamp()) { 264 if (buffer != buffers_.end() &&
269 int64 time_offset = ((*buffer)->GetDuration().InMicroseconds() * 265 (*buffer)->timestamp() != kNoTimestamp()) {
270 offset) / (*buffer)->GetDataSize(); 266 int64 time_offset = ((*buffer)->duration().InMicroseconds() * offset) /
267 (*buffer)->data_size();
271 268
272 current_time_ = (*buffer)->GetTimestamp() + 269 current_time_ = (*buffer)->timestamp() +
273 base::TimeDelta::FromMicroseconds(time_offset); 270 base::TimeDelta::FromMicroseconds(time_offset);
274 } 271 }
275 } 272 }
276 273
277 } // namespace media 274 } // namespace media
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698