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 "wtf/PtrUtil.h" | |
8 #include <memory> | |
9 | |
10 namespace blink { | |
11 | |
12 // Maximum FIFO length. | |
13 const size_t kMaxFIFOLength = 32768; | |
hongchan
2017/01/27 22:39:14
rtoy@ and I believe 32K is good enough based on th
Raymond Toy
2017/02/01 19:02:37
I checked the metrics again. There is one case of
hongchan
2017/02/01 23:09:06
Should FIFO aware of sample rate? Is that what we
Raymond Toy
2017/02/01 23:38:55
Well, I was just thinking it needs to be large eno
hongchan
2017/02/02 19:55:23
Acknowledged.
| |
14 | |
15 // Suppress the warning log if over/underflow happens more than 100 times. | |
16 const unsigned kLogCapOverUnderflow = 100; | |
Raymond Toy
2017/01/30 20:59:28
Maybe kMaxMessagesToLog?
hongchan
2017/02/01 18:07:27
Done.
| |
17 | |
18 PushPullFIFO::PushPullFIFO(unsigned numberOfChannels, size_t fifoLength) | |
19 : m_fifoLength(std::min(fifoLength, kMaxFIFOLength)), | |
20 m_fifoBus(AudioBus::create(numberOfChannels, m_fifoLength)), | |
21 m_framesAvailable(0), | |
22 m_indexRead(0), | |
23 m_indexWrite(0), | |
24 m_overflowCount(0), | |
25 m_underflowCount(0) {} | |
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 SECURITY_CHECK(inputBus->length() <= m_fifoLength); | |
34 SECURITY_CHECK(m_indexWrite < m_fifoLength); | |
Raymond Toy
2017/01/30 20:59:28
You said the push size is 128. Shouldn't there be
hongchan
2017/02/01 18:07:27
Done.
| |
35 | |
36 const size_t inputBusLength = inputBus->length(); | |
37 const size_t remainder = m_fifoLength - m_indexWrite; | |
38 | |
39 for (unsigned i = 0; i < m_fifoBus->numberOfChannels(); ++i) { | |
40 float* fifoBusChannel = m_fifoBus->channel(i)->mutableData(); | |
41 const float* inputBusChannel = inputBus->channel(i)->data(); | |
42 if (remainder >= inputBusLength) { | |
43 // The remainder is big enough for the input data. | |
44 memcpy(fifoBusChannel + m_indexWrite, inputBusChannel, | |
45 inputBusLength * sizeof(*fifoBusChannel)); | |
46 } else { | |
47 // The input data overflows the remainder size. Wrap around the index. | |
48 memcpy(fifoBusChannel + m_indexWrite, inputBusChannel, | |
49 remainder * sizeof(*fifoBusChannel)); | |
50 memcpy(fifoBusChannel, inputBusChannel + remainder, | |
51 (inputBusLength - remainder) * sizeof(*fifoBusChannel)); | |
52 } | |
53 } | |
54 | |
55 // Update the write index; wrap it around if necessary. | |
56 m_indexWrite = (m_indexWrite + inputBusLength) % m_fifoLength; | |
57 | |
58 // In case of overflow, move the read index to the updated write index. | |
59 if (inputBusLength > m_fifoLength - m_framesAvailable) { | |
60 m_indexRead = m_indexWrite; | |
61 if (++m_overflowCount < kLogCapOverUnderflow) { | |
62 DVLOG(1) << "PushPullFIFO: overflow while pushing (" | |
63 << "overflowCount=" << m_overflowCount | |
64 << ", availableFrames=" << m_framesAvailable | |
65 << ", inputFrames=" << inputBusLength | |
66 << ", fifoLength=" << m_fifoLength << ")"; | |
67 } | |
68 } | |
69 | |
70 // Update the number of frames available in FIFO. | |
71 m_framesAvailable = | |
72 std::min(m_framesAvailable + inputBusLength, m_fifoLength); | |
73 } | |
74 | |
75 // Pull the data out of FIFO to |outputBus|. If the remaining frames in the FIFO | |
76 // is less than the frames to pull, provides the remaining frames plus the | |
Raymond Toy
2017/01/30 20:59:28
s/the//
hongchan
2017/02/01 18:07:27
Done.
| |
77 // silence. | |
78 void PushPullFIFO::pull(AudioBus* outputBus, size_t framesRequested) { | |
79 CHECK(outputBus); | |
80 SECURITY_CHECK(framesRequested <= outputBus->length()); | |
Raymond Toy
2017/01/30 20:59:28
You need to add this to your contract.
hongchan
2017/02/01 18:07:27
I can certainly add, but this is more of sanity ch
Raymond Toy
2017/02/01 19:02:38
Well, you could have defined pull to use the min o
hongchan
2017/02/01 23:09:06
// - If |framesRequested| is bigger than the leng
| |
81 SECURITY_CHECK(framesRequested <= m_fifoLength); | |
Raymond Toy
2017/01/30 20:59:28
This too.
Raymond Toy
2017/02/01 19:02:38
I can imagine that if you request more than the fi
hongchan
2017/02/01 23:09:06
// - If |framesRequested| is bigger than FIFO len
| |
82 SECURITY_CHECK(m_indexRead < m_fifoLength); | |
83 | |
84 // TODO(hongchan): add a tryLock here for this operation when two threads are | |
85 // introduced. Pulling operation has a higher priority to ensure the audio | |
86 // device callback to be uninterrupted. | |
87 | |
88 const size_t remainder = m_fifoLength - m_indexRead; | |
89 const size_t framesToFill = std::min(m_framesAvailable, framesRequested); | |
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)); | |
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; | |
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 DVLOG(1) << "PushPullFIFO: underflow while pulling (" | |
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 = | |
134 m_framesAvailable > framesToFill ? m_framesAvailable - framesToFill : 0; | |
Raymond Toy
2017/01/30 20:59:28
Doesn't line 89 guarantees the m_framesAvailable >
hongchan
2017/02/01 18:07:27
Done.
| |
135 } | |
136 | |
137 const PushPullFIFOStateForTest PushPullFIFO::getStateForTest() const { | |
138 return {length(), numberOfChannels(), framesAvailable(), m_indexRead, | |
139 m_indexWrite, m_overflowCount, m_underflowCount}; | |
140 } | |
141 | |
142 } // namespace blink | |
OLD | NEW |