Bug 1274626 part 2 - implement NullDecodeModule; r?jya draft
authorKaku Kuo <tkuo@mozilla.com>
Thu, 14 Jul 2016 18:45:41 +0800
changeset 389962 193c83827a42e4332307a0d69425d7be6c79744b
parent 389961 80c174a6347034ae436747e4763d9244bad04967
child 389963 d76ffda966b3b179f19080e6cb7de743809f280f
push id23566
push usertkuo@mozilla.com
push dateWed, 20 Jul 2016 10:57:43 +0000
reviewersjya
bugs1274626
milestone50.0a1
Bug 1274626 part 2 - implement NullDecodeModule; r?jya MozReview-Commit-ID: By8h71EsXrg
dom/media/platforms/agnostic/BlankDecoderModule.cpp
--- a/dom/media/platforms/agnostic/BlankDecoderModule.cpp
+++ b/dom/media/platforms/agnostic/BlankDecoderModule.cpp
@@ -137,17 +137,16 @@ public:
 private:
   VideoInfo mInfo;
   gfx::IntRect mPicture;
   uint32_t mFrameWidth;
   uint32_t mFrameHeight;
   RefPtr<layers::ImageContainer> mImageContainer;
 };
 
-
 class BlankAudioDataCreator {
 public:
   BlankAudioDataCreator(uint32_t aChannelCount, uint32_t aSampleRate)
     : mFrameSum(0), mChannelCount(aChannelCount), mSampleRate(aSampleRate)
   {
   }
 
   MediaData* Create(const media::TimeUnit& aDTS,
@@ -188,39 +187,40 @@ public:
   }
 
 private:
   int64_t mFrameSum;
   uint32_t mChannelCount;
   uint32_t mSampleRate;
 };
 
+template<class AudioDataCreator, class VideoDataCreator>
 class BlankDecoderModule : public PlatformDecoderModule {
 public:
 
   // Decode thread.
   already_AddRefed<MediaDataDecoder>
   CreateVideoDecoder(const CreateDecoderParams& aParams) override {
     const VideoInfo& config = aParams.VideoConfig();
-    BlankVideoDataCreator* creator = new BlankVideoDataCreator(
+    VideoDataCreator* creator = new VideoDataCreator(
       config.mDisplay.width, config.mDisplay.height, aParams.mImageContainer);
     RefPtr<MediaDataDecoder> decoder =
-      new BlankMediaDataDecoder<BlankVideoDataCreator>(creator, aParams);
+      new BlankMediaDataDecoder<VideoDataCreator>(creator, aParams);
     return decoder.forget();
   }
 
   // Decode thread.
   already_AddRefed<MediaDataDecoder>
   CreateAudioDecoder(const CreateDecoderParams& aParams) override {
     const AudioInfo& config = aParams.AudioConfig();
-    BlankAudioDataCreator* creator = new BlankAudioDataCreator(
+    AudioDataCreator* creator = new AudioDataCreator(
       config.mChannels, config.mRate);
 
     RefPtr<MediaDataDecoder> decoder =
-      new BlankMediaDataDecoder<BlankAudioDataCreator>(creator, aParams);
+      new BlankMediaDataDecoder<AudioDataCreator>(creator, aParams);
     return decoder.forget();
   }
 
   bool
   SupportsMimeType(const nsACString& aMimeType,
                    DecoderDoctorDiagnostics* aDiagnostics) const override
   {
     return true;
@@ -231,13 +231,82 @@ public:
   {
     return kNeedNone;
   }
 
 };
 
 already_AddRefed<PlatformDecoderModule> CreateBlankDecoderModule()
 {
-  RefPtr<PlatformDecoderModule> pdm = new BlankDecoderModule();
+  RefPtr<PlatformDecoderModule> pdm =
+    new BlankDecoderModule<BlankAudioDataCreator, BlankVideoDataCreator>();
+  return pdm.forget();
+}
+
+class NullVideoDataCreator {
+public:
+  NullVideoDataCreator(uint32_t aFrameWidth, uint32_t aFrameHeight,
+                       layers::ImageContainer*)
+  {
+    mInfo.mDisplay = nsIntSize(aFrameWidth, aFrameHeight);
+  }
+
+  already_AddRefed<MediaData>
+  Create(const media::TimeUnit& aDTS, const media::TimeUnit& aDuration, int64_t aOffsetInStream)
+  {
+    return VideoData::Create(mInfo,
+                             nullptr,
+                             nullptr,
+                             aOffsetInStream,
+                             aDTS.ToMicroseconds(),
+                             aDuration.ToMicroseconds(),
+                             VideoData::YCbCrBuffer(),
+                             true,
+                             aDTS.ToMicroseconds(),
+                             gfx::IntRect());
+  }
+private:
+  VideoInfo mInfo;
+};
+
+class NullAudioDataCreator {
+public:
+  NullAudioDataCreator(uint32_t aChannelCount, uint32_t aSampleRate)
+    : mChannelCount(aChannelCount), mSampleRate(aSampleRate)
+  {
+  }
+
+  MediaData* Create(const media::TimeUnit& aDTS,
+                    const media::TimeUnit& aDuration,
+                    int64_t aOffsetInStream)
+  {
+    // Convert duration to frames. We add 1 to duration to account for
+    // rounding errors, so we get a consistent tone.
+    CheckedInt64 frames =
+      UsecsToFrames(aDuration.ToMicroseconds()+1, mSampleRate);
+    if (!frames.isValid() ||
+        !mChannelCount ||
+        !mSampleRate ||
+        frames.value() > (UINT32_MAX / mChannelCount)) {
+      return nullptr;
+    }
+    return new AudioData(aOffsetInStream,
+                         aDTS.ToMicroseconds(),
+                         aDuration.ToMicroseconds(),
+                         uint32_t(frames.value()),
+                         AlignedAudioBuffer(),
+                         mChannelCount,
+                         mSampleRate);
+  }
+
+private:
+  uint32_t mChannelCount;
+  uint32_t mSampleRate;
+};
+
+already_AddRefed<PlatformDecoderModule> CreateNullDecoderModule()
+{
+  RefPtr<PlatformDecoderModule> pdm =
+    new BlankDecoderModule<NullAudioDataCreator, NullVideoDataCreator>();
   return pdm.forget();
 }
 
 } // namespace mozilla