OLD | NEW |
(Empty) | |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "platform/audio/PushPullFIFO.h" |
| 6 |
| 7 #include <memory> |
| 8 #include "platform/audio/AudioUtilities.h" |
| 9 #include "wtf/PtrUtil.h" |
| 10 |
| 11 namespace blink { |
| 12 |
| 13 namespace { |
| 14 |
| 15 // Suppress the warning log if over/underflow happens more than 100 times. |
| 16 const unsigned kMaxMessagesToLog = 100; |
| 17 } |
| 18 |
| 19 const size_t PushPullFIFO::kMaxFIFOLength = 65536; |
| 20 |
| 21 PushPullFIFO::PushPullFIFO(unsigned numberOfChannels, size_t fifoLength) |
| 22 : m_fifoLength(fifoLength), |
| 23 m_framesAvailable(0), |
| 24 m_indexRead(0), |
| 25 m_indexWrite(0), |
| 26 m_overflowCount(0), |
| 27 m_underflowCount(0) { |
| 28 CHECK_LE(m_fifoLength, kMaxFIFOLength); |
| 29 m_fifoBus = AudioBus::create(numberOfChannels, m_fifoLength); |
| 30 } |
| 31 |
| 32 PushPullFIFO::~PushPullFIFO() {} |
| 33 |
| 34 // Push the data from |inputBus| to FIFO. The size of push is determined by |
| 35 // the length of |inputBus|. |
| 36 void PushPullFIFO::push(const AudioBus* inputBus) { |
| 37 CHECK(inputBus); |
| 38 CHECK_EQ(inputBus->length(), AudioUtilities::kRenderQuantumFrames); |
| 39 SECURITY_CHECK(inputBus->length() <= m_fifoLength); |
| 40 SECURITY_CHECK(m_indexWrite < m_fifoLength); |
| 41 |
| 42 const size_t inputBusLength = inputBus->length(); |
| 43 const size_t remainder = m_fifoLength - m_indexWrite; |
| 44 |
| 45 for (unsigned i = 0; i < m_fifoBus->numberOfChannels(); ++i) { |
| 46 float* fifoBusChannel = m_fifoBus->channel(i)->mutableData(); |
| 47 const float* inputBusChannel = inputBus->channel(i)->data(); |
| 48 if (remainder >= inputBusLength) { |
| 49 // The remainder is big enough for the input data. |
| 50 memcpy(fifoBusChannel + m_indexWrite, inputBusChannel, |
| 51 inputBusLength * sizeof(*fifoBusChannel)); |
| 52 } else { |
| 53 // The input data overflows the remainder size. Wrap around the index. |
| 54 memcpy(fifoBusChannel + m_indexWrite, inputBusChannel, |
| 55 remainder * sizeof(*fifoBusChannel)); |
| 56 memcpy(fifoBusChannel, inputBusChannel + remainder, |
| 57 (inputBusLength - remainder) * sizeof(*fifoBusChannel)); |
| 58 } |
| 59 } |
| 60 |
| 61 // Update the write index; wrap it around if necessary. |
| 62 m_indexWrite = (m_indexWrite + inputBusLength) % m_fifoLength; |
| 63 |
| 64 // In case of overflow, move the |indexRead| to the updated |indexWrite| to |
| 65 // avoid reading overwritten frames by the next pull. |
| 66 if (inputBusLength > m_fifoLength - m_framesAvailable) { |
| 67 m_indexRead = m_indexWrite; |
| 68 if (++m_overflowCount < kMaxMessagesToLog) { |
| 69 LOG(WARNING) << "PushPullFIFO: overflow while pushing (" |
| 70 << "overflowCount=" << m_overflowCount |
| 71 << ", availableFrames=" << m_framesAvailable |
| 72 << ", inputFrames=" << inputBusLength |
| 73 << ", fifoLength=" << m_fifoLength << ")"; |
| 74 } |
| 75 } |
| 76 |
| 77 // Update the number of frames available in FIFO. |
| 78 m_framesAvailable = |
| 79 std::min(m_framesAvailable + inputBusLength, m_fifoLength); |
| 80 DCHECK_EQ((m_indexRead + m_framesAvailable) % m_fifoLength, m_indexWrite); |
| 81 } |
| 82 |
| 83 // Pull the data out of FIFO to |outputBus|. If remaining frame in the FIFO |
| 84 // is less than the frames to pull, provides remaining frame plus the silence. |
| 85 void PushPullFIFO::pull(AudioBus* outputBus, size_t framesRequested) { |
| 86 CHECK(outputBus); |
| 87 SECURITY_CHECK(framesRequested <= outputBus->length()); |
| 88 SECURITY_CHECK(framesRequested <= m_fifoLength); |
| 89 SECURITY_CHECK(m_indexRead < m_fifoLength); |
| 90 |
| 91 const size_t remainder = m_fifoLength - m_indexRead; |
| 92 const size_t framesToFill = std::min(m_framesAvailable, framesRequested); |
| 93 |
| 94 for (unsigned i = 0; i < m_fifoBus->numberOfChannels(); ++i) { |
| 95 const float* fifoBusChannel = m_fifoBus->channel(i)->data(); |
| 96 float* outputBusChannel = outputBus->channel(i)->mutableData(); |
| 97 |
| 98 // Fill up the output bus with the available frames first. |
| 99 if (remainder >= framesToFill) { |
| 100 // The remainder is big enough for the frames to pull. |
| 101 memcpy(outputBusChannel, fifoBusChannel + m_indexRead, |
| 102 framesToFill * sizeof(*fifoBusChannel)); |
| 103 } else { |
| 104 // The frames to pull is bigger than the remainder size. |
| 105 // Wrap around the index. |
| 106 memcpy(outputBusChannel, fifoBusChannel + m_indexRead, |
| 107 remainder * sizeof(*fifoBusChannel)); |
| 108 memcpy(outputBusChannel + remainder, fifoBusChannel, |
| 109 (framesToFill - remainder) * sizeof(*fifoBusChannel)); |
| 110 } |
| 111 |
| 112 // The frames available was not enough to fulfill the requested frames. Fill |
| 113 // the rest of the channel with silence. |
| 114 if (framesRequested > framesToFill) { |
| 115 memset(outputBusChannel + framesToFill, 0, |
| 116 (framesRequested - framesToFill) * sizeof(*outputBusChannel)); |
| 117 } |
| 118 } |
| 119 |
| 120 // Update the read index; wrap it around if necessary. |
| 121 m_indexRead = (m_indexRead + framesToFill) % m_fifoLength; |
| 122 |
| 123 // In case of underflow, move the |indexWrite| to the updated |indexRead|. |
| 124 if (framesRequested > framesToFill) { |
| 125 m_indexWrite = m_indexRead; |
| 126 if (m_underflowCount++ < kMaxMessagesToLog) { |
| 127 LOG(WARNING) << "PushPullFIFO: underflow while pulling (" |
| 128 << "underflowCount=" << m_underflowCount |
| 129 << ", availableFrames=" << m_framesAvailable |
| 130 << ", requestedFrames=" << framesRequested |
| 131 << ", fifoLength=" << m_fifoLength << ")"; |
| 132 } |
| 133 } |
| 134 |
| 135 // Update the number of frames in FIFO. |
| 136 m_framesAvailable -= framesToFill; |
| 137 DCHECK_EQ((m_indexRead + m_framesAvailable) % m_fifoLength, m_indexWrite); |
| 138 } |
| 139 |
| 140 const PushPullFIFOStateForTest PushPullFIFO::getStateForTest() const { |
| 141 return {length(), numberOfChannels(), framesAvailable(), m_indexRead, |
| 142 m_indexWrite, m_overflowCount, m_underflowCount}; |
| 143 } |
| 144 |
| 145 } // namespace blink |
OLD | NEW |