Bug 834172 - Implement CreateDecoder in DecoderTraits. r=cpearce
authorThomas Zimmermann <tdz@users.sourceforge.net>
Tue, 05 Mar 2013 09:56:34 -0500
changeset 123826 dd75eab24983ba50e14c6cc4a67b1fda2264bd1e
parent 123825 3003f5aa28d44f8aa42e3348becaed5e86e497c3
child 123827 dd63d49eb5dce4abf7513a43a8fc6e27c91b74f7
push id1401
push userpastithas@mozilla.com
push dateThu, 07 Mar 2013 07:26:45 +0000
treeherderfx-team@ee4879719f78 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerscpearce
bugs834172
milestone22.0a1
Bug 834172 - Implement CreateDecoder in DecoderTraits. r=cpearce The class DecoderTraits knows about all supported decoders. Adding an interface to create decoder instances simplifies all users of this functionality. nsHtmlMediaElement has been updated to use the new function.
content/html/content/public/nsHTMLMediaElement.h
content/html/content/src/nsHTMLMediaElement.cpp
content/media/DecoderTraits.cpp
content/media/DecoderTraits.h
--- a/content/html/content/public/nsHTMLMediaElement.h
+++ b/content/html/content/public/nsHTMLMediaElement.h
@@ -375,22 +375,16 @@ protected:
    * element. When aFinishWhenEnded is true, when this element ends playback
    * we will finish the stream and not play any more into it.
    * When aFinishWhenEnded is false, ending playback does not finish the stream.
    * The stream will never finish.
    */
   already_AddRefed<DOMMediaStream> CaptureStreamInternal(bool aFinishWhenEnded);
 
   /**
-   * Create a decoder for the given aMIMEType. Returns null if we
-   * were unable to create the decoder.
-   */
-  already_AddRefed<MediaDecoder> CreateDecoder(const nsACString& aMIMEType);
-
-  /**
    * Initialize a decoder as a clone of an existing decoder in another
    * element.
    * mLoadingSrc must already be set.
    */
   nsresult InitializeDecoderAsClone(MediaDecoder* aOriginal);
 
   /**
    * Initialize a decoder to load the given channel. The decoder's stream
--- a/content/html/content/src/nsHTMLMediaElement.cpp
+++ b/content/html/content/src/nsHTMLMediaElement.cpp
@@ -70,45 +70,16 @@
 
 #include "nsCSSParser.h"
 #include "nsIMediaList.h"
 
 #include "ImageContainer.h"
 #include "nsIPowerManagerService.h"
 #include <algorithm>
 
-#ifdef MOZ_OGG
-#include "OggDecoder.h"
-#endif
-#ifdef MOZ_WAVE
-#include "WaveDecoder.h"
-#endif
-#ifdef MOZ_WEBM
-#include "WebMDecoder.h"
-#endif
-#ifdef MOZ_RAW
-#include "RawDecoder.h"
-#endif
-#ifdef MOZ_GSTREAMER
-#include "GStreamerDecoder.h"
-#endif
-#ifdef MOZ_MEDIA_PLUGINS
-#include "MediaPluginHost.h"
-#include "MediaPluginDecoder.h"
-#endif
-#ifdef MOZ_WIDGET_GONK
-#include "MediaOmxDecoder.h"
-#endif
-#ifdef MOZ_DASH
-#include "DASHDecoder.h"
-#endif
-#ifdef MOZ_WMF
-#include "WMFDecoder.h"
-#endif
-
 #ifdef PR_LOGGING
 static PRLogModuleInfo* gMediaElementLog;
 static PRLogModuleInfo* gMediaElementEventsLog;
 #define LOG(type, msg) PR_LOG(gMediaElementLog, type, msg)
 #define LOG_EVENT(type, msg) PR_LOG(gMediaElementEventsLog, type, msg)
 #else
 #define LOG(type, msg)
 #define LOG_EVENT(type, msg)
@@ -2189,99 +2160,16 @@ nsHTMLMediaElement::CanPlayType(const ns
 
   LOG(PR_LOG_DEBUG, ("%p CanPlayType(%s) = \"%s\"", this,
                      NS_ConvertUTF16toUTF8(aType).get(),
                      NS_ConvertUTF16toUTF8(aResult).get()));
 
   return NS_OK;
 }
 
-already_AddRefed<MediaDecoder>
-nsHTMLMediaElement::CreateDecoder(const nsACString& aType)
-{
-  // If you change this list to add support for new decoders for codecs that
-  // can be used by <audio>, please consider updating MediaDecodeTask::CreateDecoder
-  // as well.
-
-#ifdef MOZ_GSTREAMER
-  if (DecoderTraits::IsGStreamerSupportedType(aType)) {
-    nsRefPtr<GStreamerDecoder> decoder = new GStreamerDecoder();
-    if (decoder->Init(this)) {
-      return decoder.forget();
-    }
-  }
-#endif
-#ifdef MOZ_RAW
-  if (DecoderTraits::IsRawType(aType)) {
-    nsRefPtr<RawDecoder> decoder = new RawDecoder();
-    if (decoder->Init(this)) {
-      return decoder.forget();
-    }
-  }
-#endif
-#ifdef MOZ_OGG
-  if (DecoderTraits::IsOggType(aType)) {
-    nsRefPtr<OggDecoder> decoder = new OggDecoder();
-    if (decoder->Init(this)) {
-      return decoder.forget();
-    }
-  }
-#endif
-#ifdef MOZ_WAVE
-  if (DecoderTraits::IsWaveType(aType)) {
-    nsRefPtr<WaveDecoder> decoder = new WaveDecoder();
-    if (decoder->Init(this)) {
-      return decoder.forget();
-    }
-  }
-#endif
-#ifdef MOZ_WIDGET_GONK
-  if (DecoderTraits::IsOmxSupportedType(aType)) {
-    nsRefPtr<MediaOmxDecoder> decoder = new MediaOmxDecoder();
-    if (decoder->Init(this)) {
-      return decoder.forget();
-    }
-  }
-#endif
-#ifdef MOZ_MEDIA_PLUGINS
-  if (MediaDecoder::IsMediaPluginsEnabled() && GetMediaPluginHost()->FindDecoder(aType, NULL)) {
-    nsRefPtr<MediaPluginDecoder> decoder = new MediaPluginDecoder(aType);
-    if (decoder->Init(this)) {
-      return decoder.forget();
-    }
-  }
-#endif
-#ifdef MOZ_WEBM
-  if (DecoderTraits::IsWebMType(aType)) {
-    nsRefPtr<WebMDecoder> decoder = new WebMDecoder();
-    if (decoder->Init(this)) {
-      return decoder.forget();
-    }
-  }
-#endif
-#ifdef MOZ_DASH
-  if (DecoderTraits::IsDASHMPDType(aType)) {
-    nsRefPtr<DASHDecoder> decoder = new DASHDecoder();
-    if (decoder->Init(this)) {
-      return decoder.forget();
-    }
-  }
-#endif
-#ifdef MOZ_WMF
-  if (DecoderTraits::IsWMFSupportedType(aType)) {
-    nsRefPtr<WMFDecoder> decoder = new WMFDecoder();
-    if (decoder->Init(this)) {
-      return decoder.forget();
-    }
-  }
-#endif
-
-  return nullptr;
-}
-
 nsresult nsHTMLMediaElement::InitializeDecoderAsClone(MediaDecoder* aOriginal)
 {
   NS_ASSERTION(mLoadingSrc, "mLoadingSrc must already be set");
   NS_ASSERTION(mDecoder == nullptr, "Shouldn't have a decoder");
 
   MediaResource* originalResource = aOriginal->GetResource();
   if (!originalResource)
     return NS_ERROR_FAILURE;
@@ -2318,17 +2206,17 @@ nsresult nsHTMLMediaElement::InitializeD
   NS_ASSERTION(mLoadingSrc, "mLoadingSrc must already be set");
   NS_ASSERTION(mDecoder == nullptr, "Shouldn't have a decoder");
 
   nsAutoCString mimeType;
 
   aChannel->GetContentType(mimeType);
   NS_ASSERTION(!mimeType.IsEmpty(), "We should have the Content-Type.");
 
-  nsRefPtr<MediaDecoder> decoder = CreateDecoder(mimeType);
+  nsRefPtr<MediaDecoder> decoder = DecoderTraits::CreateDecoder(mimeType, this);
   if (!decoder) {
     nsAutoString src;
     GetCurrentSrc(src);
     NS_ConvertUTF8toUTF16 mimeUTF16(mimeType);
     const PRUnichar* params[] = { mimeUTF16.get(), src.get() };
     ReportLoadError("MediaLoadUnsupportedMimeType", params, ArrayLength(params));
     return NS_ERROR_FAILURE;
   }
--- a/content/media/DecoderTraits.cpp
+++ b/content/media/DecoderTraits.cpp
@@ -12,16 +12,45 @@
 #endif
 #ifdef MOZ_GSTREAMER
 #include "mozilla/Preferences.h"
 #endif
 #ifdef MOZ_WMF
 #include "WMFDecoder.h"
 #endif
 
+#ifdef MOZ_OGG
+#include "OggDecoder.h"
+#endif
+#ifdef MOZ_WAVE
+#include "WaveDecoder.h"
+#endif
+#ifdef MOZ_WEBM
+#include "WebMDecoder.h"
+#endif
+#ifdef MOZ_RAW
+#include "RawDecoder.h"
+#endif
+#ifdef MOZ_GSTREAMER
+#include "GStreamerDecoder.h"
+#endif
+#ifdef MOZ_MEDIA_PLUGINS
+#include "MediaPluginHost.h"
+#include "MediaPluginDecoder.h"
+#endif
+#ifdef MOZ_WIDGET_GONK
+#include "MediaOmxDecoder.h"
+#endif
+#ifdef MOZ_DASH
+#include "DASHDecoder.h"
+#endif
+#ifdef MOZ_WMF
+#include "WMFDecoder.h"
+#endif
+
 namespace mozilla
 {
 
 template <class String>
 static bool
 CodecListContains(char const *const * aCodecs, const String& aCodec)
 {
   for (int32_t i = 0; aCodecs[i]; ++i) {
@@ -348,10 +377,68 @@ DecoderTraits::CanHandleMediaType(const 
   }
   if (expectMoreTokens) {
     // Last codec name was empty
     return CANPLAY_NO;
   }
   return CANPLAY_YES;
 }
 
+/* static */
+already_AddRefed<MediaDecoder>
+DecoderTraits::CreateDecoder(const nsACString& aType, MediaDecoderOwner* aOwner)
+{
+  nsRefPtr<MediaDecoder> decoder;
+
+#ifdef MOZ_GSTREAMER
+  if (IsGStreamerSupportedType(aType)) {
+    decoder = new GStreamerDecoder();
+  }
+#endif
+#ifdef MOZ_RAW
+  if (IsRawType(aType)) {
+    decoder = new RawDecoder();
+  }
+#endif
+#ifdef MOZ_OGG
+  if (IsOggType(aType)) {
+    decoder = new OggDecoder();
+  }
+#endif
+#ifdef MOZ_WAVE
+  if (IsWaveType(aType)) {
+    decoder = new WaveDecoder();
+  }
+#endif
+#ifdef MOZ_WIDGET_GONK
+  if (IsOmxSupportedType(aType)) {
+    decoder = new MediaOmxDecoder();
+  }
+#endif
+#ifdef MOZ_MEDIA_PLUGINS
+  if (MediaDecoder::IsMediaPluginsEnabled() && GetMediaPluginHost()->FindDecoder(aType, NULL)) {
+    decoder = new MediaPluginDecoder(aType);
+  }
+#endif
+#ifdef MOZ_WEBM
+  if (IsWebMType(aType)) {
+    decoder = new WebMDecoder();
+  }
+#endif
+#ifdef MOZ_DASH
+  if (IsDASHMPDType(aType)) {
+    decoder = new DASHDecoder();
+  }
+#endif
+#ifdef MOZ_WMF
+  if (IsWMFSupportedType(aType)) {
+    decoder = new WMFDecoder();
+  }
+#endif
+
+  NS_ENSURE_TRUE(decoder != nullptr, nullptr);
+  NS_ENSURE_TRUE(decoder->Init(aOwner), nullptr);
+
+  return decoder.forget();
 }
 
+}
+
--- a/content/media/DecoderTraits.h
+++ b/content/media/DecoderTraits.h
@@ -2,21 +2,25 @@
 /* vim:set ts=2 sw=2 sts=2 et cindent: */
 /* This Source Code Form is subject to the terms of the Mozilla Public
  * License, v. 2.0. If a copy of the MPL was not distributed with this
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 #ifndef DecoderTraits_h_
 #define DecoderTraits_h_
 
+#include "nsCOMPtr.h"
 #include "nsAString.h"
 
 namespace mozilla
 {
 
+class MediaDecoder;
+class MediaDecoderOwner;
+
 enum CanPlayStatus {
   CANPLAY_NO,
   CANPLAY_MAYBE,
   CANPLAY_YES
 };
 
 class DecoderTraits {
 public:
@@ -69,14 +73,19 @@ public:
 
 #ifdef MOZ_DASH
   static bool IsDASHMPDType(const nsACString& aType);
 #endif
 
 #ifdef MOZ_WMF
   static bool IsWMFSupportedType(const nsACString& aType);
 #endif
+
+  // 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);
 };
 
 }
 
 #endif