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..3ec86e1733d6a713ef94ee694d8d4943e9f016a8 |
--- /dev/null |
+++ b/third_party/WebKit/Source/platform/audio/PushPullFIFO.cpp |
@@ -0,0 +1,139 @@ |
+// 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 { |
+ |
+// Suppress the warning log if over/underflow happens more than 100 times. |
+const int kLogCapOverUnderflow = 100; |
+ |
+PushPullFIFO::PushPullFIFO(unsigned numberOfChannels, size_t fifoLength) |
+ : m_fifoBus(AudioBus::create(numberOfChannels, fifoLength)), |
+ m_fifoLength(static_cast<int>(fifoLength)), |
+ m_framesAvailable(0), |
+ m_indexRead(0), |
+ m_indexWrite(0), |
+ m_overflowCount(0), |
+ m_underflowCount(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) { |
+ DCHECK(inputBus); |
+ if (!inputBus) |
+ return; |
+ |
+ const int inputBusLength = static_cast<int>(inputBus->length()); |
o1ka
2017/01/24 11:09:02
unsafe
hongchan
2017/01/26 22:33:18
Done.
|
+ const int remainder = m_fifoLength - m_indexWrite; |
+ SECURITY_CHECK(inputBusLength < m_fifoLength); |
+ SECURITY_CHECK(m_indexWrite < m_fifoLength); |
+ |
+ for (unsigned i = 0; i < m_fifoBus->numberOfChannels(); ++i) { |
+ float* fifoBusChannel = m_fifoBus->channel(i)->mutableData(); |
+ const float* inputBusChannel = inputBus->channel(i)->data(); |
+ if (remainder >= inputBusLength) { |
+ // The remainder is big enough for the input data. |
+ memcpy(fifoBusChannel + m_indexWrite, inputBusChannel, |
+ inputBusLength * sizeof(*fifoBusChannel)); |
+ } else { |
+ // The input data overflows the remainder size. Wrap around the index. |
+ memcpy(fifoBusChannel + m_indexWrite, inputBusChannel, |
+ remainder * sizeof(*fifoBusChannel)); |
+ memcpy(fifoBusChannel, inputBusChannel + remainder, |
+ (inputBusLength - remainder) * sizeof(*fifoBusChannel)); |
+ } |
+ } |
+ |
+ // Update the write index; wrap it around if necessary. |
+ m_indexWrite = (m_indexWrite + inputBusLength) % m_fifoLength; |
+ |
+ // In case of overflow, move the read index to the updated write index. |
+ if (m_fifoLength < inputBusLength + m_framesAvailable) { |
+ m_indexRead = m_indexWrite; |
+ if (++m_overflowCount < kLogCapOverUnderflow) { |
+ LOG(WARNING) << "PushPullFIFO::pushing overflown. " |
+ << "(count = " << m_overflowCount << ") \n" |
+ << "in-FIFO frames = " << m_framesAvailable |
+ << ", input frames = " << inputBusLength |
+ << ", FIFO size = " << m_fifoLength; |
+ } |
+ } |
+ |
+ // Update the number of frames available in FIFO. |
+ m_framesAvailable = |
+ std::min(m_fifoLength, m_framesAvailable + inputBusLength); |
+} |
+ |
+// Pull the data out of FIFO to |outputBus|. If the remaining frames in the FIFO |
+// is less than the frames to pull, provides the remaining frames plus the |
+// silence. |
+void PushPullFIFO::pull(AudioBus* outputBus, size_t framesRequested) { |
+ DCHECK(outputBus); |
+ SECURITY_CHECK(framesRequested <= outputBus->length()); |
+ if (!outputBus) |
+ return; |
+ |
+ // TODO(hongchan): add a mutex here for this operation when two threads are |
+ // introduced. Pulling operation has a higher priority to ensure the audio |
+ // device callback to be uninterrupted. |
+ |
+ const int remainder = m_fifoLength - m_indexRead; |
+ const int framesToProvide = static_cast<int>(framesRequested); |
o1ka
2017/01/24 11:09:02
unsafe
hongchan
2017/01/26 22:33:18
Done.
|
+ SECURITY_CHECK(framesToProvide < m_fifoLength); |
+ SECURITY_CHECK(m_indexRead < m_fifoLength); |
+ |
+ const int framesToFill = std::min(m_framesAvailable, framesToProvide); |
+ |
+ for (unsigned i = 0; i < m_fifoBus->numberOfChannels(); ++i) { |
+ const float* fifoBusChannel = m_fifoBus->channel(i)->data(); |
+ float* outputBusChannel = outputBus->channel(i)->mutableData(); |
+ |
+ // Fill up the output bus with the available frames first. |
+ if (remainder >= framesToFill) { |
+ // The remainder is big enough for the frames to pull. |
+ memcpy(outputBusChannel, fifoBusChannel + m_indexRead, |
+ framesToFill * sizeof(*fifoBusChannel)); |
+ } else { |
+ // The frames to pull is bigger than the remainder size. |
+ // Wrap around the index. |
+ memcpy(outputBusChannel, fifoBusChannel + m_indexRead, |
+ remainder * sizeof(*fifoBusChannel)); |
+ memcpy(outputBusChannel + remainder, fifoBusChannel, |
+ (framesToFill - remainder) * sizeof(*fifoBusChannel)); |
+ } |
+ |
+ // The frames available was not enough to fulfill the requested frames. Fill |
+ // the rest of the channel with silence. |
+ if (framesToProvide > framesToFill) { |
+ memset(outputBusChannel + framesToFill, 0, |
+ (framesToProvide - framesToFill) * sizeof(*outputBusChannel)); |
+ } |
+ } |
+ |
+ // Update the read index; wrap it around if necessary. |
+ m_indexRead = (m_indexRead + framesToFill) % m_fifoLength; |
+ |
+ // In case of underflow, move the write index to the updated read index. |
+ if (framesToProvide > framesToFill) { |
+ m_indexWrite = m_indexRead; |
+ if (m_underflowCount++ < kLogCapOverUnderflow) { |
+ LOG(WARNING) << "PushPullFIFO::pulling underflown. " |
+ << "(count = " << m_underflowCount << ") \n" |
+ << "in-FIFO frames = " << m_framesAvailable |
+ << ", requested frames = " << framesToProvide |
+ << ", FIFO size = " << m_fifoLength << ")"; |
+ } |
+ } |
+ |
+ // Update the number of frames in FIFO. |
+ m_framesAvailable = std::max(0, m_framesAvailable - framesToFill); |
+} |
+ |
+} // namespace blink |