OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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/audio_buffer_queue.h" | 5 #include "media/base/audio_buffer_queue.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/audio_bus.h" | 10 #include "media/base/audio_bus.h" |
11 #include "media/base/buffers.h" | 11 #include "media/base/buffers.h" |
12 | 12 |
13 namespace media { | 13 namespace media { |
14 | 14 |
15 AudioBufferQueue::AudioBufferQueue() { Clear(); } | 15 AudioBufferQueue::AudioBufferQueue() { Clear(); } |
16 AudioBufferQueue::~AudioBufferQueue() {} | 16 AudioBufferQueue::~AudioBufferQueue() {} |
17 | 17 |
18 void AudioBufferQueue::Clear() { | 18 void AudioBufferQueue::Clear() { |
19 buffers_.clear(); | 19 buffers_.clear(); |
20 current_buffer_ = buffers_.begin(); | 20 current_buffer_ = buffers_.begin(); |
21 current_buffer_offset_ = 0; | 21 current_buffer_offset_ = 0; |
22 frames_ = 0; | 22 frames_ = 0; |
23 current_time_ = kNoTimestamp(); | |
24 } | 23 } |
25 | 24 |
26 void AudioBufferQueue::Append(const scoped_refptr<AudioBuffer>& buffer_in) { | 25 void AudioBufferQueue::Append(const scoped_refptr<AudioBuffer>& buffer_in) { |
27 // If we have just written the first buffer, update |current_time_| to be the | |
28 // start time. | |
29 if (buffers_.empty() && buffer_in->timestamp() != kNoTimestamp()) { | |
30 current_time_ = buffer_in->timestamp(); | |
31 } | |
32 | |
33 // Add the buffer to the queue. Inserting into deque invalidates all | 26 // Add the buffer to the queue. Inserting into deque invalidates all |
34 // iterators, so point to the first buffer. | 27 // iterators, so point to the first buffer. |
35 buffers_.push_back(buffer_in); | 28 buffers_.push_back(buffer_in); |
36 current_buffer_ = buffers_.begin(); | 29 current_buffer_ = buffers_.begin(); |
37 | 30 |
38 // Update the |frames_| counter since we have added frames. | 31 // Update the |frames_| counter since we have added frames. |
39 frames_ += buffer_in->frame_count(); | 32 frames_ += buffer_in->frame_count(); |
40 CHECK_GT(frames_, 0); // make sure it doesn't overflow. | 33 CHECK_GT(frames_, 0); // make sure it doesn't overflow. |
41 } | 34 } |
42 | 35 |
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
107 // this loop. | 100 // this loop. |
108 taken += copied; | 101 taken += copied; |
109 | 102 |
110 // We have read |copied| frames from the current buffer. Advance the | 103 // We have read |copied| frames from the current buffer. Advance the |
111 // offset. | 104 // offset. |
112 current_buffer_offset += copied; | 105 current_buffer_offset += copied; |
113 } | 106 } |
114 | 107 |
115 // Has the buffer has been consumed? | 108 // Has the buffer has been consumed? |
116 if (current_buffer_offset == buffer->frame_count()) { | 109 if (current_buffer_offset == buffer->frame_count()) { |
117 if (advance_position) { | |
118 // Next buffer may not have timestamp, so we need to update current | |
119 // timestamp before switching to the next buffer. | |
120 UpdateCurrentTime(current_buffer, current_buffer_offset); | |
121 } | |
122 | |
123 // If we are at the last buffer, no more data to be copied, so stop. | 110 // If we are at the last buffer, no more data to be copied, so stop. |
124 BufferQueue::iterator next = current_buffer + 1; | 111 BufferQueue::iterator next = current_buffer + 1; |
125 if (next == buffers_.end()) | 112 if (next == buffers_.end()) |
126 break; | 113 break; |
127 | 114 |
128 // Advances the iterator. | 115 // Advances the iterator. |
129 current_buffer = next; | 116 current_buffer = next; |
130 current_buffer_offset = 0; | 117 current_buffer_offset = 0; |
131 } | 118 } |
132 } | 119 } |
133 | 120 |
134 if (advance_position) { | 121 if (advance_position) { |
135 // Update the appropriate values since |taken| frames have been copied out. | 122 // Update the appropriate values since |taken| frames have been copied out. |
136 frames_ -= taken; | 123 frames_ -= taken; |
137 DCHECK_GE(frames_, 0); | 124 DCHECK_GE(frames_, 0); |
138 DCHECK(current_buffer_ != buffers_.end() || frames_ == 0); | 125 DCHECK(current_buffer_ != buffers_.end() || frames_ == 0); |
139 | 126 |
140 UpdateCurrentTime(current_buffer, current_buffer_offset); | |
141 | |
142 // Remove any buffers before the current buffer as there is no going | 127 // Remove any buffers before the current buffer as there is no going |
143 // backwards. | 128 // backwards. |
144 buffers_.erase(buffers_.begin(), current_buffer); | 129 buffers_.erase(buffers_.begin(), current_buffer); |
145 current_buffer_ = buffers_.begin(); | 130 current_buffer_ = buffers_.begin(); |
146 current_buffer_offset_ = current_buffer_offset; | 131 current_buffer_offset_ = current_buffer_offset; |
147 } | 132 } |
148 | 133 |
149 return taken; | 134 return taken; |
150 } | 135 } |
151 | 136 |
152 void AudioBufferQueue::UpdateCurrentTime(BufferQueue::iterator buffer, | |
153 int offset) { | |
154 if (buffer != buffers_.end() && (*buffer)->timestamp() != kNoTimestamp()) { | |
155 double time_offset = ((*buffer)->duration().InMicroseconds() * offset) / | |
156 static_cast<double>((*buffer)->frame_count()); | |
157 current_time_ = | |
158 (*buffer)->timestamp() + base::TimeDelta::FromMicroseconds( | |
159 static_cast<int64>(time_offset + 0.5)); | |
160 } | |
161 } | |
162 | |
163 } // namespace media | 137 } // namespace media |
OLD | NEW |