OLD | NEW |
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" |
(...skipping 16 matching lines...) Expand all Loading... |
27 | 27 |
28 void SeekableBuffer::Clear() { | 28 void SeekableBuffer::Clear() { |
29 buffers_.clear(); | 29 buffers_.clear(); |
30 current_buffer_ = buffers_.begin(); | 30 current_buffer_ = buffers_.begin(); |
31 current_buffer_offset_ = 0; | 31 current_buffer_offset_ = 0; |
32 backward_bytes_ = 0; | 32 backward_bytes_ = 0; |
33 forward_bytes_ = 0; | 33 forward_bytes_ = 0; |
34 current_time_ = kNoTimestamp(); | 34 current_time_ = kNoTimestamp(); |
35 } | 35 } |
36 | 36 |
37 int SeekableBuffer::Read(uint8* data, int size) { | 37 int SeekableBuffer::Read(uint8_t* data, int size) { |
38 DCHECK(data); | 38 DCHECK(data); |
39 return InternalRead(data, size, true, 0); | 39 return InternalRead(data, size, true, 0); |
40 } | 40 } |
41 | 41 |
42 int SeekableBuffer::Peek(uint8* data, int size, int forward_offset) { | 42 int SeekableBuffer::Peek(uint8_t* data, int size, int forward_offset) { |
43 DCHECK(data); | 43 DCHECK(data); |
44 return InternalRead(data, size, false, forward_offset); | 44 return InternalRead(data, size, false, forward_offset); |
45 } | 45 } |
46 | 46 |
47 bool SeekableBuffer::GetCurrentChunk(const uint8** data, int* size) const { | 47 bool SeekableBuffer::GetCurrentChunk(const uint8_t** data, int* size) const { |
48 BufferQueue::iterator current_buffer = current_buffer_; | 48 BufferQueue::iterator current_buffer = current_buffer_; |
49 int current_buffer_offset = current_buffer_offset_; | 49 int current_buffer_offset = current_buffer_offset_; |
50 // Advance position if we are in the end of the current buffer. | 50 // Advance position if we are in the end of the current buffer. |
51 while (current_buffer != buffers_.end() && | 51 while (current_buffer != buffers_.end() && |
52 current_buffer_offset >= (*current_buffer)->data_size()) { | 52 current_buffer_offset >= (*current_buffer)->data_size()) { |
53 ++current_buffer; | 53 ++current_buffer; |
54 current_buffer_offset = 0; | 54 current_buffer_offset = 0; |
55 } | 55 } |
56 if (current_buffer == buffers_.end()) | 56 if (current_buffer == buffers_.end()) |
57 return false; | 57 return false; |
(...skipping 22 matching lines...) Expand all Loading... |
80 forward_bytes_ += buffer_in->data_size(); | 80 forward_bytes_ += buffer_in->data_size(); |
81 | 81 |
82 // Advise the user to stop append if the amount of forward bytes exceeds | 82 // 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 | 83 // the forward capacity. A false return value means the user should stop |
84 // appending more data to this buffer. | 84 // appending more data to this buffer. |
85 if (forward_bytes_ >= forward_capacity_) | 85 if (forward_bytes_ >= forward_capacity_) |
86 return false; | 86 return false; |
87 return true; | 87 return true; |
88 } | 88 } |
89 | 89 |
90 bool SeekableBuffer::Append(const uint8* data, int size) { | 90 bool SeekableBuffer::Append(const uint8_t* data, int size) { |
91 if (size > 0) { | 91 if (size > 0) { |
92 scoped_refptr<DataBuffer> data_buffer = DataBuffer::CopyFrom(data, size); | 92 scoped_refptr<DataBuffer> data_buffer = DataBuffer::CopyFrom(data, size); |
93 return Append(data_buffer); | 93 return Append(data_buffer); |
94 } else { | 94 } else { |
95 // Return true if we have forward capacity. | 95 // Return true if we have forward capacity. |
96 return forward_bytes_ < forward_capacity_; | 96 return forward_bytes_ < forward_capacity_; |
97 } | 97 } |
98 } | 98 } |
99 | 99 |
100 bool SeekableBuffer::Seek(int32 offset) { | 100 bool SeekableBuffer::Seek(int32_t offset) { |
101 if (offset > 0) | 101 if (offset > 0) |
102 return SeekForward(offset); | 102 return SeekForward(offset); |
103 else if (offset < 0) | 103 else if (offset < 0) |
104 return SeekBackward(-offset); | 104 return SeekBackward(-offset); |
105 return true; | 105 return true; |
106 } | 106 } |
107 | 107 |
108 bool SeekableBuffer::SeekForward(int size) { | 108 bool SeekableBuffer::SeekForward(int size) { |
109 // Perform seeking forward only if we have enough bytes in the queue. | 109 // Perform seeking forward only if we have enough bytes in the queue. |
110 if (size > forward_bytes_) | 110 if (size > forward_bytes_) |
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
173 if (i == current_buffer_) | 173 if (i == current_buffer_) |
174 break; | 174 break; |
175 scoped_refptr<DataBuffer> buffer = *i; | 175 scoped_refptr<DataBuffer> buffer = *i; |
176 backward_bytes_ -= buffer->data_size(); | 176 backward_bytes_ -= buffer->data_size(); |
177 DCHECK_GE(backward_bytes_, 0); | 177 DCHECK_GE(backward_bytes_, 0); |
178 | 178 |
179 buffers_.erase(i); | 179 buffers_.erase(i); |
180 } | 180 } |
181 } | 181 } |
182 | 182 |
183 int SeekableBuffer::InternalRead(uint8* data, int size, | 183 int SeekableBuffer::InternalRead(uint8_t* data, |
| 184 int size, |
184 bool advance_position, | 185 bool advance_position, |
185 int forward_offset) { | 186 int forward_offset) { |
186 // Counts how many bytes are actually read from the buffer queue. | 187 // Counts how many bytes are actually read from the buffer queue. |
187 int taken = 0; | 188 int taken = 0; |
188 | 189 |
189 BufferQueue::iterator current_buffer = current_buffer_; | 190 BufferQueue::iterator current_buffer = current_buffer_; |
190 int current_buffer_offset = current_buffer_offset_; | 191 int current_buffer_offset = current_buffer_offset_; |
191 | 192 |
192 int bytes_to_skip = forward_offset; | 193 int bytes_to_skip = forward_offset; |
193 while (taken < size) { | 194 while (taken < size) { |
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
260 } | 261 } |
261 | 262 |
262 return taken; | 263 return taken; |
263 } | 264 } |
264 | 265 |
265 void SeekableBuffer::UpdateCurrentTime(BufferQueue::iterator buffer, | 266 void SeekableBuffer::UpdateCurrentTime(BufferQueue::iterator buffer, |
266 int offset) { | 267 int offset) { |
267 // Garbage values are unavoidable, so this check will remain. | 268 // Garbage values are unavoidable, so this check will remain. |
268 if (buffer != buffers_.end() && | 269 if (buffer != buffers_.end() && |
269 (*buffer)->timestamp() != kNoTimestamp()) { | 270 (*buffer)->timestamp() != kNoTimestamp()) { |
270 int64 time_offset = ((*buffer)->duration().InMicroseconds() * offset) / | 271 int64_t time_offset = ((*buffer)->duration().InMicroseconds() * offset) / |
271 (*buffer)->data_size(); | 272 (*buffer)->data_size(); |
272 | 273 |
273 current_time_ = (*buffer)->timestamp() + | 274 current_time_ = (*buffer)->timestamp() + |
274 base::TimeDelta::FromMicroseconds(time_offset); | 275 base::TimeDelta::FromMicroseconds(time_offset); |
275 } | 276 } |
276 } | 277 } |
277 | 278 |
278 } // namespace media | 279 } // namespace media |
OLD | NEW |