Index: third_party/WebKit/Source/platform/audio/PushPullFIFO.cpp |
diff --git a/third_party/WebKit/Source/platform/audio/PushPullFIFO.cpp b/third_party/WebKit/Source/platform/audio/PushPullFIFO.cpp |
new file mode 100644 |
index 0000000000000000000000000000000000000000..f1da5606e41f36148df356a4036bc5f1c0a343fb |
--- /dev/null |
+++ b/third_party/WebKit/Source/platform/audio/PushPullFIFO.cpp |
@@ -0,0 +1,104 @@ |
+// Copyright 2016 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "platform/audio/PushPullFIFO.h" |
+ |
+#include "wtf/PtrUtil.h" |
+#include <memory> |
+ |
+namespace blink { |
+ |
+PushPullFIFO::PushPullFIFO(unsigned numberOfChannels, size_t length) |
+ : m_dataBus(AudioBus::create(numberOfChannels, length)), |
+ m_length(length), |
+ m_numberOfFrames(0), |
+ m_indexRead(0), |
+ m_indexWrite(0) {} |
+ |
+PushPullFIFO::~PushPullFIFO() {} |
+ |
+// Push the data from |inputBus| to FIFO. The size of push is determined by |
+// the length of |inputBus|. |
+void PushPullFIFO::push(const AudioBus* inputBus) { |
+ if (!inputBus) { |
+ NOTREACHED(); |
o1ka
2017/01/09 14:09:03
Why not just DCHECK(inputBus)?
hongchan
2017/01/09 21:53:16
Done. The early return is for the release build.
o1ka
2017/01/10 10:07:19
See my other comment about early returns
|
+ return; |
+ } |
+ |
+ size_t remainder = m_length - m_indexWrite; |
+ size_t inputBusLength = inputBus->length(); |
+ |
+ if (remainder > m_length || m_indexWrite > m_length || |
+ inputBusLength > m_length || |
+ inputBusLength + m_numberOfFrames > m_length) { |
Raymond Toy
2017/01/06 22:22:03
Need a check if number of channels for the bus mat
o1ka
2017/01/09 14:09:03
Same comment about naming: m_length, inputBusLengt
hongchan
2017/01/09 21:53:16
@rtoy
Hmm. My code is based on the current FIFO d
o1ka
2017/01/10 10:07:19
Sorry for being unclear, my comment was about vari
hongchan
2017/01/10 21:15:58
Done.
|
+ NOTREACHED(); |
o1ka
2017/01/09 14:09:03
How will we debug silent failures like this?
Shoul
hongchan
2017/01/09 21:53:16
This is a more of WebAudio's practice from the old
o1ka
2017/01/10 10:07:19
See my other comment on this.
|
+ return; |
+ } |
Raymond Toy
2017/01/06 22:22:03
Can we add DCHECKs for these too? While it's nice
hongchan
2017/01/09 21:53:16
I think these checks are being overly careful abou
Raymond Toy
2017/01/09 22:46:02
Ok. I'll leave it up to you to decide which checks
hongchan
2017/01/10 21:15:58
Done.
|
+ |
+ for (unsigned i = 0; i < m_dataBus->numberOfChannels(); ++i) { |
o1ka
2017/01/09 14:09:03
Can we name it something like "m_fifoBus" to go al
hongchan
2017/01/09 21:53:16
Done. Changed it to fifoDataBus.
|
+ float* fifoChannel = m_dataBus->channel(i)->mutableData(); |
o1ka
2017/01/09 14:09:03
Can we name int fifoBusChannel to match inputBusCh
hongchan
2017/01/09 21:53:16
Done.
|
+ const float* inputBusChannel = inputBus->channel(i)->data(); |
+ if (remainder >= inputBusLength) { |
+ // The remainder is big enough for the input data. |
+ memcpy(fifoChannel + m_indexWrite, inputBusChannel, |
+ inputBusLength * sizeof(*fifoChannel)); |
+ } else { |
+ // The input data overflows the remainder size. Wrap around the index. |
+ memcpy(fifoChannel + m_indexWrite, inputBusChannel, |
+ remainder * sizeof(*fifoChannel)); |
+ memcpy(fifoChannel, inputBusChannel + remainder, |
o1ka
2017/01/09 14:09:03
What if you overwrite the beginning of data writte
hongchan
2017/01/09 21:53:16
Yes, this is something I wanted to discuss in this
o1ka
2017/01/10 10:07:19
You added a check for that, right?
|
+ (inputBusLength - remainder) * sizeof(*fifoChannel)); |
+ } |
+ } |
+ |
+ // Accumulate the valid number of frames in FIFO. |
+ m_numberOfFrames += inputBusLength; |
o1ka
2017/01/09 14:09:03
What does |m_numberOfFrames| represent? Can it be
hongchan
2017/01/09 21:53:16
It represents 'meaningful frames' that have not be
o1ka
2017/01/10 10:07:19
|framesAvailable| maybe?
hongchan
2017/01/10 21:15:58
Done.
|
+ DCHECK_LE(m_numberOfFrames, m_length); |
+ |
+ // Update the write index; wrap it around if necessary. |
+ m_indexWrite = (m_indexWrite + inputBusLength) % m_length; |
Raymond Toy
2017/01/06 22:22:03
It would be nice if m_length were a power of two.
hongchan
2017/01/09 21:53:16
What's the benefit of the FIFO length being power
Raymond Toy
2017/01/09 22:46:02
Modulo would just be a mask instead of a division.
|
+} |
+ |
+// Pull the data out of FIFO to |outputBus|. |
Raymond Toy
2017/01/06 22:22:03
Specify what happens if outputBus is longer than f
hongchan
2017/01/09 21:53:16
Done.
|
+void PushPullFIFO::pull(AudioBus* outputBus, size_t framesToPull) { |
+ if (!outputBus) { |
+ NOTREACHED(); |
o1ka
2017/01/09 14:09:03
Same question as above for all NOTREACHED().
hongchan
2017/01/09 21:53:16
Done.
|
+ return; |
+ } |
+ |
+ size_t remainder = m_length - m_indexRead; |
+ |
o1ka
2017/01/09 14:09:03
DCHECK(remainder >= 0)?
hongchan
2017/01/09 21:53:15
Done.
Raymond Toy
2017/01/09 22:46:02
That seems wrong. remainder is a size_t so it's a
o1ka
2017/01/10 10:07:19
Right, we should have CHECKs before subtraction in
|
+ if (remainder > m_length || m_indexRead > m_length || |
+ framesToPull > m_length || framesToPull > m_numberOfFrames || |
+ outputBus->length() > framesToPull) { |
+ NOTREACHED(); |
+ return; |
+ } |
+ |
+ for (unsigned i = 0; i < m_dataBus->numberOfChannels(); ++i) { |
+ const float* fifoChannel = m_dataBus->channel(i)->data(); |
+ float* outputBusChannel = outputBus->channel(i)->mutableData(); |
+ if (remainder >= framesToPull) { |
+ // The remainder is big enough for the frames to pull. |
+ memcpy(outputBusChannel, fifoChannel + m_indexRead, |
+ framesToPull * sizeof(*fifoChannel)); |
+ } else { |
+ // The frames to pull is bigger than the remainder size. |
+ // Wrap around the index. |
+ memcpy(outputBusChannel, fifoChannel + m_indexWrite, |
Raymond Toy
2017/01/06 22:22:03
You mean indexRead, not indexWrite? It would be g
hongchan
2017/01/09 21:53:16
Done. I am planning for a unit test for the FIFO.
|
+ remainder * sizeof(*fifoChannel)); |
+ memcpy(outputBusChannel + remainder, fifoChannel, |
+ (framesToPull - remainder) * sizeof(*fifoChannel)); |
+ } |
+ } |
+ |
+ // Update the read index; wrap it around if necessary. |
+ m_indexRead = (m_indexRead + framesToPull) % m_length; |
+ |
+ // Subtract the valid number of frames in FIFO. |
+ DCHECK_GE(m_numberOfFrames, framesToPull); |
+ m_numberOfFrames -= framesToPull; |
+} |
+ |
+} // namespace blink |