Bug 1329568 - MediaExtendedMIMEType - r=jya
authorGerald Squelart <gsquelart@mozilla.com>
Thu, 01 Dec 2016 13:05:45 +1100
changeset 374377 9a83e282245da18d40ab1cf66b97c9e19c398d45
parent 374376 47e7a628e4eb7a4e4209d4fce83387f9508927b0
child 374378 c38a9125908aeb17c9086287a8c081dffbd895bb
push id6996
push userjlorenzo@mozilla.com
push dateMon, 06 Mar 2017 20:48:21 +0000
treeherdermozilla-beta@d89512dab048 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersjya
bugs1329568
milestone53.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 1329568 - MediaExtendedMIMEType - r=jya This patch factors out all data handling of MIME strings from MediaContentType to MediaExtendedMIMEType. MediaExtendedMIMEType is pretty much a copy of the old MediaContentType, as the functionality was fine (but will be modified in upcoming patches). MediaContentType then just delegates the work to its embedded MediaExtendedMIMEType field. The main difference is that the default constructor and Populate() method have been replaced with a single constructor that takes all the arguments at once. MozReview-Commit-ID: GBAgPDT2DUW
dom/media/MediaContentType.cpp
dom/media/MediaContentType.h
dom/media/MediaMIMETypes.cpp
dom/media/MediaMIMETypes.h
dom/media/moz.build
--- a/dom/media/MediaContentType.cpp
+++ b/dom/media/MediaContentType.cpp
@@ -5,66 +5,24 @@
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 #include "MediaContentType.h"
 
 #include "nsContentTypeParser.h"
 
 namespace mozilla {
 
-static int32_t
-GetParameterAsNumber(const nsContentTypeParser& aParser,
-                     const char* aParameter,
-                     const int32_t aErrorReturn)
-{
-  nsAutoString parameterString;
-  nsresult rv = aParser.GetParameter(aParameter, parameterString);
-  if (NS_FAILED_impl(rv)) {
-    return aErrorReturn;
-  }
-  int32_t number = parameterString.ToInteger(&rv);
-  if (MOZ_UNLIKELY(NS_FAILED_impl(rv))) {
-    return aErrorReturn;
-  }
-  return number;
-}
-
-bool
-MediaContentType::Populate(const nsAString& aType)
-{
-  MOZ_ASSERT(NS_IsMainThread());
-
-  nsContentTypeParser parser(aType);
-  nsAutoString mime;
-  nsresult rv = parser.GetType(mime);
-  if (!NS_SUCCEEDED(rv) || mime.IsEmpty()) {
-    return false;
-  }
-
-  mMIMEType = NS_ConvertUTF16toUTF8(mime);
-
-  rv = parser.GetParameter("codecs", mCodecs);
-  mHaveCodecs = NS_SUCCEEDED(rv);
-
-  mWidth = GetParameterAsNumber(parser, "width", -1);
-  mHeight = GetParameterAsNumber(parser, "height", -1);
-  mFramerate = GetParameterAsNumber(parser, "framerate", -1);
-  mBitrate = GetParameterAsNumber(parser, "bitrate", -1);
-
-  return true;
-}
-
 Maybe<MediaContentType>
 MakeMediaContentType(const nsAString& aType)
 {
-  Maybe<MediaContentType> type{Some(MediaContentType{})};
-  if (!type->Populate(aType)) {
-    type.reset();
+  Maybe<MediaExtendedMIMEType> mime = MakeMediaExtendedMIMEType(aType);
+  if (mime) {
+    return Some(MediaContentType(Move(*mime)));
   }
-  return type;
+  return Nothing();
 }
 
 Maybe<MediaContentType>
 MakeMediaContentType(const nsACString& aType)
 {
   return MakeMediaContentType(NS_ConvertUTF8toUTF16(aType));
 }
 
--- a/dom/media/MediaContentType.h
+++ b/dom/media/MediaContentType.h
@@ -2,56 +2,53 @@
 /* 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 MediaContentType_h_
 #define MediaContentType_h_
 
+#include "MediaMIMETypes.h"
 #include "mozilla/Maybe.h"
 #include "nsString.h"
 
 namespace mozilla {
 
-// Structure containing pre-parsed content type parameters, e.g.:
-// MIME type, optional codecs, etc.
+// Class containing media type information for containers.
 class MediaContentType
 {
 public:
-  // MIME type. Guaranteed not to be empty.
-  const nsACString& GetMIMEType() const { return mMIMEType; }
+  explicit MediaContentType(const MediaExtendedMIMEType& aType)
+    : mExtendedMIMEType(aType)
+  {
+  }
+  explicit MediaContentType(MediaExtendedMIMEType&& aType)
+    : mExtendedMIMEType(Move(aType))
+  {
+  }
+
+  const MediaExtendedMIMEType& ExtendedType() const { return mExtendedMIMEType; }
+
+  // MIME "type/subtype". Guaranteed not to be empty.
+  const nsACString& GetMIMEType() const { return mExtendedMIMEType.Type(); }
 
   // Was there an explicit 'codecs' parameter provided?
-  bool HaveCodecs() const { return mHaveCodecs; }
+  bool HaveCodecs() const { return mExtendedMIMEType.HaveCodecs(); }
   // Codecs. May be empty if not provided or explicitly provided as empty.
-  const nsAString& GetCodecs() const { return mCodecs; }
+  const nsAString& GetCodecs() const { return mExtendedMIMEType.GetCodecs(); }
 
   // Sizes and rates.
-  Maybe<int32_t> GetWidth() const { return GetMaybeNumber(mWidth); }
-  Maybe<int32_t> GetHeight() const { return GetMaybeNumber(mHeight); }
-  Maybe<int32_t> GetFramerate() const { return GetMaybeNumber(mFramerate); }
-  Maybe<int32_t> GetBitrate() const { return GetMaybeNumber(mBitrate); }
+  Maybe<int32_t> GetWidth() const { return mExtendedMIMEType.GetWidth(); }
+  Maybe<int32_t> GetHeight() const { return mExtendedMIMEType.GetHeight(); }
+  Maybe<int32_t> GetFramerate() const { return mExtendedMIMEType.GetFramerate(); }
+  Maybe<int32_t> GetBitrate() const { return mExtendedMIMEType.GetBitrate(); }
 
 private:
-  friend Maybe<MediaContentType> MakeMediaContentType(const nsAString& aType);
-  bool Populate(const nsAString& aType);
-
-  Maybe<int32_t> GetMaybeNumber(int32_t aNumber) const
-  {
-    return (aNumber < 0) ? Maybe<int32_t>(Nothing()) : Some(int32_t(aNumber));
-  }
-
-  nsCString mMIMEType; // UTF8 MIME type.
-  bool mHaveCodecs; // If false, mCodecs must be empty.
-  nsString mCodecs;
-  int32_t mWidth; // -1 if not provided.
-  int32_t mHeight; // -1 if not provided.
-  int32_t mFramerate; // -1 if not provided.
-  int32_t mBitrate; // -1 if not provided.
+  MediaExtendedMIMEType mExtendedMIMEType;
 };
 
 Maybe<MediaContentType> MakeMediaContentType(const nsAString& aType);
 Maybe<MediaContentType> MakeMediaContentType(const nsACString& aType);
 Maybe<MediaContentType> MakeMediaContentType(const char* aType);
 
 } // namespace mozilla
 
copy from dom/media/MediaContentType.cpp
copy to dom/media/MediaMIMETypes.cpp
--- a/dom/media/MediaContentType.cpp
+++ b/dom/media/MediaMIMETypes.cpp
@@ -1,15 +1,15 @@
 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
 /* 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 "MediaContentType.h"
+#include "MediaMIMETypes.h"
 
 #include "nsContentTypeParser.h"
 
 namespace mozilla {
 
 static int32_t
 GetParameterAsNumber(const nsContentTypeParser& aParser,
                      const char* aParameter,
@@ -22,59 +22,68 @@ GetParameterAsNumber(const nsContentType
   }
   int32_t number = parameterString.ToInteger(&rv);
   if (MOZ_UNLIKELY(NS_FAILED_impl(rv))) {
     return aErrorReturn;
   }
   return number;
 }
 
-bool
-MediaContentType::Populate(const nsAString& aType)
+MediaExtendedMIMEType::MediaExtendedMIMEType(const nsACString& aMIMEType,
+                                             bool aHaveCodecs,
+                                             const nsAString& aCodecs,
+                                             int32_t aWidth, int32_t aHeight,
+                                             int32_t aFramerate, int32_t aBitrate)
+  : mMIMEType(Move(aMIMEType))
+  , mHaveCodecs(aHaveCodecs)
+  , mCodecs(Move(aCodecs))
+  , mWidth(aWidth)
+  , mHeight(aHeight)
+  , mFramerate(aFramerate)
+  , mBitrate(aBitrate)
+{
+}
+
+Maybe<MediaExtendedMIMEType>
+MakeMediaExtendedMIMEType(const nsAString& aType)
 {
   MOZ_ASSERT(NS_IsMainThread());
 
   nsContentTypeParser parser(aType);
   nsAutoString mime;
   nsresult rv = parser.GetType(mime);
   if (!NS_SUCCEEDED(rv) || mime.IsEmpty()) {
-    return false;
+    return Nothing();
   }
 
-  mMIMEType = NS_ConvertUTF16toUTF8(mime);
+  NS_ConvertUTF16toUTF8 mime8{mime};
 
-  rv = parser.GetParameter("codecs", mCodecs);
-  mHaveCodecs = NS_SUCCEEDED(rv);
+  nsAutoString codecs;
+  rv = parser.GetParameter("codecs", codecs);
+  bool haveCodecs = NS_SUCCEEDED(rv);
 
-  mWidth = GetParameterAsNumber(parser, "width", -1);
-  mHeight = GetParameterAsNumber(parser, "height", -1);
-  mFramerate = GetParameterAsNumber(parser, "framerate", -1);
-  mBitrate = GetParameterAsNumber(parser, "bitrate", -1);
+  int32_t width = GetParameterAsNumber(parser, "width", -1);
+  int32_t height = GetParameterAsNumber(parser, "height", -1);
+  int32_t framerate = GetParameterAsNumber(parser, "framerate", -1);
+  int32_t bitrate = GetParameterAsNumber(parser, "bitrate", -1);
 
-  return true;
+  return Some(MediaExtendedMIMEType(mime8,
+                                    haveCodecs, codecs,
+                                    width, height,
+                                    framerate, bitrate));
 }
 
-Maybe<MediaContentType>
-MakeMediaContentType(const nsAString& aType)
+Maybe<MediaExtendedMIMEType>
+MakeMediaExtendedMIMEType(const nsACString& aType)
 {
-  Maybe<MediaContentType> type{Some(MediaContentType{})};
-  if (!type->Populate(aType)) {
-    type.reset();
-  }
-  return type;
+  return MakeMediaExtendedMIMEType(NS_ConvertUTF8toUTF16(aType));
 }
 
-Maybe<MediaContentType>
-MakeMediaContentType(const nsACString& aType)
-{
-  return MakeMediaContentType(NS_ConvertUTF8toUTF16(aType));
-}
-
-Maybe<MediaContentType>
-MakeMediaContentType(const char* aType)
+Maybe<MediaExtendedMIMEType>
+MakeMediaExtendedMIMEType(const char* aType)
 {
   if (!aType) {
     return Nothing();
   }
-  return MakeMediaContentType(nsDependentCString(aType));
+  return MakeMediaExtendedMIMEType(nsDependentCString(aType));
 }
 
 } // namespace mozilla
copy from dom/media/MediaContentType.h
copy to dom/media/MediaMIMETypes.h
--- a/dom/media/MediaContentType.h
+++ b/dom/media/MediaMIMETypes.h
@@ -1,58 +1,61 @@
 /* -*- 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/. */
 
-#ifndef MediaContentType_h_
-#define MediaContentType_h_
+#ifndef MediaMIMETypes_h_
+#define MediaMIMETypes_h_
 
 #include "mozilla/Maybe.h"
 #include "nsString.h"
 
 namespace mozilla {
 
-// Structure containing pre-parsed content type parameters, e.g.:
-// MIME type, optional codecs, etc.
-class MediaContentType
+// Class containing pre-parsed media MIME type parameters, e.g.:
+// MIME type/subtype, optional codecs, etc.
+class MediaExtendedMIMEType
 {
 public:
-  // MIME type. Guaranteed not to be empty.
-  const nsACString& GetMIMEType() const { return mMIMEType; }
+  // MIME "type/subtype".
+  const nsACString& Type() const { return mMIMEType; }
 
   // Was there an explicit 'codecs' parameter provided?
   bool HaveCodecs() const { return mHaveCodecs; }
   // Codecs. May be empty if not provided or explicitly provided as empty.
   const nsAString& GetCodecs() const { return mCodecs; }
 
   // Sizes and rates.
   Maybe<int32_t> GetWidth() const { return GetMaybeNumber(mWidth); }
   Maybe<int32_t> GetHeight() const { return GetMaybeNumber(mHeight); }
   Maybe<int32_t> GetFramerate() const { return GetMaybeNumber(mFramerate); }
   Maybe<int32_t> GetBitrate() const { return GetMaybeNumber(mBitrate); }
 
 private:
-  friend Maybe<MediaContentType> MakeMediaContentType(const nsAString& aType);
-  bool Populate(const nsAString& aType);
+  friend Maybe<MediaExtendedMIMEType> MakeMediaExtendedMIMEType(const nsAString& aType);
+  MediaExtendedMIMEType(const nsACString& aMIMEType,
+                        bool aHaveCodecs, const nsAString& aCodecs,
+                        int32_t aWidth, int32_t aHeight,
+                        int32_t aFramerate, int32_t aBitrate);
 
   Maybe<int32_t> GetMaybeNumber(int32_t aNumber) const
   {
     return (aNumber < 0) ? Maybe<int32_t>(Nothing()) : Some(int32_t(aNumber));
   }
 
   nsCString mMIMEType; // UTF8 MIME type.
   bool mHaveCodecs; // If false, mCodecs must be empty.
   nsString mCodecs;
   int32_t mWidth; // -1 if not provided.
   int32_t mHeight; // -1 if not provided.
   int32_t mFramerate; // -1 if not provided.
   int32_t mBitrate; // -1 if not provided.
 };
 
-Maybe<MediaContentType> MakeMediaContentType(const nsAString& aType);
-Maybe<MediaContentType> MakeMediaContentType(const nsACString& aType);
-Maybe<MediaContentType> MakeMediaContentType(const char* aType);
+Maybe<MediaExtendedMIMEType> MakeMediaExtendedMIMEType(const nsAString& aType);
+Maybe<MediaExtendedMIMEType> MakeMediaExtendedMIMEType(const nsACString& aType);
+Maybe<MediaExtendedMIMEType> MakeMediaExtendedMIMEType(const char* aType);
 
 } // namespace mozilla
 
-#endif // MediaContentType_h_
+#endif // MediaMIMETypes_h_
--- a/dom/media/moz.build
+++ b/dom/media/moz.build
@@ -106,16 +106,17 @@ EXPORTS += [
     'MediaDecoder.h',
     'MediaDecoderOwner.h',
     'MediaDecoderReader.h',
     'MediaDecoderStateMachine.h',
     'MediaEventSource.h',
     'MediaFormatReader.h',
     'MediaInfo.h',
     'MediaMetadataManager.h',
+    'MediaMIMETypes.h',
     'MediaPrefs.h',
     'MediaQueue.h',
     'MediaRecorder.h',
     'MediaResource.h',
     'MediaResourceCallback.h',
     'MediaResult.h',
     'MediaSegment.h',
     'MediaStatistics.h',
@@ -213,16 +214,17 @@ UNIFIED_SOURCES += [
     'MediaDecoderReader.cpp',
     'MediaDecoderReaderWrapper.cpp',
     'MediaDecoderStateMachine.cpp',
     'MediaDeviceInfo.cpp',
     'MediaDevices.cpp',
     'MediaFormatReader.cpp',
     'MediaInfo.cpp',
     'MediaManager.cpp',
+    'MediaMIMETypes.cpp',
     'MediaPrefs.cpp',
     'MediaRecorder.cpp',
     'MediaResource.cpp',
     'MediaShutdownManager.cpp',
     'MediaStreamError.cpp',
     'MediaStreamGraph.cpp',
     'MediaStreamListener.cpp',
     'MediaStreamTrack.cpp',