Chromium Code Reviews| 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 |
|
o1ka
2017/03/29 09:05:11
I understand that this is POC, so it's a side note
hongchan
2017/03/29 19:39:59
Of course. This CL will eventually have a unit tes
| |
| 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. |
|
nhiroki
2017/03/29 01:54:05
Can you add comments about thread safety of this c
Raymond Toy
2017/03/29 16:13:13
Yes, please. And add appropriate CHECKs in each o
hongchan
2017/03/29 19:39:58
nhiroki@
Can you suggest a way to check which thr
nhiroki
2017/03/30 09:04:41
Hmm... I cannot think of a good idea. If this clas
| |
| 29 class BLINK_PLATFORM_EXPORT PushPullFIFO { | 30 class BLINK_PLATFORM_EXPORT PushPullFIFO { |
| 30 USING_FAST_MALLOC(PushPullFIFO); | 31 USING_FAST_MALLOC(PushPullFIFO); |
| 31 WTF_MAKE_NONCOPYABLE(PushPullFIFO); | 32 WTF_MAKE_NONCOPYABLE(PushPullFIFO); |
| 32 | 33 |
| 33 public: | 34 public: |
| 34 // Maximum FIFO length. (512 render quanta) | 35 // Maximum FIFO length. (512 render quanta) |
| 35 static const size_t kMaxFIFOLength; | 36 static const size_t kMaxFIFOLength; |
| 36 | 37 |
| 37 // |fifoLength| cannot exceed |kMaxFIFOLength|. Otherwise it crashes. | 38 // |fifoLength| cannot exceed |kMaxFIFOLength|. Otherwise it crashes. |
| 38 explicit PushPullFIFO(unsigned numberOfChannels, size_t fifoLength); | 39 explicit PushPullFIFO(unsigned numberOfChannels, size_t fifoLength); |
| 39 ~PushPullFIFO(); | 40 ~PushPullFIFO(); |
| 40 | 41 |
| 41 // Pushes the rendered frames by WebAudio engine. | 42 // Pushes the rendered frames by WebAudio engine. |
| 42 // - The |inputBus| length is 128 frames (1 render quantum), fixed. | 43 // - The |inputBus| length is 128 frames (1 render quantum), fixed. |
| 43 // - In case of overflow (FIFO full while push), the existing frames in FIFO | 44 // - 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 // will be overwritten and |indexRead| will be forcibly moved to |
| 45 // |indexWrite| to avoid reading overwritten frames. | 46 // |indexWrite| to avoid reading overwritten frames. |
| 46 void push(const AudioBus* inputBus); | 47 void push(const AudioBus* inputBus); |
| 47 | 48 |
| 48 // Pulling |framesRequested| by the audio device thread. | 49 // Pulling |framesRequested| by the audio device thread. |
| 49 // - If |framesRequested| is bigger than the length of |outputBus|, it | 50 // - If |framesRequested| is bigger than the length of |outputBus|, it |
| 50 // violates SECURITY_CHECK(). | 51 // violates SECURITY_CHECK(). |
| 51 // - If |framesRequested| is bigger than FIFO length, it violates | 52 // - If |framesRequested| is bigger than FIFO length, it violates |
| 52 // SECURITY_CHECK(). | 53 // SECURITY_CHECK(). |
| 53 // - In case of underflow (FIFO empty while pull), the remaining space in the | 54 // - 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 // 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 // the request from the consumer without causing error, but with a glitch. |
| 56 void pull(AudioBus* outputBus, size_t framesRequested); | 57 void pull(AudioBus* outputBus, size_t framesRequested); |
| 57 | 58 |
| 58 size_t framesAvailable() const { return m_framesAvailable; } | 59 size_t framesAvailable() const { return m_framesAvailable; } |
|
o1ka
2017/03/29 09:05:11
This is a non thread-safe method.
hongchan
2017/03/29 19:39:58
Added a lock. Done.
| |
| 59 size_t length() const { return m_fifoLength; } | 60 size_t length() const { return m_fifoLength; } |
| 60 unsigned numberOfChannels() const { return m_fifoBus->numberOfChannels(); } | 61 unsigned numberOfChannels() const { return m_fifoBus->numberOfChannels(); } |
| 61 AudioBus* bus() const { return m_fifoBus.get(); } | 62 AudioBus* bus() const { return m_fifoBus.get(); } |
|
o1ka
2017/03/29 09:05:11
Providing direct access to a memory buffer which i
hongchan
2017/03/29 19:39:59
Done.
| |
| 62 | 63 |
| 63 // For unit test. Get the current configuration that consists of FIFO length, | 64 // 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 // number of channels, read/write index position and under/overflow count. |
| 65 const PushPullFIFOStateForTest getStateForTest() const; | 66 const PushPullFIFOStateForTest getStateForTest() const; |
| 66 | 67 |
| 67 private: | 68 private: |
| 69 Mutex m_lock; | |
|
nhiroki
2017/03/29 01:54:05
What does this lock protect?
haraken
2017/03/29 07:56:24
Same question from me.
hongchan
2017/03/29 19:39:59
See push()/pull() methods. This lock protects:
m_f
| |
| 70 | |
| 68 // The size of the FIFO. | 71 // The size of the FIFO. |
| 69 const size_t m_fifoLength = 0; | 72 const size_t m_fifoLength = 0; |
| 70 | 73 |
| 71 RefPtr<AudioBus> m_fifoBus; | 74 RefPtr<AudioBus> m_fifoBus; |
| 72 | 75 |
| 73 // The number of frames in the FIFO actually available for pulling. | 76 // The number of frames in the FIFO actually available for pulling. |
| 74 size_t m_framesAvailable; | 77 size_t m_framesAvailable; |
| 75 | 78 |
| 76 size_t m_indexRead; | 79 size_t m_indexRead; |
| 77 size_t m_indexWrite; | 80 size_t m_indexWrite; |
| 78 | 81 |
| 79 unsigned m_overflowCount; | 82 unsigned m_overflowCount; |
| 80 unsigned m_underflowCount; | 83 unsigned m_underflowCount; |
| 81 }; | 84 }; |
| 82 | 85 |
| 83 } // namespace blink | 86 } // namespace blink |
| 84 | 87 |
| 85 #endif // PushPullFIFO_h | 88 #endif // PushPullFIFO_h |
| OLD | NEW |