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 #ifndef PushPullFIFO_h | |
6 #define PushPullFIFO_h | |
7 | |
8 #include "platform/audio/AudioBus.h" | |
9 #include "public/platform/WebCommon.h" | |
10 #include "wtf/Allocator.h" | |
11 | |
12 namespace blink { | |
13 | |
14 // A configuration data container for PushPullFIFO unit test. | |
15 struct PushPullFIFOStateForTest { | |
16 const size_t fifoLength; | |
17 const unsigned numberOfChannels; | |
18 const size_t framesAvailable; | |
19 const size_t indexRead; | |
20 const size_t indexWrite; | |
21 const unsigned overflowCount; | |
22 const unsigned underflowCount; | |
23 }; | |
24 | |
25 // PushPullFIFO class is an intermediate audio sample storage between | |
26 // Blink-WebAudio and the renderer. The renderer's hardware callback buffer size | |
27 // varies on the platform, but the WebAudio always renders 128 frames (render | |
28 // quantum, RQ) thus FIFO is needed to handle the general case. | |
29 class BLINK_PLATFORM_EXPORT PushPullFIFO { | |
30 USING_FAST_MALLOC(PushPullFIFO); | |
31 WTF_MAKE_NONCOPYABLE(PushPullFIFO); | |
32 | |
33 public: | |
34 // Maximum FIFO length. (512 RQs) | |
o1ka
2017/02/03 10:41:17
nit: Render Quantum (RQ is a bit confusing)
| |
35 static const size_t kMaxFIFOLength; | |
36 | |
37 // |fifoLength| cannot exceed |kMaxFIFOLength|. Otherwise it crashes. | |
38 explicit PushPullFIFO(unsigned numberOfChannels, size_t fifoLength); | |
39 ~PushPullFIFO(); | |
40 | |
41 // Pushes the rendered frames by WebAudio engine. | |
42 // - The |inputBus| length is 128 frames (1 RQ), fixed. | |
43 // - In case of overflow (FIFO full while push), the existing frames in FIFO | |
44 // will be overwritten and |indexRead| will be forcibly moved to | |
45 // |indexWrite| to avoid reading overwritten frames. | |
46 void push(const AudioBus* inputBus); | |
47 | |
48 // Pulling |framesRequested| by the audio device thread. | |
49 // - If |framesRequested| is bigger than the length of |outputBus|, it | |
50 // violates SECURITY_CHECK(). | |
51 // - If |framesRequested| is bigger than FIFO length, it violates | |
52 // SECURITY_CHECK(). | |
53 // - In case of underflow (FIFO empty while pull), the remaining space in the | |
54 // requested output bus will be filled with silence. Thus it will fulfill | |
55 // the request from the consumer without causing error, but with a glitch. | |
56 void pull(AudioBus* outputBus, size_t framesRequested); | |
57 | |
58 size_t framesAvailable() const { return m_framesAvailable; } | |
59 size_t length() const { return m_fifoLength; } | |
60 unsigned numberOfChannels() const { return m_fifoBus->numberOfChannels(); } | |
61 AudioBus* bus() const { return m_fifoBus.get(); } | |
62 | |
63 // For unit test. Get the current configuration that consists of FIFO length, | |
64 // number of channels, read/write index position and under/overflow count. | |
65 const PushPullFIFOStateForTest getStateForTest() const; | |
66 | |
67 private: | |
68 // The size of the FIFO. | |
69 const size_t m_fifoLength = 0; | |
70 | |
71 RefPtr<AudioBus> m_fifoBus; | |
72 | |
73 // The number of frames in the FIFO actually available for pulling. | |
74 size_t m_framesAvailable; | |
75 | |
76 size_t m_indexRead; | |
77 size_t m_indexWrite; | |
78 | |
79 unsigned m_overflowCount; | |
80 unsigned m_underflowCount; | |
81 }; | |
82 | |
83 } // namespace blink | |
84 | |
85 #endif // PushPullFIFO_h | |
OLD | NEW |