Bug 1400674 - Fix the algorithm of filling audio gaps with silence. r=jya, a=ritu
authorJW Wang <jwwang@mozilla.com>
Fri, 13 Oct 2017 17:24:24 +0800
changeset 432595 413220444b15681a7f5af66514b4e66e9b434005
parent 432594 808585a48181544b0badc15f03711fd5d26d1269
child 432596 28f4ccb3c041da6497a98fbc9e291579d32c69fb
push id8003
push userryanvm@gmail.com
push dateWed, 18 Oct 2017 22:23:15 +0000
treeherdermozilla-beta@adc0384c565a [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersjya, ritu
bugs1400674
milestone57.0
Bug 1400674 - Fix the algorithm of filling audio gaps with silence. r=jya, a=ritu MozReview-Commit-ID: Hjsboq6PdlN
dom/media/mediasink/AudioSink.cpp
--- a/dom/media/mediasink/AudioSink.cpp
+++ b/dom/media/mediasink/AudioSink.cpp
@@ -419,38 +419,31 @@ AudioSink::NotifyAudioNeeded()
     if (missingFrames.value() > AUDIO_FUZZ_FRAMES) {
       // The next audio packet begins some time after the end of the last packet
       // we pushed to the audio hardware. We must push silence into the audio
       // hardware so that the next audio packet begins playback at the correct
       // time.
       missingFrames = std::min<int64_t>(INT32_MAX, missingFrames.value());
       mFramesParsed += missingFrames.value();
 
-      // We need to calculate how many frames are missing at the output rate.
-      missingFrames =
-        SaferMultDiv(missingFrames.value(), mOutputRate, data->mRate);
-      if (!missingFrames.isValid()) {
-        NS_WARNING("Int overflow in AudioSink");
-        mErrored = true;
-        return;
+      RefPtr<AudioData> silenceData;
+      AlignedAudioBuffer silenceBuffer(missingFrames.value() * data->mChannels);
+       if (!silenceBuffer) {
+         NS_WARNING("OOM in AudioSink");
+         mErrored = true;
+         return;
+       }
+      if (mConverter->InputConfig() != mConverter->OutputConfig()) {
+        AlignedAudioBuffer convertedData =
+          mConverter->Process(AudioSampleBuffer(Move(silenceBuffer))).Forget();
+        silenceData = CreateAudioFromBuffer(Move(convertedData), data);
+      } else {
+        silenceData = CreateAudioFromBuffer(Move(silenceBuffer), data);
       }
-
-      // We need to insert silence, first use drained frames if any.
-      missingFrames -= DrainConverter(missingFrames.value());
-      // Insert silence if still needed.
-      if (missingFrames.value()) {
-        AlignedAudioBuffer silenceData(missingFrames.value() * mOutputChannels);
-        if (!silenceData) {
-          NS_WARNING("OOM in AudioSink");
-          mErrored = true;
-          return;
-        }
-        RefPtr<AudioData> silence = CreateAudioFromBuffer(Move(silenceData), data);
-        PushProcessedAudio(silence);
-      }
+      PushProcessedAudio(silenceData);
     }
 
     mLastEndTime = data->GetEndTime();
     mFramesParsed += data->mFrames;
 
     if (mConverter->InputConfig() != mConverter->OutputConfig()) {
       // We must ensure that the size in the buffer contains exactly the number
       // of frames, in case one of the audio producer over allocated the buffer.
@@ -481,17 +474,17 @@ AudioSink::PushProcessedAudio(AudioData*
   }
   mProcessedQueue.Push(aData);
   mProcessedQueueLength += FramesToUsecs(aData->mFrames, mOutputRate).value();
   return aData->mFrames;
 }
 
 already_AddRefed<AudioData>
 AudioSink::CreateAudioFromBuffer(AlignedAudioBuffer&& aBuffer,
-                                            AudioData* aReference)
+                                 AudioData* aReference)
 {
   uint32_t frames = aBuffer.Length() / mOutputChannels;
   if (!frames) {
     return nullptr;
   }
   auto duration = FramesToTimeUnit(frames, mOutputRate);
   if (!duration.IsValid()) {
     NS_WARNING("Int overflow in AudioSink");