Bug 1496496 - part1 : add IsAudible() to detect audible data. r=padenot
authoralwu <alwu@mozilla.com>
Thu, 11 Oct 2018 18:17:41 +0000
changeset 440794 0e9d8fcb66fe94d0293322cba3fbc80f5ecb6af7
parent 440793 16aba5eca40ab337711f931f3b3140eba02c4c6f
child 440795 28db447715bfed614404a8c87443e6e1c330de53
push id34834
push userncsoregi@mozilla.com
push dateFri, 12 Oct 2018 10:15:14 +0000
treeherdermozilla-central@8bd12e6c3f99 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerspadenot
bugs1496496
milestone64.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1496496 - part1 : add IsAudible() to detect audible data. r=padenot Add method to help us know whether audio block is audible or not, so that we won't show the sound indicator for silent web audio. Differential Revision: https://phabricator.services.mozilla.com/D7819
dom/media/AudioSegment.h
dom/media/webaudio/AudioBlock.h
dom/media/webaudio/AudioDestinationNode.cpp
--- a/dom/media/AudioSegment.h
+++ b/dom/media/AudioSegment.h
@@ -217,16 +217,34 @@ struct AudioChunk {
     mBufferFormat = AUDIO_FORMAT_SILENCE;
     mPrincipalHandle = PRINCIPAL_HANDLE_NONE;
   }
 
   size_t ChannelCount() const { return mChannelData.Length(); }
 
   bool IsMuted() const { return mVolume == 0.0f; }
 
+  bool IsAudible() const
+  {
+    for (auto&& channel : mChannelData) {
+      // Transform sound into dB RMS and assume that the value smaller than -100
+      // is inaudible.
+      float dbrms = 0.0;
+      for (uint32_t idx = 0; idx < mDuration; idx++) {
+        dbrms += std::pow(static_cast<const AudioDataValue*>(channel)[idx], 2);
+      }
+      dbrms /= mDuration;
+      dbrms = std::sqrt(dbrms) != 0.0 ? 20 * log10(dbrms) : -1000.0;
+      if (dbrms > -100.0) {
+        return true;
+      }
+    }
+    return false;
+  }
+
   size_t SizeOfExcludingThisIfUnshared(MallocSizeOf aMallocSizeOf) const
   {
     return SizeOfExcludingThis(aMallocSizeOf, true);
   }
 
   size_t SizeOfExcludingThis(MallocSizeOf aMallocSizeOf, bool aUnshared) const
   {
     size_t amount = 0;
--- a/dom/media/webaudio/AudioBlock.h
+++ b/dom/media/webaudio/AudioBlock.h
@@ -35,16 +35,17 @@ public:
     : AudioChunk(aChunk)
   {
     MOZ_ASSERT(aChunk.mDuration == WEBAUDIO_BLOCK_SIZE);
   }
   ~AudioBlock();
 
   using AudioChunk::GetDuration;
   using AudioChunk::IsNull;
+  using AudioChunk::IsAudible;
   using AudioChunk::ChannelCount;
   using AudioChunk::ChannelData;
   using AudioChunk::SizeOfExcludingThisIfUnshared;
   using AudioChunk::SizeOfExcludingThis;
   // mDuration is not exposed.  Use GetDuration().
   // mBuffer is not exposed.  Use SetBuffer().
   using AudioChunk::mChannelData;
   using AudioChunk::mVolume;
--- a/dom/media/webaudio/AudioDestinationNode.cpp
+++ b/dom/media/webaudio/AudioDestinationNode.cpp
@@ -251,17 +251,19 @@ public:
   {
     *aOutput = aInput;
     aOutput->mVolume *= mVolume;
 
     if (mSuspended) {
       return;
     }
 
-    bool newInputMuted = aInput.IsNull() || aInput.IsMuted();
+    bool newInputMuted = aInput.IsNull() ||
+                         aInput.IsMuted() ||
+                         !aInput.IsAudible();
     if (newInputMuted != mLastInputMuted) {
       mLastInputMuted = newInputMuted;
 
       RefPtr<InputMutedRunnable> runnable =
         new InputMutedRunnable(aStream, newInputMuted);
       aStream->Graph()->DispatchToMainThreadAfterStreamStateUpdate(
         runnable.forget());
     }