Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / media / audio / audio_input_controller.cc
index d57a113..44ecb29 100644 (file)
@@ -33,17 +33,6 @@ const int kTimerResetIntervalSeconds = 1;
 const int kTimerInitialIntervalSeconds = 5;
 
 #if defined(AUDIO_POWER_MONITORING)
-// Time constant for AudioPowerMonitor.
-// The utilized smoothing factor (alpha) in the exponential filter is given
-// by 1-exp(-1/(fs*ts)), where fs is the sample rate in Hz and ts is the time
-// constant given by |kPowerMeasurementTimeConstantMilliseconds|.
-// Example: fs=44100, ts=10e-3 => alpha~0.022420
-//          fs=44100, ts=20e-3 => alpha~0.165903
-// A large smoothing factor corresponds to a faster filter response to input
-// changes since y(n)=alpha*x(n)+(1-alpha)*y(n-1), where x(n) is the input
-// and y(n) is the output.
-const int kPowerMeasurementTimeConstantMilliseconds = 10;
-
 // Time in seconds between two successive measurements of audio power levels.
 const int kPowerMonitorLogIntervalSeconds = 15;
 
@@ -65,7 +54,39 @@ void LogMicrophoneMuteResult(MicrophoneMuteResult result) {
                             result,
                             MICROPHONE_MUTE_MAX + 1);
 }
-#endif
+
+// Helper method which calculates the average power of an audio bus. Unit is in
+// dBFS, where 0 dBFS corresponds to all channels and samples equal to 1.0.
+float AveragePower(const media::AudioBus& buffer) {
+  const int frames = buffer.frames();
+  const int channels = buffer.channels();
+  if (frames <= 0 || channels <= 0)
+    return 0.0f;
+
+  // Scan all channels and accumulate the sum of squares for all samples.
+  float sum_power = 0.0f;
+  for (int ch = 0; ch < channels; ++ch) {
+    const float* channel_data = buffer.channel(ch);
+    for (int i = 0; i < frames; i++) {
+      const float sample = channel_data[i];
+      sum_power += sample * sample;
+    }
+  }
+
+  // Update accumulated average results, with clamping for sanity.
+  const float average_power =
+      std::max(0.0f, std::min(1.0f, sum_power / (frames * channels)));
+
+  // Convert average power level to dBFS units, and pin it down to zero if it
+  // is insignificantly small.
+  const float kInsignificantPower = 1.0e-10f;  // -100 dBFS
+  const float power_dbfs = average_power < kInsignificantPower ?
+      -std::numeric_limits<float>::infinity() : 10.0f * log10f(average_power);
+
+  return power_dbfs;
+}
+#endif  // AUDIO_POWER_MONITORING
+
 }
 
 // Used to log the result of capture startup.
@@ -96,7 +117,8 @@ AudioInputController::Factory* AudioInputController::factory_ = NULL;
 
 AudioInputController::AudioInputController(EventHandler* handler,
                                            SyncWriter* sync_writer,
-                                           UserInputMonitor* user_input_monitor)
+                                           UserInputMonitor* user_input_monitor,
+                                           const bool agc_is_enabled)
     : creator_task_runner_(base::MessageLoopProxy::current()),
       handler_(handler),
       stream_(NULL),
@@ -105,7 +127,9 @@ AudioInputController::AudioInputController(EventHandler* handler,
       sync_writer_(sync_writer),
       max_volume_(0.0),
       user_input_monitor_(user_input_monitor),
+      agc_is_enabled_(agc_is_enabled),
 #if defined(AUDIO_POWER_MONITORING)
+      power_measurement_is_enabled_(false),
       log_silence_state_(false),
       silence_state_(SILENCE_STATE_NO_MEASUREMENT),
 #endif
@@ -134,7 +158,7 @@ scoped_refptr<AudioInputController> AudioInputController::Create(
         audio_manager, event_handler, params, user_input_monitor);
   }
   scoped_refptr<AudioInputController> controller(
-      new AudioInputController(event_handler, NULL, user_input_monitor));
+      new AudioInputController(event_handler, NULL, user_input_monitor, false));
 
   controller->task_runner_ = audio_manager->GetTaskRunner();
 
@@ -160,7 +184,8 @@ scoped_refptr<AudioInputController> AudioInputController::CreateLowLatency(
     const AudioParameters& params,
     const std::string& device_id,
     SyncWriter* sync_writer,
-    UserInputMonitor* user_input_monitor) {
+    UserInputMonitor* user_input_monitor,
+    const bool agc_is_enabled) {
   DCHECK(audio_manager);
   DCHECK(sync_writer);
 
@@ -169,8 +194,8 @@ scoped_refptr<AudioInputController> AudioInputController::CreateLowLatency(
 
   // Create the AudioInputController object and ensure that it runs on
   // the audio-manager thread.
-  scoped_refptr<AudioInputController> controller(
-      new AudioInputController(event_handler, sync_writer, user_input_monitor));
+  scoped_refptr<AudioInputController> controller(new AudioInputController(
+      event_handler, sync_writer, user_input_monitor, agc_is_enabled));
   controller->task_runner_ = audio_manager->GetTaskRunner();
 
   // Create and open a new audio input stream from the existing
@@ -200,8 +225,8 @@ scoped_refptr<AudioInputController> AudioInputController::CreateForStream(
 
   // Create the AudioInputController object and ensure that it runs on
   // the audio-manager thread.
-  scoped_refptr<AudioInputController> controller(
-      new AudioInputController(event_handler, sync_writer, user_input_monitor));
+  scoped_refptr<AudioInputController> controller(new AudioInputController(
+      event_handler, sync_writer, user_input_monitor, false));
   controller->task_runner_ = task_runner;
 
   // TODO(miu): See TODO at top of file.  Until that's resolved, we need to
@@ -238,11 +263,6 @@ void AudioInputController::SetVolume(double volume) {
       &AudioInputController::DoSetVolume, this, volume));
 }
 
-void AudioInputController::SetAutomaticGainControl(bool enabled) {
-  task_runner_->PostTask(FROM_HERE, base::Bind(
-      &AudioInputController::DoSetAutomaticGainControl, this, enabled));
-}
-
 void AudioInputController::DoCreate(AudioManager* audio_manager,
                                     const AudioParameters& params,
                                     const std::string& device_id) {
@@ -252,15 +272,10 @@ void AudioInputController::DoCreate(AudioManager* audio_manager,
     handler_->OnLog(this, "AIC::DoCreate");
 
 #if defined(AUDIO_POWER_MONITORING)
-  // Create the audio (power) level meter given the provided audio parameters.
-  // An AudioBus is also needed to wrap the raw data buffer from the native
-  // layer to match AudioPowerMonitor::Scan().
-  // TODO(henrika): Remove use of extra AudioBus. See http://crbug.com/375155.
+  // Disable power monitoring for streams that run without AGC enabled to
+  // avoid adding logs and UMA for non-WebRTC clients.
+  power_measurement_is_enabled_ = agc_is_enabled_;
   last_audio_level_log_time_ = base::TimeTicks::Now();
-  audio_level_.reset(new media::AudioPowerMonitor(
-      params.sample_rate(),
-      TimeDelta::FromMilliseconds(kPowerMeasurementTimeConstantMilliseconds)));
-  audio_params_ = params;
   silence_state_ = SILENCE_STATE_NO_MEASUREMENT;
 #endif
 
@@ -312,6 +327,10 @@ void AudioInputController::DoCreateForStream(
 
   DCHECK(!no_data_timer_.get());
 
+  // Set AGC state using mode in |agc_is_enabled_| which can only be enabled in
+  // CreateLowLatency().
+  stream_->SetAutomaticGainControl(agc_is_enabled_);
+
   // Create the data timer which will call FirstCheckForNoData(). The timer
   // is started in DoRecord() and restarted in each DoCheckForNoData()
   // callback.
@@ -434,17 +453,6 @@ void AudioInputController::DoSetVolume(double volume) {
   stream_->SetVolume(max_volume_ * volume);
 }
 
-void AudioInputController::DoSetAutomaticGainControl(bool enabled) {
-  DCHECK(task_runner_->BelongsToCurrentThread());
-  DCHECK_NE(state_, RECORDING);
-
-  // Ensure that the AGC state only can be modified before streaming starts.
-  if (state_ != CREATED)
-    return;
-
-  stream_->SetAutomaticGainControl(enabled);
-}
-
 void AudioInputController::FirstCheckForNoData() {
   DCHECK(task_runner_->BelongsToCurrentThread());
   LogCaptureStartupResult(GetDataIsActive() ?
@@ -511,23 +519,17 @@ void AudioInputController::OnData(AudioInputStream* stream,
     sync_writer_->UpdateRecordedBytes(hardware_delay_bytes);
 
 #if defined(AUDIO_POWER_MONITORING)
-    // Only do power-level measurements if an AudioPowerMonitor object has
-    // been created. Done in DoCreate() but not DoCreateForStream(), hence
-    // logging will mainly be done for WebRTC and WebSpeech clients.
-    if (!audio_level_)
+    // Only do power-level measurements if DoCreate() has been called. It will
+    // ensure that logging will mainly be done for WebRTC and WebSpeech
+    // clients.
+    if (!power_measurement_is_enabled_)
       return;
 
     // Perform periodic audio (power) level measurements.
     if ((base::TimeTicks::Now() - last_audio_level_log_time_).InSeconds() >
         kPowerMonitorLogIntervalSeconds) {
-      // Wrap data into an AudioBus to match AudioPowerMonitor::Scan.
-      // TODO(henrika): remove this section when capture side uses AudioBus.
-      // See http://crbug.com/375155 for details.
-      audio_level_->Scan(*source, source->frames());
-
-      // Get current average power level and add it to the log.
-      // Possible range is given by [-inf, 0] dBFS.
-      std::pair<float, bool> result = audio_level_->ReadCurrentPowerAndClip();
+      // Calculate the average power of the signal, or the energy per sample.
+      const float average_power_dbfs = AveragePower(*source);
 
       // Add current microphone volume to log and UMA histogram.
       const int mic_volume_percent = static_cast<int>(100.0 * volume);
@@ -537,13 +539,10 @@ void AudioInputController::OnData(AudioInputStream* stream,
       task_runner_->PostTask(FROM_HERE,
                              base::Bind(&AudioInputController::DoLogAudioLevels,
                                         this,
-                                        result.first,
+                                        average_power_dbfs,
                                         mic_volume_percent));
 
       last_audio_level_log_time_ = base::TimeTicks::Now();
-
-      // Reset the average power level (since we don't log continuously).
-      audio_level_->Reset();
     }
 #endif
     return;