OLD | NEW |
---|---|
(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 PushPullFIFO::PushPullFIFO(unsigned numberOfChannels, size_t fifoLength) | |
13 : m_fifoBus(AudioBus::create(numberOfChannels, fifoLength)), | |
14 m_fifoLength(static_cast<int>(fifoLength)), | |
15 m_framesAvailable(0), | |
16 m_indexRead(0), | |
17 m_indexWrite(0) {} | |
18 | |
19 PushPullFIFO::~PushPullFIFO() {} | |
20 | |
21 // Push the data from |inputBus| to FIFO. The size of push is determined by | |
22 // the length of |inputBus|. | |
23 void PushPullFIFO::push(const AudioBus* inputBus) { | |
24 DCHECK(inputBus); | |
25 if (!inputBus) | |
26 return; | |
27 | |
28 int inputBusLength = static_cast<int>(inputBus->length()); | |
o1ka
2017/01/13 11:23:44
const?
hongchan
2017/01/13 23:29:22
Done.
| |
29 int remainder = m_fifoLength - m_indexWrite; | |
o1ka
2017/01/13 11:23:44
const?
hongchan
2017/01/13 23:29:22
Done.
| |
30 SECURITY_CHECK(inputBusLength < m_fifoLength); | |
31 SECURITY_CHECK(m_indexWrite < m_fifoLength); | |
32 | |
33 // This check catches the overflow; the write index will go over the read | |
34 // index. In the case of push overflow, move the read index to the updated | |
35 // write index. | |
36 bool fifoOverflow = m_fifoLength < inputBusLength + m_framesAvailable; | |
37 DCHECK(!fifoOverflow); | |
o1ka
2017/01/13 11:23:44
DCHECK means further execution under this conditio
hongchan
2017/01/13 23:29:22
I talked with rtoy@ about these conditions - what'
| |
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 // Accumulate the valid number of frames in FIFO. | |
56 m_framesAvailable += inputBusLength; | |
57 DCHECK_LE(m_framesAvailable, m_fifoLength); | |
58 | |
59 // Update the write index; wrap it around if necessary. | |
60 m_indexWrite = (m_indexWrite + inputBusLength) % m_fifoLength; | |
61 if (fifoOverflow) { | |
62 m_indexRead = m_indexWrite; | |
63 } | |
64 } | |
65 | |
66 // Pull the data out of FIFO to |outputBus|. If the remaining frames in the FIFO | |
67 // is less than the frames to pull, provides the remaining frames plus the | |
68 // silence. | |
69 void PushPullFIFO::pull(AudioBus* outputBus, size_t framesToPull) { | |
o1ka
2017/01/13 11:23:44
Name it framesRequested? framesToPull, framesToFil
hongchan
2017/01/13 23:29:23
Done.
| |
70 DCHECK(outputBus); | |
71 SECURITY_CHECK(framesToPull <= outputBus->length()); | |
72 if (!outputBus) | |
73 return; | |
74 | |
75 // TODO(hongchan): add a mutex here for this operation when two threads are | |
76 // introduced. Pulling operation has a higher priority to ensure the audio | |
77 // device callback to be uninterrupted. | |
78 | |
79 int remainder = m_fifoLength - m_indexRead; | |
80 int framesToProvide = static_cast<int>(framesToPull); | |
o1ka
2017/01/13 11:23:44
This is not safe cast. Switch interface to int?
o1ka
2017/01/13 11:23:44
both const?
hongchan
2017/01/13 23:29:22
Done.
hongchan
2017/01/13 23:29:23
I have thought about it, but WebAudio has been usi
| |
81 SECURITY_CHECK(framesToProvide < m_fifoLength); | |
82 SECURITY_CHECK(m_indexRead < m_fifoLength); | |
83 | |
84 // This check catches the underflow; the read index will go over the write | |
85 // index. In the case of underflow, the rest will be filled up with zeros | |
86 // (thus glitches). Note that such case does not cause assertion failure | |
87 // unlike the pushing process. Pulling an empty FIFO is a valid operation, | |
88 // and the consumer will get a block of silence. | |
Raymond Toy
2017/01/12 19:29:40
This is a good comment, but I think you really nee
hongchan
2017/01/13 23:29:23
Done.
| |
89 int framesToFill = std::min(m_framesAvailable, framesToProvide); | |
90 int silentFrames = std::max(0, framesToProvide - m_framesAvailable); | |
o1ka
2017/01/13 11:23:44
both const?
o1ka
2017/01/13 11:23:44
Just framesToProvide-framesToFill?
hongchan
2017/01/13 23:29:22
Done.
| |
91 | |
92 for (unsigned i = 0; i < m_fifoBus->numberOfChannels(); ++i) { | |
93 const float* fifoBusChannel = m_fifoBus->channel(i)->data(); | |
94 float* outputBusChannel = outputBus->channel(i)->mutableData(); | |
95 | |
96 // Fill up the output bus with the available frames first. | |
97 if (remainder >= framesToFill) { | |
98 // The remainder is big enough for the frames to pull. | |
99 memcpy(outputBusChannel, fifoBusChannel + m_indexRead, | |
100 framesToFill * sizeof(*fifoBusChannel)); | |
101 } else { | |
102 // The frames to pull is bigger than the remainder size. | |
103 // Wrap around the index. | |
104 memcpy(outputBusChannel, fifoBusChannel + m_indexRead, | |
105 remainder * sizeof(*fifoBusChannel)); | |
106 memcpy(outputBusChannel + remainder, fifoBusChannel, | |
Raymond Toy
2017/01/12 19:29:40
Should we add DCHECKs here to make sure that we do
hongchan
2017/01/13 23:29:22
I believe this is done by line 81 and 82.
| |
107 (framesToFill - remainder) * sizeof(*fifoBusChannel)); | |
108 } | |
109 | |
110 // Then handles the FIFO data shortage if necessary; The frames available | |
111 // was not enough to fulfill the requested frames. Fill the rest of the | |
112 // channel with silence. | |
113 if (silentFrames > 0) { | |
114 memset(outputBusChannel + framesToFill, 0, | |
115 silentFrames * 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 // Decrement the valid number of frames in FIFO. | |
123 m_framesAvailable -= framesToFill; | |
o1ka
2017/01/13 11:23:44
Something like DCHECK((m_indexRead + m_framesAvail
hongchan
2017/01/13 23:29:22
It seems like a neat idea, but not sure how to mak
| |
124 DCHECK_GE(m_framesAvailable, 0); | |
125 } | |
126 | |
127 } // namespace blink | |
OLD | NEW |