Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(145)

Side by Side Diff: third_party/WebKit/Source/platform/audio/PushPullFIFO.h

Issue 2777903005: Add WebThread in AudioDestination to support AudioWorkletThread (Closed)
Patch Set: Parameterized Unit Test Created 3 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 "platform/wtf/Allocator.h" 9 #include "platform/wtf/Allocator.h"
10 #include "platform/wtf/Functional.h"
11 #include "platform/wtf/Threading.h"
12 #include "platform/wtf/ThreadingPrimitives.h"
10 #include "public/platform/WebCommon.h" 13 #include "public/platform/WebCommon.h"
11 14
12 namespace blink { 15 namespace blink {
13 16
14 // A configuration data container for PushPullFIFO unit test. 17 // A configuration data container for PushPullFIFO unit test.
15 struct PushPullFIFOStateForTest { 18 struct PushPullFIFOStateForTest {
16 const size_t fifo_length; 19 const size_t fifo_length;
17 const unsigned number_of_channels; 20 const unsigned number_of_channels;
18 const size_t frames_available; 21 const size_t frames_available;
19 const size_t index_read; 22 const size_t index_read;
20 const size_t index_write; 23 const size_t index_write;
21 const unsigned overflow_count; 24 const unsigned overflow_count;
22 const unsigned underflow_count; 25 const unsigned underflow_count;
23 }; 26 };
24 27
25 // PushPullFIFO class is an intermediate audio sample storage between 28 // PushPullFIFO class is an intermediate audio sample storage between
26 // Blink-WebAudio and the renderer. The renderer's hardware callback buffer size 29 // 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 30 // varies on the platform, but the WebAudio always renders 128 frames (render
28 // quantum, RQ) thus FIFO is needed to handle the general case. 31 // quantum, RQ) thus FIFO is needed to handle the general case.
32 //
33 // Note that this object is concurrently accessed by two threads; WebAudio
34 // rendering thread (WebThread) in Blink and the audio device thread
35 // (AudioDeviceThread) from the media renderer. The push/pull operations touch
36 // most of variables in the class (index_write_, index_read_, frames_available_,
37 // and fifo_Bus_) so the thread safety must be handled with care.
38 //
39 // TODO(hongchan): add a unit test for multi-thread access.
29 class BLINK_PLATFORM_EXPORT PushPullFIFO { 40 class BLINK_PLATFORM_EXPORT PushPullFIFO {
30 USING_FAST_MALLOC(PushPullFIFO); 41 USING_FAST_MALLOC(PushPullFIFO);
31 WTF_MAKE_NONCOPYABLE(PushPullFIFO); 42 WTF_MAKE_NONCOPYABLE(PushPullFIFO);
32 43
33 public: 44 public:
34 // Maximum FIFO length. (512 render quanta) 45 // Maximum FIFO length. (512 render quanta)
35 static const size_t kMaxFIFOLength; 46 static const size_t kMaxFIFOLength;
36 47
37 // |fifoLength| cannot exceed |kMaxFIFOLength|. Otherwise it crashes. 48 // |fifoLength| cannot exceed |kMaxFIFOLength|. Otherwise it crashes.
38 explicit PushPullFIFO(unsigned number_of_channels, size_t fifo_length); 49 explicit PushPullFIFO(unsigned number_of_channels, size_t fifo_length);
39 ~PushPullFIFO(); 50 ~PushPullFIFO();
40 51
41 // Pushes the rendered frames by WebAudio engine. 52 // Pushes the rendered frames by WebAudio engine.
42 // - The |inputBus| length is 128 frames (1 render quantum), fixed. 53 // - The |inputBus| length is 128 frames (1 render quantum), fixed.
43 // - In case of overflow (FIFO full while push), the existing frames in FIFO 54 // - In case of overflow (FIFO full while push), the existing frames in FIFO
44 // will be overwritten and |indexRead| will be forcibly moved to 55 // will be overwritten and |indexRead| will be forcibly moved to
45 // |indexWrite| to avoid reading overwritten frames. 56 // |indexWrite| to avoid reading overwritten frames.
46 void Push(const AudioBus* input_bus); 57 void Push(const AudioBus* input_bus);
47 58
48 // Pulling |framesRequested| by the audio device thread. 59 // Pulling |framesRequested| by the audio device thread and returns the actual
nhiroki 2017/04/25 00:35:51 s/Pulling/Pulls/
hongchan 2017/04/25 20:36:42 Done.
60 // number of frames to be rendered by the source. (i.e. WebAudio graph)
49 // - If |framesRequested| is bigger than the length of |outputBus|, it 61 // - If |framesRequested| is bigger than the length of |outputBus|, it
50 // violates SECURITY_CHECK(). 62 // violates SECURITY_CHECK().
51 // - If |framesRequested| is bigger than FIFO length, it violates 63 // - If |framesRequested| is bigger than FIFO length, it violates
52 // SECURITY_CHECK(). 64 // SECURITY_CHECK().
53 // - In case of underflow (FIFO empty while pull), the remaining space in the 65 // - 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 66 // 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. 67 // the request from the consumer without causing error, but with a glitch.
56 void Pull(AudioBus* output_bus, size_t frames_requested); 68 size_t Pull(AudioBus* output_bus, size_t frames_requested);
57 69
58 size_t FramesAvailable() const { return frames_available_; }
59 size_t length() const { return fifo_length_; } 70 size_t length() const { return fifo_length_; }
60 unsigned NumberOfChannels() const { return fifo_bus_->NumberOfChannels(); } 71 unsigned NumberOfChannels() const { return fifo_bus_->NumberOfChannels(); }
61 AudioBus* Bus() const { return fifo_bus_.Get(); } 72
73 // TODO(hongchan): This is unsafe even for testing. Consider refactoring.
74 AudioBus* GetFIFOBusForTest() const { return fifo_bus_.Get(); }
o1ka 2017/04/25 08:23:22 We should fix this TODO before landing the CL. WDY
hongchan 2017/04/25 20:36:41 I have to think about moving |bus| to the protecte
62 75
63 // For unit test. Get the current configuration that consists of FIFO length, 76 // For unit test. Get the current configuration that consists of FIFO length,
64 // number of channels, read/write index position and under/overflow count. 77 // number of channels, read/write index position and under/overflow count.
65 const PushPullFIFOStateForTest GetStateForTest() const; 78 const PushPullFIFOStateForTest GetStateForTest() const;
66 79
67 private: 80 private:
68 // The size of the FIFO. 81 // The size of the FIFO.
69 const size_t fifo_length_ = 0; 82 const size_t fifo_length_ = 0;
70 83
71 RefPtr<AudioBus> fifo_bus_; 84 unsigned overflow_count_ = 0;
85 unsigned underflow_count_ = 0;
72 86
87 // This lock protects variables below.
88 Mutex lock_;
73 // The number of frames in the FIFO actually available for pulling. 89 // The number of frames in the FIFO actually available for pulling.
74 size_t frames_available_; 90 size_t frames_available_;
75
76 size_t index_read_; 91 size_t index_read_;
77 size_t index_write_; 92 size_t index_write_;
o1ka 2017/04/25 08:23:22 Initialize everything with zeroes here?
hongchan 2017/04/25 20:36:42 Done.
78 93 RefPtr<AudioBus> fifo_bus_;
79 unsigned overflow_count_;
80 unsigned underflow_count_;
81 }; 94 };
82 95
83 } // namespace blink 96 } // namespace blink
84 97
85 #endif // PushPullFIFO_h 98 #endif // PushPullFIFO_h
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698