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