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

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

Issue 2549093009: Introduce PushPullFIFO class and remove other FIFOs (Closed)
Patch Set: More comments Created 3 years, 11 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(static_cast<int>(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 int inputBusLength = static_cast<int>(inputBus->length());
o1ka 2017/01/24 11:09:02 unsafe
hongchan 2017/01/26 22:33:18 Done.
34 const int 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 (m_fifoLength < inputBusLength + m_framesAvailable) {
59 m_indexRead = m_indexWrite;
60 if (++m_overflowCount < kLogCapOverUnderflow) {
61 LOG(WARNING) << "PushPullFIFO::pushing overflown. "
62 << "(count = " << m_overflowCount << ") \n"
63 << "in-FIFO frames = " << m_framesAvailable
64 << ", input frames = " << inputBusLength
65 << ", FIFO size = " << m_fifoLength;
66 }
67 }
68
69 // Update the number of frames available in FIFO.
70 m_framesAvailable =
71 std::min(m_fifoLength, m_framesAvailable + inputBusLength);
72 }
73
74 // Pull the data out of FIFO to |outputBus|. If the remaining frames in the FIFO
75 // is less than the frames to pull, provides the remaining frames plus the
76 // silence.
77 void PushPullFIFO::pull(AudioBus* outputBus, size_t framesRequested) {
78 DCHECK(outputBus);
79 SECURITY_CHECK(framesRequested <= outputBus->length());
80 if (!outputBus)
81 return;
82
83 // TODO(hongchan): add a mutex here for this operation when two threads are
84 // introduced. Pulling operation has a higher priority to ensure the audio
85 // device callback to be uninterrupted.
86
87 const int remainder = m_fifoLength - m_indexRead;
88 const int framesToProvide = static_cast<int>(framesRequested);
o1ka 2017/01/24 11:09:02 unsafe
hongchan 2017/01/26 22:33:18 Done.
89 SECURITY_CHECK(framesToProvide < m_fifoLength);
90 SECURITY_CHECK(m_indexRead < m_fifoLength);
91
92 const int framesToFill = std::min(m_framesAvailable, framesToProvide);
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 (framesToProvide > framesToFill) {
115 memset(outputBusChannel + framesToFill, 0,
116 (framesToProvide - 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 write index to the updated read index.
124 if (framesToProvide > framesToFill) {
125 m_indexWrite = m_indexRead;
126 if (m_underflowCount++ < kLogCapOverUnderflow) {
127 LOG(WARNING) << "PushPullFIFO::pulling underflown. "
128 << "(count = " << m_underflowCount << ") \n"
129 << "in-FIFO frames = " << m_framesAvailable
130 << ", requested frames = " << framesToProvide
131 << ", FIFO size = " << m_fifoLength << ")";
132 }
133 }
134
135 // Update the number of frames in FIFO.
136 m_framesAvailable = std::max(0, m_framesAvailable - framesToFill);
137 }
138
139 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698