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" |
(...skipping 17 matching lines...) Expand all Loading... |
28 current_buffer_ = buffers_.begin(); | 28 current_buffer_ = buffers_.begin(); |
29 | 29 |
30 // Update the |frames_| counter since we have added frames. | 30 // Update the |frames_| counter since we have added frames. |
31 frames_ += buffer_in->frame_count(); | 31 frames_ += buffer_in->frame_count(); |
32 CHECK_GT(frames_, 0); // make sure it doesn't overflow. | 32 CHECK_GT(frames_, 0); // make sure it doesn't overflow. |
33 } | 33 } |
34 | 34 |
35 int AudioBufferQueue::ReadFrames(int frames, | 35 int AudioBufferQueue::ReadFrames(int frames, |
36 int dest_frame_offset, | 36 int dest_frame_offset, |
37 AudioBus* dest) { | 37 AudioBus* dest) { |
38 DCHECK_GE(dest->frames(), frames + dest_frame_offset); | 38 if (!dest->is_raw_format()) |
| 39 DCHECK_GE(dest->frames(), frames + dest_frame_offset); |
39 return InternalRead(frames, true, 0, dest_frame_offset, dest); | 40 return InternalRead(frames, true, 0, dest_frame_offset, dest); |
40 } | 41 } |
41 | 42 |
42 int AudioBufferQueue::PeekFrames(int frames, | 43 int AudioBufferQueue::PeekFrames(int frames, |
43 int source_frame_offset, | 44 int source_frame_offset, |
44 int dest_frame_offset, | 45 int dest_frame_offset, |
45 AudioBus* dest) { | 46 AudioBus* dest) { |
46 DCHECK_GE(dest->frames(), frames); | 47 DCHECK_GE(dest->frames(), frames); |
47 return InternalRead( | 48 return InternalRead( |
48 frames, false, source_frame_offset, dest_frame_offset, dest); | 49 frames, false, source_frame_offset, dest_frame_offset, dest); |
49 } | 50 } |
50 | 51 |
51 void AudioBufferQueue::SeekFrames(int frames) { | 52 void AudioBufferQueue::SeekFrames(int frames) { |
52 // Perform seek only if we have enough bytes in the queue. | 53 // Perform seek only if we have enough bytes in the queue. |
53 CHECK_LE(frames, frames_); | 54 CHECK_LE(frames, frames_); |
54 int taken = InternalRead(frames, true, 0, 0, NULL); | 55 int taken = InternalRead(frames, true, 0, 0, NULL); |
55 DCHECK_EQ(taken, frames); | 56 DCHECK_EQ(taken, frames); |
56 } | 57 } |
57 | 58 |
58 int AudioBufferQueue::InternalRead(int frames, | 59 int AudioBufferQueue::InternalRead(int frames, |
59 bool advance_position, | 60 bool advance_position, |
60 int source_frame_offset, | 61 int source_frame_offset, |
61 int dest_frame_offset, | 62 int dest_frame_offset, |
62 AudioBus* dest) { | 63 AudioBus* dest) { |
| 64 if (!buffers_.size()) |
| 65 return 0; |
| 66 |
| 67 if ((*buffers_.begin())->sample_format() == kSampleFormatRaw) { |
| 68 DCHECK(!dest_frame_offset); |
| 69 DCHECK(!source_frame_offset); |
| 70 |
| 71 scoped_refptr<AudioBuffer> buffer = buffers_.front(); |
| 72 int taken = buffer->frame_count(); |
| 73 |
| 74 // if |dest| is NULL, there's no need to copy. |
| 75 if (dest) |
| 76 buffer->ReadFrames(buffer->frame_count(), 0, dest->data_size(), dest); |
| 77 |
| 78 if (advance_position) { |
| 79 // Update the appropriate values since |taken| frames have been copied |
| 80 // out. |
| 81 frames_ -= taken; |
| 82 DCHECK_GE(frames_, 0); |
| 83 |
| 84 // Remove any buffers before the current buffer as there is no going |
| 85 // backwards. |
| 86 buffers_.pop_front(); |
| 87 current_buffer_ = buffers_.begin(); |
| 88 } |
| 89 |
| 90 return taken; |
| 91 } |
| 92 |
63 // Counts how many frames are actually read from the buffer queue. | 93 // Counts how many frames are actually read from the buffer queue. |
64 int taken = 0; | 94 int taken = 0; |
65 BufferQueue::iterator current_buffer = current_buffer_; | 95 BufferQueue::iterator current_buffer = current_buffer_; |
66 int current_buffer_offset = current_buffer_offset_; | 96 int current_buffer_offset = current_buffer_offset_; |
67 | 97 |
68 int frames_to_skip = source_frame_offset; | 98 int frames_to_skip = source_frame_offset; |
69 while (taken < frames) { | 99 while (taken < frames) { |
70 // |current_buffer| is valid since the first time this buffer is appended | 100 // |current_buffer| is valid since the first time this buffer is appended |
71 // with data. Make sure there is data to be processed. | 101 // with data. Make sure there is data to be processed. |
72 if (current_buffer == buffers_.end()) | 102 if (current_buffer == buffers_.end()) |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
127 // backwards. | 157 // backwards. |
128 buffers_.erase(buffers_.begin(), current_buffer); | 158 buffers_.erase(buffers_.begin(), current_buffer); |
129 current_buffer_ = buffers_.begin(); | 159 current_buffer_ = buffers_.begin(); |
130 current_buffer_offset_ = current_buffer_offset; | 160 current_buffer_offset_ = current_buffer_offset; |
131 } | 161 } |
132 | 162 |
133 return taken; | 163 return taken; |
134 } | 164 } |
135 | 165 |
136 } // namespace media | 166 } // namespace media |
OLD | NEW |