Bug 1176218 - Use MediaContentType in DecoderTraits - r?jya draft
authorGerald Squelart <gsquelart@mozilla.com>
Sat, 01 Oct 2016 18:09:50 +1000
changeset 419882 a2a2242d6b542a3815e24b429097ea8bd4fd8cf4
parent 419881 2dd548a046ad0195bfb9bcc486261b74d79b5c07
child 419883 41d3cee42a88e470ac078d1a6f7baaad24ece90c
push id31034
push usergsquelart@mozilla.com
push dateSat, 01 Oct 2016 12:07:57 +0000
reviewersjya
bugs1176218
milestone52.0a1
Bug 1176218 - Use MediaContentType in DecoderTraits - r?jya MozReview-Commit-ID: G0Blu5qOpwD
dom/media/DecoderTraits.cpp
--- a/dom/media/DecoderTraits.cpp
+++ b/dom/media/DecoderTraits.cpp
@@ -1,15 +1,16 @@
 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
 /* 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/. */
 
 #include "DecoderTraits.h"
+#include "MediaContentType.h"
 #include "MediaDecoder.h"
 #include "nsCharSeparatedTokenizer.h"
 #include "nsMimeTypes.h"
 #include "mozilla/Preferences.h"
 #include "mozilla/Telemetry.h"
 
 #include "OggDecoder.h"
 #include "OggReader.h"
@@ -302,93 +303,92 @@ static bool
 IsFlacSupportedType(const nsACString& aType,
                    const nsAString& aCodecs = EmptyString())
 {
   return FlacDecoder::CanHandleMediaType(aType, aCodecs);
 }
 
 static
 CanPlayStatus
-CanHandleCodecsType(const char* aMIMEType,
-                    const nsAString& aRequestedCodecs,
+CanHandleCodecsType(const MediaContentType& aType,
                     DecoderDoctorDiagnostics* aDiagnostics)
 {
   char const* const* codecList = nullptr;
-  if (IsOggTypeAndEnabled(nsDependentCString(aMIMEType))) {
-    if (IsOggSupportedType(nsDependentCString(aMIMEType), aRequestedCodecs)) {
+  if (IsOggTypeAndEnabled(aType.MimeType())) {
+    if (IsOggSupportedType(aType.MimeType(), aType.Codecs())) {
       return CANPLAY_YES;
     } else {
       // We can only reach this position if a particular codec was requested,
       // ogg is supported and working: the codec must be invalid.
       return CANPLAY_NO;
     }
   }
-  if (IsWaveType(nsDependentCString(aMIMEType))) {
+  if (IsWaveType(aType.MimeType())) {
     codecList = gWaveCodecs;
   }
 #if !defined(MOZ_OMX_WEBM_DECODER)
-  if (DecoderTraits::IsWebMTypeAndEnabled(nsDependentCString(aMIMEType))) {
-    if (IsWebMSupportedType(nsDependentCString(aMIMEType), aRequestedCodecs)) {
+  if (DecoderTraits::IsWebMTypeAndEnabled(aType.MimeType())) {
+    if (IsWebMSupportedType(aType.MimeType(), aType.Codecs())) {
       return CANPLAY_YES;
     } else {
       // We can only reach this position if a particular codec was requested,
       // webm is supported and working: the codec must be invalid.
       return CANPLAY_NO;
     }
   }
 #endif
 #ifdef MOZ_FMP4
-  if (DecoderTraits::IsMP4TypeAndEnabled(nsDependentCString(aMIMEType), aDiagnostics)) {
-    if (IsMP4SupportedType(nsDependentCString(aMIMEType), aDiagnostics, aRequestedCodecs)) {
+  if (DecoderTraits::IsMP4TypeAndEnabled(aType.MimeType(), aDiagnostics)) {
+    if (IsMP4SupportedType(aType.MimeType(), aDiagnostics, aType.Codecs())) {
       return CANPLAY_YES;
     } else {
       // We can only reach this position if a particular codec was requested,
       // fmp4 is supported and working: the codec must be invalid.
       return CANPLAY_NO;
     }
   }
 #endif
-  if (IsMP3SupportedType(nsDependentCString(aMIMEType), aRequestedCodecs)) {
+  if (IsMP3SupportedType(aType.MimeType(), aType.Codecs())) {
     return CANPLAY_YES;
   }
-  if (IsAACSupportedType(nsDependentCString(aMIMEType), aRequestedCodecs)) {
+  if (IsAACSupportedType(aType.MimeType(), aType.Codecs())) {
     return CANPLAY_YES;
   }
-  if (IsFlacSupportedType(nsDependentCString(aMIMEType), aRequestedCodecs)) {
+  if (IsFlacSupportedType(aType.MimeType(), aType.Codecs())) {
     return CANPLAY_YES;
   }
 #ifdef MOZ_OMX_DECODER
-  if (IsOmxSupportedType(nsDependentCString(aMIMEType))) {
-    if (nsDependentCString(aMIMEType).EqualsASCII("audio/mpeg")) {
+  if (IsOmxSupportedType(aType.MimeType())) {
+    if (aType.MimeType().EqualsASCII("audio/mpeg")) {
       codecList = gMpegAudioCodecs;
 #ifdef MOZ_OMX_WEBM_DECODER
-    } else if (nsDependentCString(aMIMEType).EqualsASCII("audio/webm") ||
-               nsDependentCString(aMIMEType).EqualsASCII("video/webm")) {
+    } else if (aType.MimeType().EqualsASCII("audio/webm") ||
+        aType.MimeType().EqualsASCII("video/webm")) {
       codecList = gOMXWebMCodecs;
 #endif
     } else {
       codecList = gH264Codecs;
     }
   }
 #endif
 #ifdef MOZ_DIRECTSHOW
-  DirectShowDecoder::GetSupportedCodecs(nsDependentCString(aMIMEType), &codecList);
+  DirectShowDecoder::GetSupportedCodecs(aType.MimeType(), &codecList);
 #endif
 #ifdef MOZ_ANDROID_OMX
   if (MediaDecoder::IsAndroidMediaPluginEnabled()) {
-    EnsureAndroidMediaPluginHost()->FindDecoder(nsDependentCString(aMIMEType), &codecList);
+    EnsureAndroidMediaPluginHost()->FindDecoder(aType.MimeType(), &codecList);
   }
 #endif
   if (!codecList) {
     return CANPLAY_MAYBE;
   }
 
   // See http://www.rfc-editor.org/rfc/rfc4281.txt for the description
   // of the 'codecs' parameter
-  nsCharSeparatedTokenizer tokenizer(aRequestedCodecs, ',');
+  nsCharSeparatedTokenizer tokenizer(aType.Codecs(), ',');
   bool expectMoreTokens = false;
   while (tokenizer.hasMoreTokens()) {
     const nsSubstring& token = tokenizer.nextToken();
 
     if (!CodecListContains(codecList, token)) {
       // Totally unsupported codec
       return CANPLAY_NO;
     }
@@ -399,71 +399,67 @@ CanHandleCodecsType(const char* aMIMETyp
     return CANPLAY_NO;
   }
 
   return CANPLAY_YES;
 }
 
 static
 CanPlayStatus
-CanHandleMediaType(const char* aMIMEType,
-                   bool aHaveRequestedCodecs,
-                   const nsAString& aRequestedCodecs,
+CanHandleMediaType(const MediaContentType& aType,
                    DecoderDoctorDiagnostics* aDiagnostics)
 {
   MOZ_ASSERT(NS_IsMainThread());
 
-  if (IsHttpLiveStreamingType(nsDependentCString(aMIMEType))) {
+  if (IsHttpLiveStreamingType(aType.MimeType())) {
     Telemetry::Accumulate(Telemetry::MEDIA_HLS_CANPLAY_REQUESTED, true);
   }
 
-  if (aHaveRequestedCodecs) {
-    CanPlayStatus result = CanHandleCodecsType(aMIMEType,
-                                               aRequestedCodecs,
-                                               aDiagnostics);
+  if (aType.HaveCodecs()) {
+    CanPlayStatus result = CanHandleCodecsType(aType, aDiagnostics);
     if (result == CANPLAY_NO || result == CANPLAY_YES) {
       return result;
     }
   }
-  if (IsOggTypeAndEnabled(nsDependentCString(aMIMEType))) {
+  if (IsOggTypeAndEnabled(aType.MimeType())) {
     return CANPLAY_MAYBE;
   }
-  if (IsWaveType(nsDependentCString(aMIMEType))) {
+  if (IsWaveType(aType.MimeType())) {
     return CANPLAY_MAYBE;
   }
-  if (DecoderTraits::IsMP4TypeAndEnabled(nsDependentCString(aMIMEType), aDiagnostics)) {
+  if (DecoderTraits::IsMP4TypeAndEnabled(aType.MimeType(), aDiagnostics)) {
     return CANPLAY_MAYBE;
   }
 #if !defined(MOZ_OMX_WEBM_DECODER)
-  if (DecoderTraits::IsWebMTypeAndEnabled(nsDependentCString(aMIMEType))) {
+  if (DecoderTraits::IsWebMTypeAndEnabled(aType.MimeType())) {
     return CANPLAY_MAYBE;
   }
 #endif
-  if (IsMP3SupportedType(nsDependentCString(aMIMEType))) {
+  if (IsMP3SupportedType(aType.MimeType())) {
     return CANPLAY_MAYBE;
   }
-  if (IsAACSupportedType(nsDependentCString(aMIMEType))) {
+  if (IsAACSupportedType(aType.MimeType())) {
     return CANPLAY_MAYBE;
   }
-  if (IsFlacSupportedType(nsDependentCString(aMIMEType))) {
+  if (IsFlacSupportedType(aType.MimeType())) {
     return CANPLAY_MAYBE;
   }
 #ifdef MOZ_OMX_DECODER
-  if (IsOmxSupportedType(nsDependentCString(aMIMEType))) {
+  if (IsOmxSupportedType(aType.MimeType())) {
     return CANPLAY_MAYBE;
   }
 #endif
 #ifdef MOZ_DIRECTSHOW
-  if (DirectShowDecoder::GetSupportedCodecs(nsDependentCString(aMIMEType), nullptr)) {
+  if (DirectShowDecoder::GetSupportedCodecs(aType.MimeType(), nullptr)) {
     return CANPLAY_MAYBE;
   }
 #endif
 #ifdef MOZ_ANDROID_OMX
   if (MediaDecoder::IsAndroidMediaPluginEnabled() &&
-      EnsureAndroidMediaPluginHost()->FindDecoder(nsDependentCString(aMIMEType), nullptr)) {
+      EnsureAndroidMediaPluginHost()->FindDecoder(aType.MimeType(), nullptr)) {
     return CANPLAY_MAYBE;
   }
 #endif
   return CANPLAY_NO;
 }
 
 /* static */
 CanPlayStatus
@@ -474,30 +470,23 @@ DecoderTraits::CanHandleContentType(cons
   return CanHandleContentType(parser, aDiagnostics);
 }
 
 /* static */
 CanPlayStatus
 DecoderTraits::CanHandleContentType(nsContentTypeParser& aParsedType,
                                     DecoderDoctorDiagnostics* aDiagnostics)
 {
-  nsAutoString mimeType;
-  nsresult rv = aParsedType.GetType(mimeType);
-  if (NS_FAILED(rv)) {
+  MediaContentType type(aParsedType);
+  if (type.MimeType().IsEmpty()) {
+    // Failed parsing.
     return CANPLAY_NO;
   }
 
-  nsAutoString codecs;
-  rv = aParsedType.GetParameter("codecs", codecs);
-
-  NS_ConvertUTF16toUTF8 mimeTypeUTF8(mimeType);
-  return CanHandleMediaType(mimeTypeUTF8.get(),
-                            NS_SUCCEEDED(rv),
-                            codecs,
-                            aDiagnostics);
+  return CanHandleMediaType(type, aDiagnostics);
 }
 
 /* static */
 bool DecoderTraits::ShouldHandleMediaType(const char* aMIMEType,
                                           DecoderDoctorDiagnostics* aDiagnostics)
 {
   if (IsWaveType(nsDependentCString(aMIMEType))) {
     // We should not return true for Wave types, since there are some
@@ -514,17 +503,18 @@ bool DecoderTraits::ShouldHandleMediaTyp
   if (nsDependentCString(aMIMEType).EqualsASCII("video/quicktime")) {
     RefPtr<nsPluginHost> pluginHost = nsPluginHost::GetInst();
     if (pluginHost &&
         pluginHost->HavePluginForType(nsDependentCString(aMIMEType))) {
       return false;
     }
   }
 
-  return CanHandleMediaType(aMIMEType, false, EmptyString(), aDiagnostics)
+  MediaContentType parsed{nsDependentCString(aMIMEType)};
+  return CanHandleMediaType(parsed, aDiagnostics)
          != CANPLAY_NO;
 }
 
 // Instantiates but does not initialize decoder.
 static
 already_AddRefed<MediaDecoder>
 InstantiateDecoder(const nsACString& aType,
                    MediaDecoderOwner* aOwner,