Bug 1330284 - Use MediaContentType in CreateReader and MediaBufferDecoder - r?jya draft
authorGerald Squelart <gsquelart@mozilla.com>
Thu, 22 Dec 2016 11:57:48 +1100
changeset 460044 9c8e146cb88ce0e03cf2824120312c944e2ea90e
parent 460043 8de4988b0fe4adadcf73680819175768883d3d81
child 460045 388a82e668baf5ffe940f393b24fea53e8e9dc79
push id41348
push usergsquelart@mozilla.com
push dateThu, 12 Jan 2017 20:50:49 +0000
reviewersjya
bugs1330284
milestone53.0a1
Bug 1330284 - Use MediaContentType in CreateReader and MediaBufferDecoder - r?jya MozReview-Commit-ID: E9yVaxNdLad
dom/media/DecoderTraits.cpp
dom/media/DecoderTraits.h
dom/media/webaudio/MediaBufferDecoder.cpp
--- a/dom/media/DecoderTraits.cpp
+++ b/dom/media/DecoderTraits.cpp
@@ -365,57 +365,55 @@ DecoderTraits::CreateDecoder(const nsACS
   Maybe<MediaContentType> type = MakeMediaContentType(aType);
   if (!type) {
     return nullptr;
   }
   return InstantiateDecoder(*type, aOwner, aDiagnostics);
 }
 
 /* static */
-MediaDecoderReader* DecoderTraits::CreateReader(const nsACString& aType, AbstractMediaDecoder* aDecoder)
+MediaDecoderReader*
+DecoderTraits::CreateReader(const MediaContentType& aType,
+                            AbstractMediaDecoder* aDecoder)
 {
   MOZ_ASSERT(NS_IsMainThread());
   MediaDecoderReader* decoderReader = nullptr;
 
   if (!aDecoder) {
     return decoderReader;
   }
-  Maybe<MediaContentType> type = MakeMediaContentType(aType);
-  if (!type) {
-    return decoderReader;
-  }
 
 #ifdef MOZ_FMP4
-  if (MP4Decoder::IsSupportedType(*type,
+  if (MP4Decoder::IsSupportedType(aType,
                                   /* DecoderDoctorDiagnostics* */ nullptr)) {
     decoderReader = new MediaFormatReader(aDecoder, new MP4Demuxer(aDecoder->GetResource()));
   } else
 #endif
-  if (MP3Decoder::IsSupportedType(*type)) {
+  if (MP3Decoder::IsSupportedType(aType)) {
     decoderReader = new MediaFormatReader(aDecoder, new mp3::MP3Demuxer(aDecoder->GetResource()));
   } else
-  if (ADTSDecoder::IsSupportedType(*type)) {
+  if (ADTSDecoder::IsSupportedType(aType)) {
     decoderReader = new MediaFormatReader(aDecoder, new ADTSDemuxer(aDecoder->GetResource()));
   } else
-  if (WaveDecoder::IsSupportedType(*type)) {
+  if (WaveDecoder::IsSupportedType(aType)) {
     decoderReader = new MediaFormatReader(aDecoder, new WAVDemuxer(aDecoder->GetResource()));
   } else
-  if (FlacDecoder::IsSupportedType(*type)) {
+  if (FlacDecoder::IsSupportedType(aType)) {
     decoderReader = new MediaFormatReader(aDecoder, new FlacDemuxer(aDecoder->GetResource()));
   } else
-  if (OggDecoder::IsSupportedType(*type)) {
+  if (OggDecoder::IsSupportedType(aType)) {
     decoderReader = new MediaFormatReader(aDecoder, new OggDemuxer(aDecoder->GetResource()));
   } else
 #ifdef MOZ_ANDROID_OMX
   if (MediaDecoder::IsAndroidMediaPluginEnabled() &&
       EnsureAndroidMediaPluginHost()->FindDecoder(aType, nullptr)) {
-    decoderReader = new AndroidMediaReader(aDecoder, *type);
+    decoderReader = new AndroidMediaReader(aDecoder, aType);
   } else
 #endif
-  if (WebMDecoder::IsSupportedType(*type)) {
+  if (WebMDecoder::IsSupportedType(aType)) {
     decoderReader =
       new MediaFormatReader(aDecoder, new WebMDemuxer(aDecoder->GetResource()));
   } else
 #ifdef MOZ_DIRECTSHOW
   if (DirectShowDecoder::GetSupportedCodecs(*type, nullptr)) {
     decoderReader = new DirectShowReader(aDecoder);
   } else
 #endif
--- a/dom/media/DecoderTraits.h
+++ b/dom/media/DecoderTraits.h
@@ -43,17 +43,17 @@ public:
   // Create a decoder for the given aType. Returns null if we
   // were unable to create the decoder.
   static already_AddRefed<MediaDecoder> CreateDecoder(const nsACString& aType,
                                                       MediaDecoderOwner* aOwner,
                                                       DecoderDoctorDiagnostics* aDiagnostics);
 
   // Create a reader for thew given MIME type aType. Returns null
   // if we were unable to create the reader.
-  static MediaDecoderReader* CreateReader(const nsACString& aType,
+  static MediaDecoderReader* CreateReader(const MediaContentType& aType,
                                           AbstractMediaDecoder* aDecoder);
 
   // Returns true if MIME type aType is supported in video documents,
   // or false otherwise. Not all platforms support all MIME types, and
   // vice versa.
   static bool IsSupportedInVideoDocument(const nsACString& aType);
 
   // Convenience function that returns false if MOZ_FMP4 is not defined,
--- a/dom/media/webaudio/MediaBufferDecoder.cpp
+++ b/dom/media/webaudio/MediaBufferDecoder.cpp
@@ -13,16 +13,17 @@
 #include <speex/speex_resampler.h>
 #include "nsXPCOMCIDInternal.h"
 #include "nsComponentManagerUtils.h"
 #include "MediaDecoderReader.h"
 #include "BufferMediaResource.h"
 #include "DecoderTraits.h"
 #include "AudioContext.h"
 #include "AudioBuffer.h"
+#include "MediaContentType.h"
 #include "nsContentUtils.h"
 #include "nsIScriptObjectPrincipal.h"
 #include "nsIScriptError.h"
 #include "nsMimeTypes.h"
 #include "VideoUtils.h"
 #include "WebAudioUtils.h"
 #include "mozilla/dom/Promise.h"
 #include "mozilla/Telemetry.h"
@@ -93,17 +94,17 @@ enum class PhaseEnum : int
   Decode,
   AllocateBuffer,
   Done
 };
 
 class MediaDecodeTask final : public Runnable
 {
 public:
-  MediaDecodeTask(const char* aContentType, uint8_t* aBuffer,
+  MediaDecodeTask(const MediaContentType& aContentType, uint8_t* aBuffer,
                   uint32_t aLength,
                   WebAudioDecodeJob& aDecodeJob)
     : mContentType(aContentType)
     , mBuffer(aBuffer)
     , mLength(aLength)
     , mDecodeJob(aDecodeJob)
     , mPhase(PhaseEnum::Decode)
     , mFirstFrameDecoded(false)
@@ -147,17 +148,17 @@ private:
     // MediaDecoderReader expects that BufferDecoder is alive.
     // Destruct MediaDecoderReader first.
     mDecoderReader = nullptr;
     mBufferDecoder = nullptr;
     JS_free(nullptr, mBuffer);
   }
 
 private:
-  nsCString mContentType;
+  MediaContentType mContentType;
   uint8_t* mBuffer;
   uint32_t mLength;
   WebAudioDecodeJob& mDecodeJob;
   PhaseEnum mPhase;
   RefPtr<BufferDecoder> mBufferDecoder;
   RefPtr<MediaDecoderReader> mDecoderReader;
   MediaInfo mMediaInfo;
   MediaQueue<MediaData> mAudioQueue;
@@ -210,17 +211,17 @@ MediaDecodeTask::CreateReader()
   nsCOMPtr<nsIPrincipal> principal;
   nsCOMPtr<nsIScriptObjectPrincipal> sop = do_QueryInterface(mDecodeJob.mContext->GetParentObject());
   if (sop) {
     principal = sop->GetPrincipal();
   }
 
   RefPtr<BufferMediaResource> resource =
     new BufferMediaResource(static_cast<uint8_t*> (mBuffer),
-                            mLength, principal, mContentType);
+                            mLength, principal, mContentType.Type().AsString());
 
   MOZ_ASSERT(!mBufferDecoder);
   mBufferDecoder = new BufferDecoder(resource,
     new BufferDecoderGMPCrashHelper(mDecodeJob.mContext->GetParentObject()));
 
   // If you change this list to add support for new decoders, please consider
   // updating HTMLMediaElement::CreateDecoder as well.
 
@@ -290,17 +291,18 @@ MediaDecodeTask::OnMetadataRead(Metadata
     ReportFailureOnMainThread(WebAudioDecodeJob::NoAudio);
     return;
   }
 
   nsCString codec;
   if (!mMediaInfo.mAudio.GetAsAudioInfo()->mMimeType.IsEmpty()) {
     codec = nsPrintfCString("webaudio; %s", mMediaInfo.mAudio.GetAsAudioInfo()->mMimeType.get());
   } else {
-    codec = nsPrintfCString("webaudio;resource; %s", mContentType.get());
+    codec = nsPrintfCString("webaudio;resource; %s",
+                            mContentType.Type().AsString().Data());
   }
 
   nsCOMPtr<nsIRunnable> task = NS_NewRunnableFunction([codec]() -> void {
     MOZ_ASSERT(!codec.IsEmpty());
     MOZ_LOG(gMediaDecoderLog,
             LogLevel::Debug,
             ("Telemetry (WebAudio) MEDIA_CODEC_USED= '%s'", codec.get()));
     Telemetry::Accumulate(Telemetry::ID::MEDIA_CODEC_USED, codec);
@@ -496,31 +498,33 @@ WebAudioDecodeJob::AllocateBuffer()
                                 mBuffer.forget(), rv);
   return !rv.Failed();
 }
 
 void
 AsyncDecodeWebAudio(const char* aContentType, uint8_t* aBuffer,
                     uint32_t aLength, WebAudioDecodeJob& aDecodeJob)
 {
+  Maybe<MediaContentType> contentType = MakeMediaContentType(aContentType);
   // Do not attempt to decode the media if we were not successful at sniffing
   // the content type.
   if (!*aContentType ||
-      strcmp(aContentType, APPLICATION_OCTET_STREAM) == 0) {
+      strcmp(aContentType, APPLICATION_OCTET_STREAM) == 0 ||
+      !contentType) {
     nsCOMPtr<nsIRunnable> event =
       new ReportResultTask(aDecodeJob,
                            &WebAudioDecodeJob::OnFailure,
                            WebAudioDecodeJob::UnknownContent);
     JS_free(nullptr, aBuffer);
     NS_DispatchToMainThread(event);
     return;
   }
 
   RefPtr<MediaDecodeTask> task =
-    new MediaDecodeTask(aContentType, aBuffer, aLength, aDecodeJob);
+    new MediaDecodeTask(*contentType, aBuffer, aLength, aDecodeJob);
   if (!task->CreateReader()) {
     nsCOMPtr<nsIRunnable> event =
       new ReportResultTask(aDecodeJob,
                            &WebAudioDecodeJob::OnFailure,
                            WebAudioDecodeJob::UnknownError);
     NS_DispatchToMainThread(event);
   } else {
     // If we did this without a temporary: