Chromium Code Reviews| 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..c54f38c883c7084a1ec97a74112ef4bdafade868 |
| --- /dev/null |
| +++ b/third_party/WebKit/Source/platform/audio/PushPullFIFO.cpp |
| @@ -0,0 +1,138 @@ |
| +// Copyright 2017 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 "platform/audio/AudioUtilities.h" |
| +#include "wtf/PtrUtil.h" |
| +#include <memory> |
| + |
| +namespace blink { |
| + |
| +// Suppress the warning log if over/underflow happens more than 100 times. |
| +const unsigned kMaxMessagesToLog = 100; |
|
o1ka
2017/02/02 16:30:24
Put it into an unnamed namespace?
hongchan
2017/02/02 19:55:23
Done.
|
| + |
| +PushPullFIFO::PushPullFIFO(unsigned numberOfChannels, size_t fifoLength) |
| + : m_fifoLength(fifoLength), |
| + m_framesAvailable(0), |
| + m_indexRead(0), |
| + m_indexWrite(0), |
| + m_overflowCount(0), |
| + m_underflowCount(0) { |
| + CHECK_LE(m_fifoLength, kMaxFIFOLength); |
| + m_fifoBus = AudioBus::create(numberOfChannels, m_fifoLength); |
| +} |
| + |
| +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) { |
| + CHECK(inputBus); |
| + CHECK_EQ(inputBus->length(), AudioUtilities::kRenderQuantumFrames); |
| + SECURITY_CHECK(inputBus->length() <= m_fifoLength); |
| + SECURITY_CHECK(m_indexWrite < m_fifoLength); |
| + |
| + const size_t inputBusLength = inputBus->length(); |
| + const size_t remainder = m_fifoLength - m_indexWrite; |
| + |
| + 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 |indexRead| to the updated |indexWrite| to |
| + // avoid reading overwritten frames by the next pull. |
| + if (inputBusLength > m_fifoLength - m_framesAvailable) { |
| + m_indexRead = m_indexWrite; |
| + if (++m_overflowCount < kMaxMessagesToLog) { |
| + DVLOG(1) << "PushPullFIFO: overflow while pushing (" |
| + << "overflowCount=" << m_overflowCount |
| + << ", availableFrames=" << m_framesAvailable |
| + << ", inputFrames=" << inputBusLength |
| + << ", fifoLength=" << m_fifoLength << ")"; |
| + } |
| + } |
| + |
| + // Update the number of frames available in FIFO. |
| + m_framesAvailable = |
| + std::min(m_framesAvailable + inputBusLength, m_fifoLength); |
|
o1ka
2017/02/02 16:30:24
DCHECK that relations between m_indexRead, m_index
hongchan
2017/02/02 19:55:23
This doesn't seem to possible because of the heuri
o1ka
2017/02/03 10:41:17
How about DCHECK_EQ((m_indexRead + m_framesAvailab
hongchan
2017/02/03 17:20:11
Well, I thought you wanted the verification purely
|
| +} |
| + |
| +// Pull the data out of FIFO to |outputBus|. If remaining frame in the FIFO |
| +// is less than the frames to pull, provides remaining frame plus the silence. |
| +void PushPullFIFO::pull(AudioBus* outputBus, size_t framesRequested) { |
| + CHECK(outputBus); |
| + SECURITY_CHECK(framesRequested <= outputBus->length()); |
| + SECURITY_CHECK(framesRequested <= m_fifoLength); |
| + SECURITY_CHECK(m_indexRead < m_fifoLength); |
| + |
| + const size_t remainder = m_fifoLength - m_indexRead; |
| + const size_t framesToFill = std::min(m_framesAvailable, framesRequested); |
| + |
| + 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 (framesRequested > framesToFill) { |
| + memset(outputBusChannel + framesToFill, 0, |
| + (framesRequested - 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 |indexWrite| to the updated |indexRead|. |
| + if (framesRequested > framesToFill) { |
| + m_indexWrite = m_indexRead; |
| + if (m_underflowCount++ < kMaxMessagesToLog) { |
| + DVLOG(1) << "PushPullFIFO: underflow while pulling (" |
| + << "underflowCount=" << m_underflowCount |
| + << ", availableFrames=" << m_framesAvailable |
| + << ", requestedFrames=" << framesRequested |
| + << ", fifoLength=" << m_fifoLength << ")"; |
| + } |
| + } |
| + |
| + // Update the number of frames in FIFO. |
| + m_framesAvailable -= framesToFill; |
|
o1ka
2017/02/02 16:30:24
Same here: DCHECK the relations
hongchan
2017/02/02 19:55:23
Ditto.
o1ka
2017/02/03 10:41:17
Ditto :)
hongchan
2017/02/03 17:20:12
Done.
|
| +} |
| + |
| +const PushPullFIFOStateForTest PushPullFIFO::getStateForTest() const { |
| + return {length(), numberOfChannels(), framesAvailable(), m_indexRead, |
| + m_indexWrite, m_overflowCount, m_underflowCount}; |
| +} |
| + |
| +} // namespace blink |