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

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

Issue 2777903005: Add WebThread in AudioDestination to support AudioWorkletThread (Closed)
Patch Set: Refactoring AudioDestination for thread safety 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.
49 // - If |framesRequested| is bigger than the length of |outputBus|, it 60 // - If |framesRequested| is bigger than the length of |outputBus|, it
50 // violates SECURITY_CHECK(). 61 // violates SECURITY_CHECK().
51 // - If |framesRequested| is bigger than FIFO length, it violates 62 // - If |framesRequested| is bigger than FIFO length, it violates
52 // SECURITY_CHECK(). 63 // SECURITY_CHECK().
53 // - In case of underflow (FIFO empty while pull), the remaining space in the 64 // - 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 65 // 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. 66 // the request from the consumer without causing error, but with a glitch.
56 void Pull(AudioBus* output_bus, size_t frames_requested); 67 void Pull(AudioBus* output_bus,
68 size_t frames_requested,
69 std::unique_ptr<WTF::Function<void(size_t, size_t)>>
70 push_request_render_callback);
hongchan 2017/04/14 16:31:48 @nhiroki @haraken Let me know if this is a weird t
57 71
58 size_t FramesAvailable() const { return frames_available_; }
59 size_t length() const { return fifo_length_; } 72 size_t length() const { return fifo_length_; }
60 unsigned NumberOfChannels() const { return fifo_bus_->NumberOfChannels(); } 73 unsigned NumberOfChannels() const { return fifo_bus_->NumberOfChannels(); }
61 AudioBus* Bus() const { return fifo_bus_.Get(); } 74
75 // TODO(hongchan): This is unsafe even for testing. Consider refactoring.
76 AudioBus* GetFIFOBusForTest() const { return fifo_bus_.Get(); }
62 77
63 // For unit test. Get the current configuration that consists of FIFO length, 78 // For unit test. Get the current configuration that consists of FIFO length,
64 // number of channels, read/write index position and under/overflow count. 79 // number of channels, read/write index position and under/overflow count.
65 const PushPullFIFOStateForTest GetStateForTest() const; 80 const PushPullFIFOStateForTest GetStateForTest() const;
66 81
67 private: 82 private:
68 // The size of the FIFO. 83 // The size of the FIFO.
69 const size_t fifo_length_ = 0; 84 const size_t fifo_length_ = 0;
70 85
71 RefPtr<AudioBus> fifo_bus_; 86 RefPtr<AudioBus> fifo_bus_;
72 87
73 // The number of frames in the FIFO actually available for pulling. 88 // The number of frames in the FIFO actually available for pulling.
74 size_t frames_available_; 89 size_t frames_available_;
75 90
76 size_t index_read_; 91 size_t index_read_;
77 size_t index_write_; 92 size_t index_write_;
78 93
79 unsigned overflow_count_; 94 unsigned overflow_count_;
80 unsigned underflow_count_; 95 unsigned underflow_count_;
96
97 // This lock protects: |fifo_bus_|, |frames_available_|, |index_read_| and
98 // |index_write_|.
99 mutable Mutex lock_;
81 }; 100 };
82 101
83 } // namespace blink 102 } // namespace blink
84 103
85 #endif // PushPullFIFO_h 104 #endif // PushPullFIFO_h
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698