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 #include "platform/audio/PushPullFIFO.h" | 5 #include "platform/audio/PushPullFIFO.h" |
6 | 6 |
7 #include <memory> | 7 #include <memory> |
8 #include "platform/audio/AudioUtilities.h" | 8 #include "platform/audio/AudioUtilities.h" |
9 #include "platform/wtf/PtrUtil.h" | 9 #include "platform/wtf/PtrUtil.h" |
10 | 10 |
(...skipping 16 matching lines...) Expand all Loading... | |
27 underflow_count_(0) { | 27 underflow_count_(0) { |
28 CHECK_LE(fifo_length_, kMaxFIFOLength); | 28 CHECK_LE(fifo_length_, kMaxFIFOLength); |
29 fifo_bus_ = AudioBus::Create(number_of_channels, fifo_length_); | 29 fifo_bus_ = AudioBus::Create(number_of_channels, fifo_length_); |
30 } | 30 } |
31 | 31 |
32 PushPullFIFO::~PushPullFIFO() {} | 32 PushPullFIFO::~PushPullFIFO() {} |
33 | 33 |
34 // Push the data from |inputBus| to FIFO. The size of push is determined by | 34 // Push the data from |inputBus| to FIFO. The size of push is determined by |
35 // the length of |inputBus|. | 35 // the length of |inputBus|. |
36 void PushPullFIFO::Push(const AudioBus* input_bus) { | 36 void PushPullFIFO::Push(const AudioBus* input_bus) { |
37 MutexLocker locker(lock_); | |
38 | |
37 CHECK(input_bus); | 39 CHECK(input_bus); |
38 CHECK_EQ(input_bus->length(), AudioUtilities::kRenderQuantumFrames); | 40 CHECK_EQ(input_bus->length(), AudioUtilities::kRenderQuantumFrames); |
39 SECURITY_CHECK(input_bus->length() <= fifo_length_); | 41 SECURITY_CHECK(input_bus->length() <= fifo_length_); |
40 SECURITY_CHECK(index_write_ < fifo_length_); | 42 SECURITY_CHECK(index_write_ < fifo_length_); |
41 | 43 |
42 const size_t input_bus_length = input_bus->length(); | 44 const size_t input_bus_length = input_bus->length(); |
43 const size_t remainder = fifo_length_ - index_write_; | 45 const size_t remainder = fifo_length_ - index_write_; |
44 | 46 |
45 for (unsigned i = 0; i < fifo_bus_->NumberOfChannels(); ++i) { | 47 for (unsigned i = 0; i < fifo_bus_->NumberOfChannels(); ++i) { |
46 float* fifo_bus_channel = fifo_bus_->Channel(i)->MutableData(); | 48 float* fifo_bus_channel = fifo_bus_->Channel(i)->MutableData(); |
(...skipping 28 matching lines...) Expand all Loading... | |
75 } | 77 } |
76 | 78 |
77 // Update the number of frames available in FIFO. | 79 // Update the number of frames available in FIFO. |
78 frames_available_ = | 80 frames_available_ = |
79 std::min(frames_available_ + input_bus_length, fifo_length_); | 81 std::min(frames_available_ + input_bus_length, fifo_length_); |
80 DCHECK_EQ((index_read_ + frames_available_) % fifo_length_, index_write_); | 82 DCHECK_EQ((index_read_ + frames_available_) % fifo_length_, index_write_); |
81 } | 83 } |
82 | 84 |
83 // Pull the data out of FIFO to |outputBus|. If remaining frame in the FIFO | 85 // Pull the data out of FIFO to |outputBus|. If remaining frame in the FIFO |
84 // is less than the frames to pull, provides remaining frame plus the silence. | 86 // is less than the frames to pull, provides remaining frame plus the silence. |
85 void PushPullFIFO::Pull(AudioBus* output_bus, size_t frames_requested) { | 87 size_t PushPullFIFO::Pull(AudioBus* output_bus, size_t frames_requested) { |
88 MutexLocker locker(lock_); | |
89 | |
86 #if OS(ANDROID) | 90 #if OS(ANDROID) |
87 if (!output_bus) { | 91 if (!output_bus) { |
88 // Log when outputBus or FIFO object is invalid. (crbug.com/692423) | 92 // Log when outputBus or FIFO object is invalid. (crbug.com/692423) |
89 LOG(WARNING) << "[WebAudio/PushPullFIFO::pull <" << static_cast<void*>(this) | 93 LOG(WARNING) << "[WebAudio/PushPullFIFO::pull <" << static_cast<void*>(this) |
90 << ">] |outputBus| is invalid."; | 94 << ">] |outputBus| is invalid."; |
91 // Silently return to avoid crash. | 95 // Silently return to avoid crash. |
92 return; | 96 return 0; |
93 } | 97 } |
94 | 98 |
95 // The following checks are in place to catch the inexplicable crash. | 99 // The following checks are in place to catch the inexplicable crash. |
96 // (crbug.com/692423) | 100 // (crbug.com/692423) |
97 if (frames_requested > output_bus->length()) { | 101 if (frames_requested > output_bus->length()) { |
98 LOG(WARNING) << "[WebAudio/PushPullFIFO::pull <" << static_cast<void*>(this) | 102 LOG(WARNING) << "[WebAudio/PushPullFIFO::pull <" << static_cast<void*>(this) |
99 << ">] framesRequested > outputBus->length() (" | 103 << ">] framesRequested > outputBus->length() (" |
100 << frames_requested << " > " << output_bus->length() << ")"; | 104 << frames_requested << " > " << output_bus->length() << ")"; |
101 } | 105 } |
102 if (frames_requested > fifo_length_) { | 106 if (frames_requested > fifo_length_) { |
103 LOG(WARNING) << "[WebAudio/PushPullFIFO::pull <" << static_cast<void*>(this) | 107 LOG(WARNING) << "[WebAudio/PushPullFIFO::pull <" << static_cast<void*>(this) |
104 << ">] framesRequested > m_fifoLength (" << frames_requested | 108 << ">] framesRequested > m_fifoLength (" << frames_requested |
105 << " > " << fifo_length_ << ")"; | 109 << " > " << fifo_length_ << ")"; |
106 } | 110 } |
107 if (index_read_ >= fifo_length_) { | 111 if (index_read_ >= fifo_length_) { |
108 LOG(WARNING) << "[WebAudio/PushPullFIFO::pull <" << static_cast<void*>(this) | 112 LOG(WARNING) << "[WebAudio/PushPullFIFO::pull <" << static_cast<void*>(this) |
109 << ">] m_indexRead >= m_fifoLength (" << index_read_ | 113 << ">] m_indexRead >= m_fifoLength (" << index_read_ |
110 << " >= " << fifo_length_ << ")"; | 114 << " >= " << fifo_length_ << ")"; |
111 } | 115 } |
112 #endif | 116 #endif |
117 | |
113 CHECK(output_bus); | 118 CHECK(output_bus); |
114 SECURITY_CHECK(frames_requested <= output_bus->length()); | 119 SECURITY_CHECK(frames_requested <= output_bus->length()); |
115 SECURITY_CHECK(frames_requested <= fifo_length_); | 120 SECURITY_CHECK(frames_requested <= fifo_length_); |
116 SECURITY_CHECK(index_read_ < fifo_length_); | 121 SECURITY_CHECK(index_read_ < fifo_length_); |
117 | 122 |
118 const size_t remainder = fifo_length_ - index_read_; | 123 const size_t remainder = fifo_length_ - index_read_; |
119 const size_t frames_to_fill = std::min(frames_available_, frames_requested); | 124 const size_t frames_to_fill = std::min(frames_available_, frames_requested); |
120 | 125 |
121 for (unsigned i = 0; i < fifo_bus_->NumberOfChannels(); ++i) { | 126 for (unsigned i = 0; i < fifo_bus_->NumberOfChannels(); ++i) { |
122 const float* fifo_bus_channel = fifo_bus_->Channel(i)->Data(); | 127 const float* fifo_bus_channel = fifo_bus_->Channel(i)->Data(); |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
155 << "underflowCount=" << underflow_count_ | 160 << "underflowCount=" << underflow_count_ |
156 << ", availableFrames=" << frames_available_ | 161 << ", availableFrames=" << frames_available_ |
157 << ", requestedFrames=" << frames_requested | 162 << ", requestedFrames=" << frames_requested |
158 << ", fifoLength=" << fifo_length_ << ")"; | 163 << ", fifoLength=" << fifo_length_ << ")"; |
159 } | 164 } |
160 } | 165 } |
161 | 166 |
162 // Update the number of frames in FIFO. | 167 // Update the number of frames in FIFO. |
163 frames_available_ -= frames_to_fill; | 168 frames_available_ -= frames_to_fill; |
164 DCHECK_EQ((index_read_ + frames_available_) % fifo_length_, index_write_); | 169 DCHECK_EQ((index_read_ + frames_available_) % fifo_length_, index_write_); |
170 | |
171 // Number of frames to render via WebAudio graph. |framesToRender > 0| means | |
172 // the frames in FIFO is not enough to fulfill the requested frames from the | |
173 // audio device. | |
o1ka
2017/04/18 14:14:57
Better to put this comment into the method descrip
hongchan
2017/04/18 18:16:09
I moved the comment about the return value to the
| |
174 return frames_requested > frames_available_ | |
175 ? frames_requested - frames_available_ | |
176 : 0; | |
165 } | 177 } |
166 | 178 |
167 const PushPullFIFOStateForTest PushPullFIFO::GetStateForTest() const { | 179 const PushPullFIFOStateForTest PushPullFIFO::GetStateForTest() const { |
168 return {length(), NumberOfChannels(), FramesAvailable(), index_read_, | 180 return {length(), NumberOfChannels(), frames_available_, index_read_, |
169 index_write_, overflow_count_, underflow_count_}; | 181 index_write_, overflow_count_, underflow_count_}; |
170 } | 182 } |
171 | 183 |
172 } // namespace blink | 184 } // namespace blink |
OLD | NEW |