Bug 1329568 - MediaCodecs - r?jya draft
authorGerald Squelart <gsquelart@mozilla.com>
Wed, 28 Dec 2016 10:48:27 +1100
changeset 460522 0af34d201b9e33dcde63f4f678127de873ec6a6a
parent 460521 f992ece1d80d17130a671183855206ae038c2905
child 460523 e378124fded2ec917c2306874ca06351d6b107b3
push id41406
push usergsquelart@mozilla.com
push dateFri, 13 Jan 2017 06:23:59 +0000
reviewersjya
bugs1329568
milestone53.0a1
Bug 1329568 - MediaCodecs - r?jya MediaCodecs factors out the codecs string from MediaExtendedMIMEType. It also provides utility methods to go through a list of codecs, and test the presence of specific codecs. Note that there is no real way (yet?) to validate the given codecs strings, we just assume that it's a comma-separated list of codecs. Further work can be done later on if useful. MozReview-Commit-ID: 5n2nWmaNT2O
dom/media/MediaContentType.h
dom/media/MediaMIMETypes.cpp
dom/media/MediaMIMETypes.h
--- a/dom/media/MediaContentType.h
+++ b/dom/media/MediaContentType.h
@@ -36,17 +36,17 @@ public:
   const MediaExtendedMIMEType& ExtendedType() const { return mExtendedMIMEType; }
 
   // MIME "type/subtype". Guaranteed not to be empty.
   const nsACString& GetMIMEType() const { return mExtendedMIMEType.Type().AsString(); }
 
   // Was there an explicit 'codecs' parameter provided?
   bool HaveCodecs() const { return mExtendedMIMEType.HaveCodecs(); }
   // Codecs. May be empty if not provided or explicitly provided as empty.
-  const nsAString& GetCodecs() const { return mExtendedMIMEType.GetCodecs(); }
+  const nsAString& GetCodecs() const { return mExtendedMIMEType.Codecs().AsString(); }
 
   // Sizes and rates.
   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:
--- a/dom/media/MediaMIMETypes.cpp
+++ b/dom/media/MediaMIMETypes.cpp
@@ -73,16 +73,38 @@ Maybe<MediaMIMEType>
 MakeMediaMIMEType(const char* aType)
 {
   if (!aType) {
     return Nothing();
   }
   return MakeMediaMIMEType(nsDependentCString(aType));
 }
 
+bool
+MediaCodecs::Contains(const nsAString& aCodec) const
+{
+  for (const auto& myCodec : Range()) {
+    if (myCodec == aCodec) {
+      return true;
+    }
+  }
+  return false;
+}
+
+bool
+MediaCodecs::ContainsAll(const MediaCodecs& aCodecs) const
+{
+  const auto& codecsToTest = aCodecs.Range();
+  for (const auto& codecToTest : codecsToTest) {
+    if (!Contains(codecToTest)) {
+      return false;
+    }
+  }
+  return true;
+}
 
 static int32_t
 GetParameterAsNumber(const nsContentTypeParser& aParser,
                      const char* aParameter,
                      const int32_t aErrorReturn)
 {
   nsAutoString parameterString;
   nsresult rv = aParser.GetParameter(aParameter, parameterString);
--- a/dom/media/MediaMIMETypes.h
+++ b/dom/media/MediaMIMETypes.h
@@ -4,16 +4,17 @@
  * 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 MediaMIMETypes_h_
 #define MediaMIMETypes_h_
 
 #include "mozilla/Maybe.h"
 #include "nsString.h"
+#include "VideoUtils.h"
 
 namespace mozilla {
 
 // Class containing pointing at a media MIME "type/subtype" string literal.
 // See IsMediaMIMEType for restrictions.
 // Mainly used to help construct a MediaMIMEType through the statically-checked
 // MEDIAMIMETYPE macro, or to compare a MediaMIMEType to a literal.
 class DependentMediaMIMEType
@@ -94,31 +95,72 @@ private:
   nsCString mMIMEType; // UTF8 MIME "type/subtype".
 };
 
 Maybe<MediaMIMEType> MakeMediaMIMEType(const nsAString& aType);
 Maybe<MediaMIMEType> MakeMediaMIMEType(const nsACString& aType);
 Maybe<MediaMIMEType> MakeMediaMIMEType(const char* aType);
 
 
+// A list of codecs attached to a MediaExtendedMIMEType.
+class MediaCodecs
+{
+public:
+  MediaCodecs() {}
+  // Construction from a comma-separated list of codecs. Unchecked.
+  explicit MediaCodecs(const nsAString& aCodecs)
+    : mCodecs(aCodecs)
+  {}
+  // Construction from a literal comma-separated list of codecs. Unchecked.
+  template <size_t N>
+  explicit MediaCodecs(const char (&aCodecs)[N])
+    : mCodecs(NS_ConvertUTF8toUTF16(aCodecs, N - 1))
+  {}
+
+  bool IsEmpty() const { return mCodecs.IsEmpty(); }
+  const nsAString& AsString() const { return mCodecs; }
+
+  using RangeType =
+    const StringListRange<nsString, StringListRangeEmptyItems::ProcessEmptyItems>;
+
+  // Produces a range object with begin()&end(), can be used in range-for loops.
+  // This will iterate through all codecs, even empty ones (except if the
+  // original list was an empty string). Iterators dereference to
+  // 'const nsDependentString', valid for as long as this MediaCodecs object.
+  RangeType Range() const
+  {
+    return RangeType(mCodecs);
+  };
+
+  bool Contains(const nsAString& aCodec) const;
+  bool ContainsAll(const MediaCodecs& aCodecs) const;
+
+private:
+  // UTF16 comma-separated list of codecs.
+  // See http://www.rfc-editor.org/rfc/rfc4281.txt for the description
+  // of the 'codecs' parameter.
+  nsString mCodecs;
+};
+
+
 // Class containing pre-parsed media MIME type parameters, e.g.:
 // MIME type/subtype, optional codecs, etc.
 class MediaExtendedMIMEType
 {
 public:
   explicit MediaExtendedMIMEType(const MediaMIMEType& aType);
   explicit MediaExtendedMIMEType(MediaMIMEType&& aType);
 
   // MIME "type/subtype".
   const MediaMIMEType& 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; }
+  const MediaCodecs& Codecs() 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:
@@ -130,17 +172,17 @@ private:
 
   Maybe<int32_t> GetMaybeNumber(int32_t aNumber) const
   {
     return (aNumber < 0) ? Maybe<int32_t>(Nothing()) : Some(int32_t(aNumber));
   }
 
   MediaMIMEType mMIMEType; // MIME type/subtype.
   bool mHaveCodecs = false; // If false, mCodecs must be empty.
-  nsString mCodecs;
+  MediaCodecs mCodecs;
   int32_t mWidth = -1; // -1 if not provided.
   int32_t mHeight = -1; // -1 if not provided.
   int32_t mFramerate = -1; // -1 if not provided.
   int32_t mBitrate = -1; // -1 if not provided.
 };
 
 Maybe<MediaExtendedMIMEType> MakeMediaExtendedMIMEType(const nsAString& aType);
 Maybe<MediaExtendedMIMEType> MakeMediaExtendedMIMEType(const nsACString& aType);