OLD | NEW |
1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 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/buffer_queue.h" | 5 #include "media/base/buffer_queue.h" |
6 | 6 |
7 #include "media/base/buffers.h" | 7 #include "media/base/buffers.h" |
8 | 8 |
9 namespace media { | 9 namespace media { |
10 | 10 |
11 BufferQueue::BufferQueue() | 11 BufferQueue::BufferQueue() |
12 : data_offset_(0), | 12 : data_offset_(0), |
13 size_in_bytes_(0) { | 13 size_in_bytes_(0), |
| 14 most_recent_time_() { |
14 } | 15 } |
15 | 16 |
16 BufferQueue::~BufferQueue() { | 17 BufferQueue::~BufferQueue() { |
17 } | 18 } |
18 | 19 |
19 void BufferQueue::Consume(size_t bytes_to_be_consumed) { | 20 void BufferQueue::Consume(size_t bytes_to_be_consumed) { |
20 // Make sure user isn't trying to consume more than we have. | 21 // Make sure user isn't trying to consume more than we have. |
21 DCHECK(size_in_bytes_ >= bytes_to_be_consumed); | 22 DCHECK(size_in_bytes_ >= bytes_to_be_consumed); |
22 | 23 |
23 // As we have enough data to consume, adjust |size_in_bytes_|. | 24 // As we have enough data to consume, adjust |size_in_bytes_|. |
24 size_in_bytes_ -= bytes_to_be_consumed; | 25 size_in_bytes_ -= bytes_to_be_consumed; |
25 | 26 |
26 // Now consume them. | 27 // Now consume them. |
27 while (bytes_to_be_consumed > 0) { | 28 while (bytes_to_be_consumed > 0) { |
28 // Calculate number of usable bytes in the front of the |queue_|. | 29 // Calculate number of usable bytes in the front of the |queue_|. |
29 size_t front_remaining = queue_.front()->GetDataSize() - data_offset_; | 30 size_t front_remaining = queue_.front()->GetDataSize() - data_offset_; |
30 | 31 |
31 // If there is enough data in our first buffer to advance into it, do so. | 32 // If there is enough data in our first buffer to advance into it, do so. |
32 // Otherwise, advance into the queue. | 33 // Otherwise, advance into the queue. |
33 if (front_remaining > bytes_to_be_consumed) { | 34 if (front_remaining > bytes_to_be_consumed) { |
34 data_offset_ += bytes_to_be_consumed; | 35 data_offset_ += bytes_to_be_consumed; |
35 bytes_to_be_consumed = 0; | 36 bytes_to_be_consumed = 0; |
| 37 // Garbage values are unavoidable, so this check will remain. |
| 38 if (queue_.front()->GetTimestamp().InMicroseconds() > 0) { |
| 39 int64 offset = (queue_.front()->GetDuration().InMicroseconds() * |
| 40 data_offset_) / queue_.front()->GetDataSize(); |
| 41 |
| 42 most_recent_time_ = queue_.front()->GetTimestamp() + |
| 43 base::TimeDelta::FromMicroseconds(offset); |
| 44 } |
36 } else { | 45 } else { |
37 data_offset_ = 0; | 46 data_offset_ = 0; |
| 47 // Garbage values are unavoidable, so this check will remain. |
| 48 if (queue_.front()->GetTimestamp().InMicroseconds() > 0) { |
| 49 most_recent_time_ = queue_.front()->GetTimestamp() + |
| 50 queue_.front()->GetDuration(); |
| 51 } |
38 queue_.pop_front(); | 52 queue_.pop_front(); |
39 bytes_to_be_consumed -= front_remaining; | 53 bytes_to_be_consumed -= front_remaining; |
40 } | 54 } |
41 } | 55 } |
42 } | 56 } |
43 | 57 |
44 size_t BufferQueue::Copy(uint8* dest, size_t bytes) { | 58 size_t BufferQueue::Copy(uint8* dest, size_t bytes) { |
45 if (bytes == 0) | 59 if (bytes == 0) |
46 return 0; | 60 return 0; |
47 | 61 |
(...skipping 21 matching lines...) Expand all Loading... |
69 memcpy(dest + copied, current, current_remaining); | 83 memcpy(dest + copied, current, current_remaining); |
70 | 84 |
71 // Modify counts and pointers. | 85 // Modify counts and pointers. |
72 copied += current_remaining; | 86 copied += current_remaining; |
73 bytes -= current_remaining; | 87 bytes -= current_remaining; |
74 } | 88 } |
75 return copied; | 89 return copied; |
76 } | 90 } |
77 | 91 |
78 void BufferQueue::Enqueue(Buffer* buffer_in) { | 92 void BufferQueue::Enqueue(Buffer* buffer_in) { |
| 93 if (queue_.empty() && buffer_in->GetTimestamp().InMicroseconds() > 0) { |
| 94 most_recent_time_ = buffer_in->GetTimestamp(); |
| 95 } |
79 queue_.push_back(buffer_in); | 96 queue_.push_back(buffer_in); |
80 size_in_bytes_ += buffer_in->GetDataSize(); | 97 size_in_bytes_ += buffer_in->GetDataSize(); |
81 } | 98 } |
82 | 99 |
83 base::TimeDelta BufferQueue::GetTime(double bytes_to_sec) { | 100 base::TimeDelta BufferQueue::GetTime() { |
84 double bytes_to_usec = bytes_to_sec * base::Time::kMicrosecondsPerSecond; | 101 return most_recent_time_; |
85 | |
86 return queue_.front()->GetTimestamp() + | |
87 base::TimeDelta::FromMicroseconds(static_cast<int64>( | |
88 data_offset_ * bytes_to_usec)); | |
89 } | 102 } |
90 | 103 |
91 void BufferQueue::Clear() { | 104 void BufferQueue::Clear() { |
92 queue_.clear(); | 105 queue_.clear(); |
93 size_in_bytes_ = 0; | 106 size_in_bytes_ = 0; |
94 data_offset_ = 0; | 107 data_offset_ = 0; |
| 108 most_recent_time_ = base::TimeDelta(); |
95 } | 109 } |
96 | 110 |
97 bool BufferQueue::IsEmpty() { | 111 bool BufferQueue::IsEmpty() { |
98 // Since we keep track of the number of bytes, this is easier than calling | 112 // Since we keep track of the number of bytes, this is easier than calling |
99 // into |queue_|. | 113 // into |queue_|. |
100 return size_in_bytes_ == 0; | 114 return size_in_bytes_ == 0; |
101 } | 115 } |
102 | 116 |
103 size_t BufferQueue::SizeInBytes() { | 117 size_t BufferQueue::SizeInBytes() { |
104 return size_in_bytes_; | 118 return size_in_bytes_; |
105 } | 119 } |
106 | 120 |
107 } // namespace media | 121 } // namespace media |
OLD | NEW |