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 length) | |
13 : m_fifoDataBus(AudioBus::create(numberOfChannels, length)), | |
14 m_fifoLength(length), | |
15 m_framesInFIFO(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 // | |
24 // TODO(hongchan): consider the following cases. | |
25 // - What if the write index wraps around and passes the read index again? | |
o1ka
2017/01/10 10:07:19
This is the most interesting part.
| |
26 // - What if the pulling is invoked while the pushing is in progress? | |
o1ka
2017/01/10 10:07:19
Grab the lock :)
| |
27 void PushPullFIFO::push(const AudioBus* inputBus) { | |
28 DCHECK(inputBus); | |
29 DCHECK_LE(inputBus->length() + m_framesInFIFO, m_fifoLength); | |
30 DCHECK_LE(m_indexWrite, m_fifoLength); | |
31 | |
32 if (!inputBus) | |
33 return; | |
o1ka
2017/01/10 10:07:19
Why these silent returns are less preferable than
Raymond Toy
2017/01/10 16:18:15
This is how most of WebAudio works, from the origi
| |
34 | |
35 size_t remainder = m_fifoLength - m_indexWrite; | |
o1ka
2017/01/10 10:07:19
Here and in other places where you work with memor
| |
36 size_t inputBusLength = inputBus->length(); | |
37 | |
38 for (unsigned i = 0; i < m_fifoDataBus->numberOfChannels(); ++i) { | |
39 float* fifoBusChannel = m_fifoDataBus->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 // Accumulate the valid number of frames in FIFO. | |
55 m_framesInFIFO += inputBusLength; | |
56 DCHECK_LE(m_framesInFIFO, m_fifoLength); | |
57 | |
58 // Update the write index; wrap it around if necessary. | |
59 m_indexWrite = (m_indexWrite + inputBusLength) % m_fifoLength; | |
60 } | |
61 | |
62 // Pull the data out of FIFO to |outputBus|. If the remaining frames in the FIFO | |
63 // is less than the frames to pull, provides the remaining frames plus the | |
64 // silence. | |
o1ka
2017/01/10 10:07:20
This is not true as of now. You have a DCHECK on l
| |
65 // | |
66 // TODO(hongchan): consider the following cases. | |
67 // - What if the frames to pull is bigger than the frames in FIFO? | |
68 // - What should the lock be placed? | |
o1ka
2017/01/10 10:07:19
Not sure what it means.
| |
69 void PushPullFIFO::pull(AudioBus* outputBus, size_t framesToPull) { | |
70 DCHECK(outputBus); | |
71 DCHECK_LE(framesToPull, outputBus->length()); | |
72 DCHECK_LE(framesToPull, m_fifoLength); | |
73 DCHECK_LE(framesToPull, m_framesInFIFO); | |
74 DCHECK_LE(m_indexRead, m_fifoLength); | |
o1ka
2017/01/10 10:07:19
See my other comment about memory indexes: these s
| |
75 | |
76 if (!outputBus) | |
77 return; | |
78 | |
79 // Pulling operation has a higher priority to ensure the audio device callback | |
80 // to be uninterrupted. | |
81 MutexLocker locker(m_fifoDataBusLock); | |
o1ka
2017/01/10 10:07:20
I'm not sure how "priority" is addressed here.
How
| |
82 | |
83 size_t remainder = m_fifoLength - m_indexRead; | |
84 | |
85 for (unsigned i = 0; i < m_fifoDataBus->numberOfChannels(); ++i) { | |
86 const float* fifoBusChannel = m_fifoDataBus->channel(i)->data(); | |
87 float* outputBusChannel = outputBus->channel(i)->mutableData(); | |
88 if (remainder >= framesToPull) { | |
89 // The remainder is big enough for the frames to pull. | |
90 memcpy(outputBusChannel, fifoBusChannel + m_indexRead, | |
91 framesToPull * sizeof(*fifoBusChannel)); | |
92 } else { | |
93 // The frames to pull is bigger than the remainder size. | |
94 // Wrap around the index. | |
95 memcpy(outputBusChannel, fifoBusChannel + m_indexRead, | |
96 remainder * sizeof(*fifoBusChannel)); | |
97 memcpy(outputBusChannel + remainder, fifoBusChannel, | |
98 (framesToPull - remainder) * sizeof(*fifoBusChannel)); | |
99 } | |
100 } | |
101 | |
102 // Update the read index; wrap it around if necessary. | |
103 m_indexRead = (m_indexRead + framesToPull) % m_fifoLength; | |
104 | |
105 // Decrement the valid number of frames in FIFO. | |
106 m_framesInFIFO -= framesToPull; | |
107 } | |
108 | |
109 } // namespace blink | |
OLD | NEW |