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

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

Powered by Google App Engine
This is Rietveld 408576698