Bug 1568058 - pt 4 - Use batch decoding in MediaBufferDecoder. r=jya
authorMichael Froman <mfroman@mozilla.com>
Thu, 14 Nov 2019 16:06:54 +0000
changeset 502088 db32a8eb28a2d053f009765785bac23f4f9f6d3f
parent 502087 53a35e1379d64cb80fe1df622afb9aa8020dbcf1
child 502089 7a28c229e654d10ca9ab630e42753954390e67a7
push id114172
push userdluca@mozilla.com
push dateTue, 19 Nov 2019 11:31:10 +0000
treeherdermozilla-inbound@b5c5ba07d3db [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersjya
bugs1568058
milestone72.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 1568058 - pt 4 - Use batch decoding in MediaBufferDecoder. r=jya - Add a pref for controlling the batch size when doing webaudio decoding on RDD. - If batch size is greater than 1 and the decoder is capable of batch decoding, send raw sample batches to decoder. Differential Revision: https://phabricator.services.mozilla.com/D51454
dom/media/webaudio/MediaBufferDecoder.cpp
modules/libpref/init/StaticPrefList.yaml
--- a/dom/media/webaudio/MediaBufferDecoder.cpp
+++ b/dom/media/webaudio/MediaBufferDecoder.cpp
@@ -5,16 +5,17 @@
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 #include "MediaBufferDecoder.h"
 #include "mozilla/dom/AudioContextBinding.h"
 #include "mozilla/dom/BaseAudioContextBinding.h"
 #include "mozilla/dom/DOMException.h"
 #include "mozilla/dom/ScriptSettings.h"
 #include "mozilla/AbstractThread.h"
+#include "mozilla/StaticPrefs_media.h"
 #include <speex/speex_resampler.h>
 #include "nsXPCOMCIDInternal.h"
 #include "nsComponentManagerUtils.h"
 #include "MediaQueue.h"
 #include "BufferMediaResource.h"
 #include "DecoderTraits.h"
 #include "AudioContext.h"
 #include "AudioBuffer.h"
@@ -74,16 +75,17 @@ enum class PhaseEnum : int { Decode, All
 class MediaDecodeTask final : public Runnable {
  public:
   MediaDecodeTask(const MediaContainerType& aContainerType, uint8_t* aBuffer,
                   uint32_t aLength, WebAudioDecodeJob& aDecodeJob)
       : Runnable("MediaDecodeTask"),
         mContainerType(aContainerType),
         mBuffer(aBuffer),
         mLength(aLength),
+        mBatchSize(StaticPrefs::media_rdd_webaudio_batch_size()),
         mDecodeJob(aDecodeJob),
         mPhase(PhaseEnum::Decode) {
     MOZ_ASSERT(aBuffer);
     MOZ_ASSERT(NS_IsMainThread());
   }
 
   // MOZ_CAN_RUN_SCRIPT_BOUNDARY until Runnable::Run is MOZ_CAN_RUN_SCRIPT.  See
   // bug 1535398.
@@ -149,17 +151,18 @@ class MediaDecodeTask final : public Run
     mTrackDemuxer = nullptr;
     mDemuxer = nullptr;
     mPDecoderTaskQueue = nullptr;
   }
 
  private:
   MediaContainerType mContainerType;
   uint8_t* mBuffer;
-  uint32_t mLength;
+  const uint32_t mLength;
+  const uint32_t mBatchSize;
   WebAudioDecodeJob& mDecodeJob;
   PhaseEnum mPhase;
   RefPtr<TaskQueue> mPDecoderTaskQueue;
   RefPtr<MediaDataDemuxer> mDemuxer;
   RefPtr<MediaTrackDemuxer> mTrackDemuxer;
   RefPtr<MediaDataDecoder> mDecoder;
   nsTArray<RefPtr<MediaRawData>> mRawSamples;
   MediaInfo mMediaInfo;
@@ -306,19 +309,20 @@ void MediaDecodeTask::OnInitDecoderFaile
 
   ShutdownDecoder();
   ReportFailureOnMainThread(WebAudioDecodeJob::InvalidContent);
 }
 
 void MediaDecodeTask::DoDemux() {
   MOZ_ASSERT(OnPDecoderTaskQueue());
 
-  mTrackDemuxer->GetSamples(1)->Then(PDecoderTaskQueue(), __func__, this,
-                                     &MediaDecodeTask::OnAudioDemuxCompleted,
-                                     &MediaDecodeTask::OnAudioDemuxFailed);
+  mTrackDemuxer->GetSamples(mBatchSize)
+      ->Then(PDecoderTaskQueue(), __func__, this,
+             &MediaDecodeTask::OnAudioDemuxCompleted,
+             &MediaDecodeTask::OnAudioDemuxFailed);
 }
 
 void MediaDecodeTask::OnAudioDemuxCompleted(
     RefPtr<MediaTrackDemuxer::SamplesHolder> aSamples) {
   MOZ_ASSERT(OnPDecoderTaskQueue());
 
   mRawSamples.AppendElements(aSamples->GetSamples());
 
@@ -339,23 +343,39 @@ void MediaDecodeTask::OnAudioDemuxFailed
 void MediaDecodeTask::DoDecode() {
   MOZ_ASSERT(OnPDecoderTaskQueue());
 
   if (mRawSamples.IsEmpty()) {
     DoDrain();
     return;
   }
 
-  RefPtr<MediaRawData> sample = std::move(mRawSamples[0]);
+  if (mBatchSize > 1 && mDecoder->CanDecodeBatch()) {
+    nsTArray<RefPtr<MediaRawData>> rawSampleBatch;
+    const int batchSize = std::min((unsigned long)mBatchSize,
+                                   (unsigned long)mRawSamples.Length());
+    for (int i = 0; i < batchSize; ++i) {
+      rawSampleBatch.AppendElement(std::move(mRawSamples[i]));
+    }
 
-  mDecoder->Decode(sample)->Then(PDecoderTaskQueue(), __func__, this,
-                                 &MediaDecodeTask::OnAudioDecodeCompleted,
-                                 &MediaDecodeTask::OnAudioDecodeFailed);
+    mDecoder->DecodeBatch(std::move(rawSampleBatch))
+        ->Then(PDecoderTaskQueue(), __func__, this,
+               &MediaDecodeTask::OnAudioDecodeCompleted,
+               &MediaDecodeTask::OnAudioDecodeFailed);
 
-  mRawSamples.RemoveElementAt(0);
+    mRawSamples.RemoveElementsAt(0, batchSize);
+  } else {
+    RefPtr<MediaRawData> sample = std::move(mRawSamples[0]);
+
+    mDecoder->Decode(sample)->Then(PDecoderTaskQueue(), __func__, this,
+                                   &MediaDecodeTask::OnAudioDecodeCompleted,
+                                   &MediaDecodeTask::OnAudioDecodeFailed);
+
+    mRawSamples.RemoveElementAt(0);
+  }
 }
 
 void MediaDecodeTask::OnAudioDecodeCompleted(
     MediaDataDecoder::DecodedData&& aResults) {
   MOZ_ASSERT(OnPDecoderTaskQueue());
 
   for (auto&& sample : aResults) {
     MOZ_ASSERT(sample->mType == MediaData::Type::AUDIO_DATA);
--- a/modules/libpref/init/StaticPrefList.yaml
+++ b/modules/libpref/init/StaticPrefList.yaml
@@ -5823,16 +5823,21 @@
   value: false
 #elif defined(XP_MACOSX)
   value: true
 #else
   value: false
 #endif
   mirror: always
 
+- name: media.rdd-webaudio.batch.size
+  type: RelaxedAtomicInt32
+  value: 100
+  mirror: always
+
 #ifdef ANDROID
   # Enable the MediaCodec PlatformDecoderModule by default.
 -   name: media.android-media-codec.enabled
     type: RelaxedAtomicBool
     value: true
     mirror: always
 
 -   name: media.android-media-codec.preferred