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

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

Issue 2549093009: Introduce PushPullFIFO class and remove other FIFOs (Closed)
Patch Set: Introduced new streamline 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 2016 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 "wtf/PtrUtil.h"
8 #include <memory>
9
10 namespace blink {
11
12 // Suppress the warning log if over/underflow happens more than 100 times.
13 const int kLogCapOverUnderflow = 100;
14
15 PushPullFIFO::PushPullFIFO(unsigned numberOfChannels, size_t fifoLength)
16 : m_fifoBus(AudioBus::create(numberOfChannels, 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
24 PushPullFIFO::~PushPullFIFO() {}
25
26 // Push the data from |inputBus| to FIFO. The size of push is determined by
27 // the length of |inputBus|.
28 void PushPullFIFO::push(const AudioBus* inputBus) {
29 DCHECK(inputBus);
30 if (!inputBus)
31 return;
32
33 const size_t inputBusLength = inputBus->length();
34 const size_t remainder = m_fifoLength - m_indexWrite;
35 SECURITY_CHECK(inputBusLength <= m_fifoLength);
36 SECURITY_CHECK(m_indexWrite < m_fifoLength);
37
38 for (unsigned i = 0; i < m_fifoBus->numberOfChannels(); ++i) {
39 float* fifoBusChannel = m_fifoBus->channel(i)->mutableData();
40 const float* inputBusChannel = inputBus->channel(i)->data();
41 if (remainder >= inputBusLength) {
42 // The remainder is big enough for the input data.
43 memcpy(fifoBusChannel + m_indexWrite, inputBusChannel,
44 inputBusLength * sizeof(*fifoBusChannel));
45 } else {
46 // The input data overflows the remainder size. Wrap around the index.
47 memcpy(fifoBusChannel + m_indexWrite, inputBusChannel,
48 remainder * sizeof(*fifoBusChannel));
49 memcpy(fifoBusChannel, inputBusChannel + remainder,
50 (inputBusLength - remainder) * sizeof(*fifoBusChannel));
51 }
52 }
53
54 // Update the write index; wrap it around if necessary.
55 m_indexWrite = (m_indexWrite + inputBusLength) % m_fifoLength;
56
57 // In case of overflow, move the read index to the updated write index.
58 if (inputBusLength > m_fifoLength - m_framesAvailable) {
59 m_indexRead = m_indexWrite;
60 if (++m_overflowCount < kLogCapOverUnderflow) {
61 LOG(WARNING) << "PushPullFIFO::overflow while pushing ("
Raymond Toy 2017/01/27 17:39:44 Grammar: "PushPullFIFO: overflow..." The message
hongchan 2017/01/27 22:39:14 Done.
62 << "overflowCount=" << m_overflowCount
63 << ", availalbeFrames=" << m_framesAvailable
Raymond Toy 2017/01/27 17:39:43 Typo.
hongchan 2017/01/27 22:39:14 Done.
64 << ", inputFrames=" << inputBusLength
65 << ", fifoLength=" << m_fifoLength << ")";
66 }
67 }
68
69 // Update the number of frames available in FIFO.
70 m_framesAvailable += inputBusLength;
o1ka 2017/01/27 13:47:54 m_framesAvailable cannot be more than m_fifoLength
hongchan 2017/01/27 22:39:13 You're right. I am reverting it with std::min().
71 }
72
73 // Pull the data out of FIFO to |outputBus|. If the remaining frames in the FIFO
74 // is less than the frames to pull, provides the remaining frames plus the
75 // silence.
76 void PushPullFIFO::pull(AudioBus* outputBus, size_t framesRequested) {
77 DCHECK(outputBus);
78 if (!outputBus)
79 return;
o1ka 2017/01/27 13:47:54 Can we avoid silent failures by using CHECK()?
Raymond Toy 2017/01/27 17:39:44 What is the intent here? Because of the checks be
hongchan 2017/01/27 22:39:13 Yes. I was simply following WebAudio's convention
80
81 // TODO(hongchan): add a tryLock here for this operation when two threads are
82 // introduced. Pulling operation has a higher priority to ensure the audio
83 // device callback to be uninterrupted.
84
85 const size_t remainder = m_fifoLength - m_indexRead;
86 const size_t framesToFill = std::min(m_framesAvailable, framesRequested);
87 SECURITY_CHECK(framesRequested <= outputBus->length());
88 SECURITY_CHECK(framesRequested <= m_fifoLength);
89 SECURITY_CHECK(m_indexRead < m_fifoLength);
Raymond Toy 2017/01/27 17:39:44 Move these to the beginning of the function.
hongchan 2017/01/27 22:39:14 Done.
90
91 for (unsigned i = 0; i < m_fifoBus->numberOfChannels(); ++i) {
92 const float* fifoBusChannel = m_fifoBus->channel(i)->data();
93 float* outputBusChannel = outputBus->channel(i)->mutableData();
94
95 // Fill up the output bus with the available frames first.
96 if (remainder >= framesToFill) {
97 // The remainder is big enough for the frames to pull.
98 memcpy(outputBusChannel, fifoBusChannel + m_indexRead,
99 framesToFill * sizeof(*fifoBusChannel));
o1ka 2017/01/27 13:47:54 What if framesToFill * sizeof(*fifoBusChannel) ove
hongchan 2017/01/27 22:39:14 With the newly introduced max FIFO length, the ove
100 } else {
101 // The frames to pull is bigger than the remainder size.
102 // Wrap around the index.
103 memcpy(outputBusChannel, fifoBusChannel + m_indexRead,
104 remainder * sizeof(*fifoBusChannel));
105 memcpy(outputBusChannel + remainder, fifoBusChannel,
106 (framesToFill - remainder) * sizeof(*fifoBusChannel));
107 }
108
109 // The frames available was not enough to fulfill the requested frames. Fill
110 // the rest of the channel with silence.
111 if (framesRequested > framesToFill) {
112 memset(outputBusChannel + framesToFill, 0,
113 (framesRequested - framesToFill) * sizeof(*outputBusChannel));
114 }
115 }
116
117 // Update the read index; wrap it around if necessary.
118 m_indexRead = (m_indexRead + framesToFill) % m_fifoLength;
o1ka 2017/01/27 13:47:54 What is addition overflows?
hongchan 2017/01/27 22:39:13 With the newly introduced max FIFO length, the ove
119
120 // In case of underflow, move the write index to the updated read index.
121 if (framesRequested > framesToFill) {
122 m_indexWrite = m_indexRead;
123 if (m_underflowCount++ < kLogCapOverUnderflow) {
124 LOG(WARNING) << "PushPullFIFO::underflow while pulling ("
Raymond Toy 2017/01/27 17:39:44 Grammar: "PushPullFIFO: overflow while...". Looks
hongchan 2017/01/27 22:39:13 Done.
125 << "underflowCount=" << m_underflowCount
126 << ", availableFrames=" << m_framesAvailable
127 << ", requestedFrames=" << framesRequested
128 << ", fifoLength=" << m_fifoLength << ")";
129 }
130 }
131
132 // Update the number of frames in FIFO.
133 m_framesAvailable -= framesToFill;
134 }
135
136 const PushPullFIFOStateForTest PushPullFIFO::getStateForTest() const {
137 return {length(), numberOfChannels(), framesAvailable(), m_indexRead,
o1ka 2017/01/27 13:47:54 Does this spacing comply with style guide?
hongchan 2017/01/27 22:39:14 I checked twice; this is the result of |git cl for
138 m_indexWrite, m_overflowCount, m_underflowCount};
139 }
140
141 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698