Index: media/audio/audio_power_monitor.cc |
diff --git a/media/audio/audio_power_monitor.cc b/media/audio/audio_power_monitor.cc |
index fa76828cc43e5348f5eef67b1c1f228c0fc1deca..68c2bbb48a16a09d328135a9b4a608411ac74082 100644 |
--- a/media/audio/audio_power_monitor.cc |
+++ b/media/audio/audio_power_monitor.cc |
@@ -7,38 +7,34 @@ |
#include <algorithm> |
#include <cmath> |
-#include "base/bind.h" |
#include "base/float_util.h" |
#include "base/logging.h" |
-#include "base/message_loop/message_loop.h" |
+#include "base/third_party/dynamic_annotations/dynamic_annotations.h" |
#include "base/time/time.h" |
#include "media/base/audio_bus.h" |
namespace media { |
AudioPowerMonitor::AudioPowerMonitor( |
- int sample_rate, |
- const base::TimeDelta& time_constant, |
- const base::TimeDelta& measurement_period, |
- base::MessageLoop* message_loop, |
- const PowerMeasurementCallback& callback) |
+ int sample_rate, const base::TimeDelta& time_constant) |
: sample_weight_( |
- 1.0f - expf(-1.0f / (sample_rate * time_constant.InSecondsF()))), |
- num_frames_per_callback_(sample_rate * measurement_period.InSecondsF()), |
- message_loop_(message_loop), |
- power_level_callback_(callback), |
- average_power_(0.0f), |
- clipped_since_last_notification_(false), |
- frames_since_last_notification_(0), |
- last_reported_power_(-1.0f), |
- last_reported_clipped_(false) { |
- DCHECK(message_loop_); |
- DCHECK(!power_level_callback_.is_null()); |
+ 1.0f - expf(-1.0f / (sample_rate * time_constant.InSecondsF()))) { |
+ ANNOTATE_BENIGN_RACE( |
DaleCurtis
2013/08/12 18:23:29
Annotating only keeps TSAN happy, while the impl i
miu
2013/08/12 19:41:46
Done.
|
+ &average_power_, "only one writer; reader takes samples periodically"); |
+ ANNOTATE_BENIGN_RACE( |
+ &has_clipped_, |
+ "flag set by one thread, cleared by the other; okay to be out-of-sync"); |
+ Reset(); |
} |
AudioPowerMonitor::~AudioPowerMonitor() { |
} |
+void AudioPowerMonitor::Reset() { |
+ average_power_ = 0.0f; |
+ has_clipped_ = false; |
+} |
+ |
void AudioPowerMonitor::Scan(const AudioBus& buffer, int num_frames) { |
DCHECK_LE(num_frames, buffer.frames()); |
const int num_channels = buffer.channels(); |
@@ -71,38 +67,28 @@ void AudioPowerMonitor::Scan(const AudioBus& buffer, int num_frames) { |
// Update accumulated results. |
DaleCurtis
2013/08/12 18:23:29
if (lock.Try()) {
average_power_exported_ = aver
miu
2013/08/12 19:41:46
Done.
|
average_power_ = std::max(0.0f, std::min(1.0f, sum_power / num_channels)); |
- clipped_since_last_notification_ |= clipped; |
- frames_since_last_notification_ += num_frames; |
+ if (clipped) |
DaleCurtis
2013/08/12 18:23:29
This is not the same behavior as before and will l
miu
2013/08/12 19:41:46
It looks like the same behavior to me. Can you pr
DaleCurtis
2013/08/12 20:43:56
Ah you're right. The | is clearer to me apparentl
|
+ has_clipped_ = true; |
+} |
- // Once enough frames have been scanned, report the accumulated results. |
- if (frames_since_last_notification_ >= num_frames_per_callback_) { |
- // Note: Forgo making redundant callbacks when results remain unchanged. |
- // Part of this is to pin-down the power to zero if it is insignificantly |
- // small. |
- const float kInsignificantPower = 1.0e-10f; // -100 dBFS |
- const float power = |
- (average_power_ < kInsignificantPower) ? 0.0f : average_power_; |
- if (power != last_reported_power_ || |
- clipped_since_last_notification_ != last_reported_clipped_) { |
- const float power_dbfs = |
- power > 0.0f ? 10.0f * log10f(power) : zero_power(); |
- // Try to post a task to run the callback with the dBFS result. The |
- // posting of the task is guaranteed to be non-blocking, and therefore |
- // could fail. However, in the common case, failures should be rare (and |
- // then the task-post will likely succeed the next time it's attempted). |
- if (!message_loop_->TryPostTask( |
- FROM_HERE, |
- base::Bind(power_level_callback_, |
- power_dbfs, clipped_since_last_notification_))) { |
- DVLOG(2) << "TryPostTask() did not succeed."; |
- return; |
- } |
- last_reported_power_ = power; |
- last_reported_clipped_ = clipped_since_last_notification_; |
- } |
- clipped_since_last_notification_ = false; |
- frames_since_last_notification_ = 0; |
- } |
+float AudioPowerMonitor::ReadCurrentPower() const { |
+ // Ensure we have a copy of |average_power_| local to this thread so that the |
+ // value will not mutate while evaluating the return expression below. |
+ float sampled_power; |
+ memcpy(&sampled_power, &average_power_, sizeof(float)); |
DaleCurtis
2013/08/12 18:23:29
I don't think memcpy provides you any guarantees,
miu
2013/08/12 19:41:46
Removed. Not needed anymore now that we're using
|
+ |
+ // Return the sampled power level, converted to dBFS units, and pinned-down to |
+ // zero if it is insignificantly small. |
+ const float kInsignificantPower = 1.0e-10f; // -100 dBFS |
+ return sampled_power < kInsignificantPower ? zero_power() : |
+ 10.0f * log10f(sampled_power); |
+} |
+ |
+bool AudioPowerMonitor::TestForClippingAndClear() { |
+ const bool clipped = has_clipped_; |
+ if (clipped) |
+ has_clipped_ = false; |
+ return clipped; |
} |
} // namespace media |