OLD | NEW |
---|---|
1 // Copyright 2017 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef PushPullFIFO_h | 5 #ifndef PushPullFIFO_h |
6 #define PushPullFIFO_h | 6 #define PushPullFIFO_h |
7 | 7 |
8 #include "platform/audio/AudioBus.h" | 8 #include "platform/audio/AudioBus.h" |
9 #include "public/platform/WebCommon.h" | 9 #include "public/platform/WebCommon.h" |
10 #include "wtf/Allocator.h" | 10 #include "wtf/Allocator.h" |
11 #include "wtf/ThreadingPrimitives.h" | |
11 | 12 |
12 namespace blink { | 13 namespace blink { |
13 | 14 |
14 // A configuration data container for PushPullFIFO unit test. | 15 // A configuration data container for PushPullFIFO unit test. |
15 struct PushPullFIFOStateForTest { | 16 struct PushPullFIFOStateForTest { |
16 const size_t fifoLength; | 17 const size_t fifoLength; |
17 const unsigned numberOfChannels; | 18 const unsigned numberOfChannels; |
18 const size_t framesAvailable; | 19 const size_t framesAvailable; |
19 const size_t indexRead; | 20 const size_t indexRead; |
20 const size_t indexWrite; | 21 const size_t indexWrite; |
21 const unsigned overflowCount; | 22 const unsigned overflowCount; |
22 const unsigned underflowCount; | 23 const unsigned underflowCount; |
23 }; | 24 }; |
24 | 25 |
25 // PushPullFIFO class is an intermediate audio sample storage between | 26 // PushPullFIFO class is an intermediate audio sample storage between |
26 // Blink-WebAudio and the renderer. The renderer's hardware callback buffer size | 27 // 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 // 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 // quantum, RQ) thus FIFO is needed to handle the general case. |
30 // | |
31 // Note that this object is concurrently accessed by two threads; WebAudio | |
32 // rendering thread (WebThread) in Blink and the audio device thread | |
33 // (AudioDeviceThread) from the media renderer. The push/pull operations touch | |
34 // most of variables in the class (m_indexWrite, m_indexRead, m_framesAvailable, | |
35 // and m_fifoBus) so the thread safety must be handled with care. | |
36 // | |
37 // TODO(hongchan): add a unit test for multi-thread access. | |
29 class BLINK_PLATFORM_EXPORT PushPullFIFO { | 38 class BLINK_PLATFORM_EXPORT PushPullFIFO { |
30 USING_FAST_MALLOC(PushPullFIFO); | 39 USING_FAST_MALLOC(PushPullFIFO); |
31 WTF_MAKE_NONCOPYABLE(PushPullFIFO); | 40 WTF_MAKE_NONCOPYABLE(PushPullFIFO); |
32 | 41 |
33 public: | 42 public: |
34 // Maximum FIFO length. (512 render quanta) | 43 // Maximum FIFO length. (512 render quanta) |
35 static const size_t kMaxFIFOLength; | 44 static const size_t kMaxFIFOLength; |
36 | 45 |
37 // |fifoLength| cannot exceed |kMaxFIFOLength|. Otherwise it crashes. | 46 // |fifoLength| cannot exceed |kMaxFIFOLength|. Otherwise it crashes. |
38 explicit PushPullFIFO(unsigned numberOfChannels, size_t fifoLength); | 47 explicit PushPullFIFO(unsigned numberOfChannels, size_t fifoLength); |
39 ~PushPullFIFO(); | 48 ~PushPullFIFO(); |
40 | 49 |
41 // Pushes the rendered frames by WebAudio engine. | 50 // Pushes the rendered frames by WebAudio rendering thread. |
42 // - The |inputBus| length is 128 frames (1 render quantum), fixed. | 51 // - The |inputBus| length is 128 frames (1 render quantum), fixed. |
43 // - In case of overflow (FIFO full while push), the existing frames in FIFO | 52 // - In case of overflow (FIFO full while push), the existing frames in FIFO |
44 // will be overwritten and |indexRead| will be forcibly moved to | 53 // will be overwritten and |indexRead| will be forcibly moved to |
45 // |indexWrite| to avoid reading overwritten frames. | 54 // |indexWrite| to avoid reading overwritten frames. |
46 void push(const AudioBus* inputBus); | 55 void push(const AudioBus* inputBus); |
47 | 56 |
48 // Pulling |framesRequested| by the audio device thread. | 57 // Pulling |framesRequested| by the audio device thread. |
49 // - If |framesRequested| is bigger than the length of |outputBus|, it | 58 // - If |framesRequested| is bigger than the length of |outputBus|, it |
50 // violates SECURITY_CHECK(). | 59 // violates SECURITY_CHECK(). |
51 // - If |framesRequested| is bigger than FIFO length, it violates | 60 // - If |framesRequested| is bigger than FIFO length, it violates |
52 // SECURITY_CHECK(). | 61 // SECURITY_CHECK(). |
53 // - In case of underflow (FIFO empty while pull), the remaining space in the | 62 // - 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 | 63 // 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. | 64 // the request from the consumer without causing error, but with a glitch. |
56 void pull(AudioBus* outputBus, size_t framesRequested); | 65 void pull(AudioBus* outputBus, size_t framesRequested); |
57 | 66 |
58 size_t framesAvailable() const { return m_framesAvailable; } | 67 size_t framesAvailable() const; |
59 size_t length() const { return m_fifoLength; } | 68 size_t length() const { return m_fifoLength; } |
60 unsigned numberOfChannels() const { return m_fifoBus->numberOfChannels(); } | 69 unsigned numberOfChannels() const { return m_fifoBus->numberOfChannels(); } |
61 AudioBus* bus() const { return m_fifoBus.get(); } | 70 AudioBus* getFIFOBusForTest() const; |
62 | 71 |
63 // For unit test. Get the current configuration that consists of FIFO length, | 72 // For unit test. Get the current configuration that consists of FIFO length, |
64 // number of channels, read/write index position and under/overflow count. | 73 // number of channels, read/write index position and under/overflow count. |
65 const PushPullFIFOStateForTest getStateForTest() const; | 74 const PushPullFIFOStateForTest getStateForTest() const; |
66 | 75 |
67 private: | 76 private: |
77 // This lock protects all members in this class from being accessed by two | |
o1ka
2017/03/30 13:17:05
This is technically not quite correct: see length(
hongchan
2017/03/30 18:36:00
Done.
| |
78 // threads simultaneously. | |
79 mutable Mutex m_threadMutex; | |
o1ka
2017/03/30 13:17:05
I'm not sure if this rename is useful.
hongchan
2017/03/30 18:36:00
Done.
| |
80 | |
68 // The size of the FIFO. | 81 // The size of the FIFO. |
69 const size_t m_fifoLength = 0; | 82 const size_t m_fifoLength = 0; |
70 | 83 |
71 RefPtr<AudioBus> m_fifoBus; | 84 RefPtr<AudioBus> m_fifoBus; |
o1ka
2017/03/30 13:17:05
Should it be const?
hongchan
2017/03/30 18:36:00
We can, but currently the constructor allocates th
| |
72 | 85 |
73 // The number of frames in the FIFO actually available for pulling. | 86 // The number of frames in the FIFO actually available for pulling. |
74 size_t m_framesAvailable; | 87 size_t m_framesAvailable; |
75 | 88 |
76 size_t m_indexRead; | 89 size_t m_indexRead; |
77 size_t m_indexWrite; | 90 size_t m_indexWrite; |
78 | 91 |
79 unsigned m_overflowCount; | 92 unsigned m_overflowCount; |
80 unsigned m_underflowCount; | 93 unsigned m_underflowCount; |
81 }; | 94 }; |
82 | 95 |
83 } // namespace blink | 96 } // namespace blink |
84 | 97 |
85 #endif // PushPullFIFO_h | 98 #endif // PushPullFIFO_h |
OLD | NEW |