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/audio_fifo.h" | 5 #include "media/base/audio_fifo.h" |
6 | 6 |
7 #include "base/logging.h" | 7 #include "base/logging.h" |
8 | 8 |
9 using base::subtle::Atomic32; | |
10 using base::subtle::NoBarrier_Store; | |
11 | |
12 namespace media { | 9 namespace media { |
13 | 10 |
14 // Given current position in the FIFO, the maximum number of elements in the | 11 // Given current position in the FIFO, the maximum number of elements in the |
15 // FIFO and the size of the input; this method provides two output results: | 12 // FIFO and the size of the input; this method provides two output results: |
16 // |size| and |wrap_size|. These two results can then be utilized for memcopy | 13 // |size| and |wrap_size|. These two results can then be utilized for memcopy |
17 // operations to and from the FIFO. | 14 // operations to and from the FIFO. |
18 // Under "normal" circumstances, |size| will be equal to |in_size| and | 15 // Under "normal" circumstances, |size| will be equal to |in_size| and |
19 // |wrap_size| will be zero. This case corresponding to the non-wrapping case | 16 // |wrap_size| will be zero. This case corresponding to the non-wrapping case |
20 // where we have not yet reached the "edge" of the FIFO. If |pos| + |in_size| | 17 // where we have not yet reached the "edge" of the FIFO. If |pos| + |in_size| |
21 // exceeds the total size of the FIFO, we must wrap around and start reusing | 18 // exceeds the total size of the FIFO, we must wrap around and start reusing |
(...skipping 23 matching lines...) Expand all Loading... |
45 max_frames_(frames), | 42 max_frames_(frames), |
46 frames_pushed_(0), | 43 frames_pushed_(0), |
47 frames_consumed_(0), | 44 frames_consumed_(0), |
48 read_pos_(0), | 45 read_pos_(0), |
49 write_pos_(0) {} | 46 write_pos_(0) {} |
50 | 47 |
51 AudioFifo::~AudioFifo() {} | 48 AudioFifo::~AudioFifo() {} |
52 | 49 |
53 int AudioFifo::frames() const { | 50 int AudioFifo::frames() const { |
54 int delta = frames_pushed_ - frames_consumed_; | 51 int delta = frames_pushed_ - frames_consumed_; |
55 base::subtle::MemoryBarrier(); | |
56 return delta; | 52 return delta; |
57 } | 53 } |
58 | 54 |
59 void AudioFifo::Push(const AudioBus* source) { | 55 void AudioFifo::Push(const AudioBus* source) { |
60 DCHECK(source); | 56 DCHECK(source); |
61 DCHECK_EQ(source->channels(), audio_bus_->channels()); | 57 DCHECK_EQ(source->channels(), audio_bus_->channels()); |
62 | 58 |
63 // Ensure that there is space for the new data in the FIFO. | 59 // Ensure that there is space for the new data in the FIFO. |
64 const int source_size = source->frames(); | 60 const int source_size = source->frames(); |
65 CHECK_LE(source_size + frames(), max_frames_); | 61 CHECK_LE(source_size + frames(), max_frames_); |
(...skipping 10 matching lines...) Expand all Loading... |
76 const float* src = source->channel(ch); | 72 const float* src = source->channel(ch); |
77 | 73 |
78 // Append part of (or the complete) source to the FIFO. | 74 // Append part of (or the complete) source to the FIFO. |
79 memcpy(&dest[write_pos_], &src[0], append_size * sizeof(src[0])); | 75 memcpy(&dest[write_pos_], &src[0], append_size * sizeof(src[0])); |
80 if (wrap_size > 0) { | 76 if (wrap_size > 0) { |
81 // Wrapping is needed: copy remaining part from the source to the FIFO. | 77 // Wrapping is needed: copy remaining part from the source to the FIFO. |
82 memcpy(&dest[0], &src[append_size], wrap_size * sizeof(src[0])); | 78 memcpy(&dest[0], &src[append_size], wrap_size * sizeof(src[0])); |
83 } | 79 } |
84 } | 80 } |
85 | 81 |
86 // Ensure the data is *really* written before updating |frames_pushed_|. | 82 frames_pushed_ += source_size; |
87 base::subtle::MemoryBarrier(); | |
88 | |
89 Atomic32 new_frames_pushed = frames_pushed_ + source_size; | |
90 NoBarrier_Store(&frames_pushed_, new_frames_pushed); | |
91 | |
92 DCHECK_LE(frames(), max_frames()); | 83 DCHECK_LE(frames(), max_frames()); |
93 write_pos_ = UpdatePos(write_pos_, source_size, max_frames()); | 84 write_pos_ = UpdatePos(write_pos_, source_size, max_frames()); |
94 } | 85 } |
95 | 86 |
96 void AudioFifo::Consume(AudioBus* destination, | 87 void AudioFifo::Consume(AudioBus* destination, |
97 int start_frame, | 88 int start_frame, |
98 int frames_to_consume) { | 89 int frames_to_consume) { |
99 DCHECK(destination); | 90 DCHECK(destination); |
100 DCHECK_EQ(destination->channels(), audio_bus_->channels()); | 91 DCHECK_EQ(destination->channels(), audio_bus_->channels()); |
101 | 92 |
(...skipping 19 matching lines...) Expand all Loading... |
121 | 112 |
122 // Copy a selected part of the FIFO to the destination. | 113 // Copy a selected part of the FIFO to the destination. |
123 memcpy(&dest[start_frame], &src[read_pos_], consume_size * sizeof(src[0])); | 114 memcpy(&dest[start_frame], &src[read_pos_], consume_size * sizeof(src[0])); |
124 if (wrap_size > 0) { | 115 if (wrap_size > 0) { |
125 // Wrapping is needed: copy remaining part to the destination. | 116 // Wrapping is needed: copy remaining part to the destination. |
126 memcpy(&dest[consume_size + start_frame], &src[0], | 117 memcpy(&dest[consume_size + start_frame], &src[0], |
127 wrap_size * sizeof(src[0])); | 118 wrap_size * sizeof(src[0])); |
128 } | 119 } |
129 } | 120 } |
130 | 121 |
131 Atomic32 new_frames_consumed = frames_consumed_ + frames_to_consume; | 122 frames_consumed_ += frames_to_consume; |
132 NoBarrier_Store(&frames_consumed_, new_frames_consumed); | |
133 | |
134 read_pos_ = UpdatePos(read_pos_, frames_to_consume, max_frames()); | 123 read_pos_ = UpdatePos(read_pos_, frames_to_consume, max_frames()); |
135 } | 124 } |
136 | 125 |
137 void AudioFifo::Clear() { | 126 void AudioFifo::Clear() { |
138 frames_pushed_ = 0; | 127 frames_pushed_ = 0; |
139 frames_consumed_ = 0; | 128 frames_consumed_ = 0; |
140 read_pos_ = 0; | 129 read_pos_ = 0; |
141 write_pos_ = 0; | 130 write_pos_ = 0; |
142 } | 131 } |
143 | 132 |
144 } // namespace media | 133 } // namespace media |
OLD | NEW |