Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(253)

Side by Side Diff: third_party/WebKit/Source/platform/audio/PushPullFIFO.cpp

Issue 2549093009: Introduce PushPullFIFO class and remove other FIFOs (Closed)
Patch Set: Comments and unit test Created 3 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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 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 DVLOG(1) << "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 }
81
82 // Pull the data out of FIFO to |outputBus|. If remaining frame in the FIFO
83 // is less than the frames to pull, provides remaining frame plus the silence.
84 void PushPullFIFO::pull(AudioBus* outputBus, size_t framesRequested) {
85 CHECK(outputBus);
86 SECURITY_CHECK(framesRequested <= outputBus->length());
87 SECURITY_CHECK(framesRequested <= m_fifoLength);
88 SECURITY_CHECK(m_indexRead < m_fifoLength);
89
90 const size_t remainder = m_fifoLength - m_indexRead;
91 const size_t framesToFill = std::min(m_framesAvailable, framesRequested);
92
93 for (unsigned i = 0; i < m_fifoBus->numberOfChannels(); ++i) {
94 const float* fifoBusChannel = m_fifoBus->channel(i)->data();
95 float* outputBusChannel = outputBus->channel(i)->mutableData();
96
97 // Fill up the output bus with the available frames first.
98 if (remainder >= framesToFill) {
99 // The remainder is big enough for the frames to pull.
100 memcpy(outputBusChannel, fifoBusChannel + m_indexRead,
101 framesToFill * sizeof(*fifoBusChannel));
102 } else {
103 // The frames to pull is bigger than the remainder size.
104 // Wrap around the index.
105 memcpy(outputBusChannel, fifoBusChannel + m_indexRead,
106 remainder * sizeof(*fifoBusChannel));
107 memcpy(outputBusChannel + remainder, fifoBusChannel,
108 (framesToFill - remainder) * sizeof(*fifoBusChannel));
109 }
110
111 // The frames available was not enough to fulfill the requested frames. Fill
112 // the rest of the channel with silence.
113 if (framesRequested > framesToFill) {
114 memset(outputBusChannel + framesToFill, 0,
115 (framesRequested - framesToFill) * sizeof(*outputBusChannel));
116 }
117 }
118
119 // Update the read index; wrap it around if necessary.
120 m_indexRead = (m_indexRead + framesToFill) % m_fifoLength;
121
122 // In case of underflow, move the |indexWrite| to the updated |indexRead|.
123 if (framesRequested > framesToFill) {
124 m_indexWrite = m_indexRead;
125 if (m_underflowCount++ < kMaxMessagesToLog) {
126 DVLOG(1) << "PushPullFIFO: underflow while pulling ("
127 << "underflowCount=" << m_underflowCount
128 << ", availableFrames=" << m_framesAvailable
129 << ", requestedFrames=" << framesRequested
130 << ", fifoLength=" << m_fifoLength << ")";
131 }
132 }
133
134 // Update the number of frames in FIFO.
135 m_framesAvailable -= framesToFill;
136 }
137
138 const PushPullFIFOStateForTest PushPullFIFO::getStateForTest() const {
139 return {length(), numberOfChannels(), framesAvailable(), m_indexRead,
140 m_indexWrite, m_overflowCount, m_underflowCount};
141 }
142
143 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698