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 using base::subtle::MemoryBarrier; | |
12 | |
9 namespace media { | 13 namespace media { |
10 | 14 |
11 // Given current position in the FIFO, the maximum number of elements in the | 15 // Given current position in the FIFO, the maximum number of elements in the |
12 // FIFO and the size of the input; this method provides two output results: | 16 // FIFO and the size of the input; this method provides two output results: |
13 // |size| and |wrap_size|. These two results can then be utilized for memcopy | 17 // |size| and |wrap_size|. These two results can then be utilized for memcopy |
14 // operations to and from the FIFO. | 18 // operations to and from the FIFO. |
15 // Under "normal" circumstances, |size| will be equal to |in_size| and | 19 // Under "normal" circumstances, |size| will be equal to |in_size| and |
16 // |wrap_size| will be zero. This case corresponding to the non-wrapping case | 20 // |wrap_size| will be zero. This case corresponding to the non-wrapping case |
17 // where we have not yet reached the "edge" of the FIFO. If |pos| + |in_size| | 21 // where we have not yet reached the "edge" of the FIFO. If |pos| + |in_size| |
18 // exceeds the total size of the FIFO, we must wrap around and start reusing | 22 // exceeds the total size of the FIFO, we must wrap around and start reusing |
(...skipping 14 matching lines...) Expand all Loading... | |
33 // Updates the read/write position with |step| modulo the maximum number of | 37 // Updates the read/write position with |step| modulo the maximum number of |
34 // elements in the FIFO to ensure that the position counters wraps around at | 38 // elements in the FIFO to ensure that the position counters wraps around at |
35 // the endpoint. | 39 // the endpoint. |
36 static int UpdatePos(int pos, int step, int max_size) { | 40 static int UpdatePos(int pos, int step, int max_size) { |
37 return ((pos + step) % max_size); | 41 return ((pos + step) % max_size); |
38 } | 42 } |
39 | 43 |
40 AudioFifo::AudioFifo(int channels, int frames) | 44 AudioFifo::AudioFifo(int channels, int frames) |
41 : audio_bus_(AudioBus::Create(channels, frames)), | 45 : audio_bus_(AudioBus::Create(channels, frames)), |
42 max_frames_(frames), | 46 max_frames_(frames), |
43 frames_(0), | 47 frames_pushed_(0), |
48 frames_consumed_(0), | |
44 read_pos_(0), | 49 read_pos_(0), |
45 write_pos_(0) {} | 50 write_pos_(0) {} |
46 | 51 |
47 AudioFifo::~AudioFifo() {} | 52 AudioFifo::~AudioFifo() {} |
48 | 53 |
54 int AudioFifo::frames() const { | |
55 MemoryBarrier(); | |
56 return frames_pushed_ - frames_consumed_; | |
DaleCurtis
2012/09/17 21:17:36
Seems like this could overflow on 32-bit systems p
Chris Rogers
2012/09/17 22:00:42
It can, but the math still works out properly if e
| |
57 } | |
58 | |
49 void AudioFifo::Push(const AudioBus* source) { | 59 void AudioFifo::Push(const AudioBus* source) { |
50 DCHECK(source); | 60 DCHECK(source); |
51 DCHECK_EQ(source->channels(), audio_bus_->channels()); | 61 DCHECK_EQ(source->channels(), audio_bus_->channels()); |
52 | 62 |
53 // Ensure that there is space for the new data in the FIFO. | 63 // Ensure that there is space for the new data in the FIFO. |
54 const int source_size = source->frames(); | 64 const int source_size = source->frames(); |
55 CHECK_LE(source_size + frames_, max_frames_); | 65 CHECK_LE(source_size + frames(), max_frames_); |
56 | 66 |
57 // Figure out if wrapping is needed and if so what segment sizes we need | 67 // Figure out if wrapping is needed and if so what segment sizes we need |
58 // when adding the new audio bus content to the FIFO. | 68 // when adding the new audio bus content to the FIFO. |
59 int append_size = 0; | 69 int append_size = 0; |
60 int wrap_size = 0; | 70 int wrap_size = 0; |
61 GetSizes(write_pos_, max_frames(), source_size, &append_size, &wrap_size); | 71 GetSizes(write_pos_, max_frames(), source_size, &append_size, &wrap_size); |
62 | 72 |
63 // Copy all channels from the source to the FIFO. Wrap around if needed. | 73 // Copy all channels from the source to the FIFO. Wrap around if needed. |
64 for (int ch = 0; ch < source->channels(); ++ch) { | 74 for (int ch = 0; ch < source->channels(); ++ch) { |
65 float* dest = audio_bus_->channel(ch); | 75 float* dest = audio_bus_->channel(ch); |
66 const float* src = source->channel(ch); | 76 const float* src = source->channel(ch); |
67 | 77 |
68 // Append part of (or the complete) source to the FIFO. | 78 // Append part of (or the complete) source to the FIFO. |
69 memcpy(&dest[write_pos_], &src[0], append_size * sizeof(src[0])); | 79 memcpy(&dest[write_pos_], &src[0], append_size * sizeof(src[0])); |
70 if (wrap_size > 0) { | 80 if (wrap_size > 0) { |
71 // Wrapping is needed: copy remaining part from the source to the FIFO. | 81 // Wrapping is needed: copy remaining part from the source to the FIFO. |
72 memcpy(&dest[0], &src[append_size], wrap_size * sizeof(src[0])); | 82 memcpy(&dest[0], &src[append_size], wrap_size * sizeof(src[0])); |
73 } | 83 } |
74 } | 84 } |
75 | 85 |
76 frames_ += source_size; | 86 // Ensure the data is *really* written before updating |frames_pushed_|. |
77 DCHECK_LE(frames_, max_frames()); | 87 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()); | |
78 write_pos_ = UpdatePos(write_pos_, source_size, max_frames()); | 93 write_pos_ = UpdatePos(write_pos_, source_size, max_frames()); |
79 } | 94 } |
80 | 95 |
81 void AudioFifo::Consume(AudioBus* destination, | 96 void AudioFifo::Consume(AudioBus* destination, |
82 int start_frame, | 97 int start_frame, |
83 int frames_to_consume) { | 98 int frames_to_consume) { |
84 DCHECK(destination); | 99 DCHECK(destination); |
85 DCHECK_EQ(destination->channels(), audio_bus_->channels()); | 100 DCHECK_EQ(destination->channels(), audio_bus_->channels()); |
86 | 101 |
87 // It is not possible to ask for more data than what is available in the FIFO. | 102 // It is not possible to ask for more data than what is available in the FIFO. |
88 CHECK_LE(frames_to_consume, frames_); | 103 CHECK_LE(frames_to_consume, frames()); |
89 | 104 |
90 // A copy from the FIFO to |destination| will only be performed if the | 105 // A copy from the FIFO to |destination| will only be performed if the |
91 // allocated memory in |destination| is sufficient. | 106 // allocated memory in |destination| is sufficient. |
92 CHECK_LE(frames_to_consume + start_frame, destination->frames()); | 107 CHECK_LE(frames_to_consume + start_frame, destination->frames()); |
93 | 108 |
94 // Figure out if wrapping is needed and if so what segment sizes we need | 109 // Figure out if wrapping is needed and if so what segment sizes we need |
95 // when removing audio bus content from the FIFO. | 110 // when removing audio bus content from the FIFO. |
96 int consume_size = 0; | 111 int consume_size = 0; |
97 int wrap_size = 0; | 112 int wrap_size = 0; |
98 GetSizes(read_pos_, max_frames(), frames_to_consume, | 113 GetSizes(read_pos_, max_frames(), frames_to_consume, |
99 &consume_size, &wrap_size); | 114 &consume_size, &wrap_size); |
100 | 115 |
101 // For all channels, remove the requested amount of data from the FIFO | 116 // For all channels, remove the requested amount of data from the FIFO |
102 // and copy the content to the destination. Wrap around if needed. | 117 // and copy the content to the destination. Wrap around if needed. |
103 for (int ch = 0; ch < destination->channels(); ++ch) { | 118 for (int ch = 0; ch < destination->channels(); ++ch) { |
104 float* dest = destination->channel(ch); | 119 float* dest = destination->channel(ch); |
105 const float* src = audio_bus_->channel(ch); | 120 const float* src = audio_bus_->channel(ch); |
106 | 121 |
107 // Copy a selected part of the FIFO to the destination. | 122 // Copy a selected part of the FIFO to the destination. |
108 memcpy(&dest[start_frame], &src[read_pos_], consume_size * sizeof(src[0])); | 123 memcpy(&dest[start_frame], &src[read_pos_], consume_size * sizeof(src[0])); |
109 if (wrap_size > 0) { | 124 if (wrap_size > 0) { |
110 // Wrapping is needed: copy remaining part to the destination. | 125 // Wrapping is needed: copy remaining part to the destination. |
111 memcpy(&dest[consume_size + start_frame], &src[0], | 126 memcpy(&dest[consume_size + start_frame], &src[0], |
112 wrap_size * sizeof(src[0])); | 127 wrap_size * sizeof(src[0])); |
113 } | 128 } |
114 } | 129 } |
115 | 130 |
116 frames_ -= frames_to_consume; | 131 Atomic32 new_frames_consumed = frames_consumed_ + frames_to_consume; |
132 NoBarrier_Store(&frames_consumed_, new_frames_consumed); | |
133 | |
117 read_pos_ = UpdatePos(read_pos_, frames_to_consume, max_frames()); | 134 read_pos_ = UpdatePos(read_pos_, frames_to_consume, max_frames()); |
118 } | 135 } |
119 | 136 |
120 void AudioFifo::Clear() { | 137 void AudioFifo::Clear() { |
121 frames_ = 0; | 138 frames_pushed_ = 0; |
139 frames_consumed_ = 0; | |
122 read_pos_ = 0; | 140 read_pos_ = 0; |
123 write_pos_ = 0; | 141 write_pos_ = 0; |
124 } | 142 } |
125 | 143 |
126 } // namespace media | 144 } // namespace media |
OLD | NEW |