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_AtomicExchange; | |
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 |
49 void AudioFifo::Push(const AudioBus* source) { | 54 void AudioFifo::Push(const AudioBus* source) { |
50 DCHECK(source); | 55 DCHECK(source); |
51 DCHECK_EQ(source->channels(), audio_bus_->channels()); | 56 DCHECK_EQ(source->channels(), audio_bus_->channels()); |
52 | 57 |
53 // Ensure that there is space for the new data in the FIFO. | 58 // Ensure that there is space for the new data in the FIFO. |
54 const int source_size = source->frames(); | 59 const int source_size = source->frames(); |
55 CHECK_LE(source_size + frames_, max_frames_); | 60 CHECK_LE(source_size + frames(), max_frames_); |
56 | 61 |
57 // Figure out if wrapping is needed and if so what segment sizes we need | 62 // 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. | 63 // when adding the new audio bus content to the FIFO. |
59 int append_size = 0; | 64 int append_size = 0; |
60 int wrap_size = 0; | 65 int wrap_size = 0; |
61 GetSizes(write_pos_, max_frames(), source_size, &append_size, &wrap_size); | 66 GetSizes(write_pos_, max_frames(), source_size, &append_size, &wrap_size); |
62 | 67 |
63 // Copy all channels from the source to the FIFO. Wrap around if needed. | 68 // Copy all channels from the source to the FIFO. Wrap around if needed. |
64 for (int ch = 0; ch < source->channels(); ++ch) { | 69 for (int ch = 0; ch < source->channels(); ++ch) { |
65 float* dest = audio_bus_->channel(ch); | 70 float* dest = audio_bus_->channel(ch); |
66 const float* src = source->channel(ch); | 71 const float* src = source->channel(ch); |
67 | 72 |
68 // Append part of (or the complete) source to the FIFO. | 73 // Append part of (or the complete) source to the FIFO. |
69 memcpy(&dest[write_pos_], &src[0], append_size * sizeof(src[0])); | 74 memcpy(&dest[write_pos_], &src[0], append_size * sizeof(src[0])); |
70 if (wrap_size > 0) { | 75 if (wrap_size > 0) { |
71 // Wrapping is needed: copy remaining part from the source to the FIFO. | 76 // Wrapping is needed: copy remaining part from the source to the FIFO. |
72 memcpy(&dest[0], &src[append_size], wrap_size * sizeof(src[0])); | 77 memcpy(&dest[0], &src[append_size], wrap_size * sizeof(src[0])); |
73 } | 78 } |
74 } | 79 } |
75 | 80 |
76 frames_ += source_size; | 81 // Ensure the data is *really* written before updating |frames_pushed_|. |
77 DCHECK_LE(frames_, max_frames()); | 82 MemoryBarrier(); |
83 | |
84 Atomic32 new_frames_pushed = frames_pushed_ + source_size; | |
scherkus (not reviewing)
2012/09/17 14:51:17
is the intent of this change to make this class lo
Chris Rogers
2012/09/17 20:44:23
Added section in description about thread-safety.
| |
85 NoBarrier_AtomicExchange(&frames_pushed_, new_frames_pushed); | |
jbauman
2012/09/15 00:33:52
This (and the next one) should be NoBarrier_Store,
Chris Rogers
2012/09/17 20:44:23
Done.
| |
86 | |
87 DCHECK_LE(frames(), max_frames()); | |
78 write_pos_ = UpdatePos(write_pos_, source_size, max_frames()); | 88 write_pos_ = UpdatePos(write_pos_, source_size, max_frames()); |
79 } | 89 } |
80 | 90 |
81 void AudioFifo::Consume(AudioBus* destination, | 91 void AudioFifo::Consume(AudioBus* destination, |
82 int start_frame, | 92 int start_frame, |
83 int frames_to_consume) { | 93 int frames_to_consume) { |
84 DCHECK(destination); | 94 DCHECK(destination); |
85 DCHECK_EQ(destination->channels(), audio_bus_->channels()); | 95 DCHECK_EQ(destination->channels(), audio_bus_->channels()); |
86 | 96 |
87 // It is not possible to ask for more data than what is available in the FIFO. | 97 // It is not possible to ask for more data than what is available in the FIFO. |
88 CHECK_LE(frames_to_consume, frames_); | 98 CHECK_LE(frames_to_consume, frames()); |
89 | 99 |
90 // A copy from the FIFO to |destination| will only be performed if the | 100 // A copy from the FIFO to |destination| will only be performed if the |
91 // allocated memory in |destination| is sufficient. | 101 // allocated memory in |destination| is sufficient. |
92 CHECK_LE(frames_to_consume + start_frame, destination->frames()); | 102 CHECK_LE(frames_to_consume + start_frame, destination->frames()); |
93 | 103 |
94 // Figure out if wrapping is needed and if so what segment sizes we need | 104 // Figure out if wrapping is needed and if so what segment sizes we need |
95 // when removing audio bus content from the FIFO. | 105 // when removing audio bus content from the FIFO. |
96 int consume_size = 0; | 106 int consume_size = 0; |
97 int wrap_size = 0; | 107 int wrap_size = 0; |
98 GetSizes(read_pos_, max_frames(), frames_to_consume, | 108 GetSizes(read_pos_, max_frames(), frames_to_consume, |
99 &consume_size, &wrap_size); | 109 &consume_size, &wrap_size); |
100 | 110 |
101 // For all channels, remove the requested amount of data from the FIFO | 111 // For all channels, remove the requested amount of data from the FIFO |
102 // and copy the content to the destination. Wrap around if needed. | 112 // and copy the content to the destination. Wrap around if needed. |
103 for (int ch = 0; ch < destination->channels(); ++ch) { | 113 for (int ch = 0; ch < destination->channels(); ++ch) { |
104 float* dest = destination->channel(ch); | 114 float* dest = destination->channel(ch); |
105 const float* src = audio_bus_->channel(ch); | 115 const float* src = audio_bus_->channel(ch); |
106 | 116 |
107 // Copy a selected part of the FIFO to the destination. | 117 // Copy a selected part of the FIFO to the destination. |
108 memcpy(&dest[start_frame], &src[read_pos_], consume_size * sizeof(src[0])); | 118 memcpy(&dest[start_frame], &src[read_pos_], consume_size * sizeof(src[0])); |
109 if (wrap_size > 0) { | 119 if (wrap_size > 0) { |
110 // Wrapping is needed: copy remaining part to the destination. | 120 // Wrapping is needed: copy remaining part to the destination. |
111 memcpy(&dest[consume_size + start_frame], &src[0], | 121 memcpy(&dest[consume_size + start_frame], &src[0], |
112 wrap_size * sizeof(src[0])); | 122 wrap_size * sizeof(src[0])); |
113 } | 123 } |
114 } | 124 } |
115 | 125 |
116 frames_ -= frames_to_consume; | 126 Atomic32 new_frames_consumed = frames_consumed_ + frames_to_consume; |
127 NoBarrier_AtomicExchange(&frames_consumed_, new_frames_consumed); | |
128 | |
117 read_pos_ = UpdatePos(read_pos_, frames_to_consume, max_frames()); | 129 read_pos_ = UpdatePos(read_pos_, frames_to_consume, max_frames()); |
118 } | 130 } |
119 | 131 |
120 void AudioFifo::Clear() { | 132 void AudioFifo::Clear() { |
no longer working on chromium
2012/09/17 20:47:46
when this Clear() is called? is there any potentia
Chris Rogers
2012/09/17 22:00:42
Yes there is, but I already mention in the comment
| |
121 frames_ = 0; | 133 frames_pushed_ = 0; |
134 frames_consumed_ = 0; | |
122 read_pos_ = 0; | 135 read_pos_ = 0; |
123 write_pos_ = 0; | 136 write_pos_ = 0; |
124 } | 137 } |
125 | 138 |
126 } // namespace media | 139 } // namespace media |
OLD | NEW |