Bug 1176218 - p2. New MediaContentType class to parse content type string - r=jya draft
authorGerald Squelart <gsquelart@mozilla.com>
Fri, 07 Oct 2016 17:38:44 +1100
changeset 421994 823717a46e698342dce0f9e0b5bbfab21281297d
parent 421963 d07371c8cdcf116952bffafad0cd6cda78f59b20
child 421995 85b8e852f004b2728b06831c43b33ee4e7aebe52
push id31653
push usergsquelart@mozilla.com
push dateFri, 07 Oct 2016 06:47:50 +0000
reviewersjya
bugs1176218
milestone52.0a1
Bug 1176218 - p2. New MediaContentType class to parse content type string - r=jya Collect all parsed parameters in a handy MediaContentType structure. MozReview-Commit-ID: DlnilsmO1eP
dom/media/MediaContentType.cpp
dom/media/MediaContentType.h
dom/media/moz.build
new file mode 100644
--- /dev/null
+++ b/dom/media/MediaContentType.cpp
@@ -0,0 +1,60 @@
+/* -*- 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 "nsContentTypeParser.h"
+
+namespace mozilla {
+
+MediaContentType::MediaContentType(const nsAString& aType)
+{
+  Populate(aType);
+}
+
+MediaContentType::MediaContentType(const nsACString& aType)
+{
+  NS_ConvertUTF8toUTF16 typeUTF16(aType);
+  Populate(typeUTF16);
+}
+
+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;
+}
+
+void
+MediaContentType::Populate(const nsAString& aType)
+{
+  nsContentTypeParser parser(aType);
+  nsAutoString mime;
+  nsresult rv = parser.GetType(mime);
+  if (NS_SUCCEEDED(rv)) {
+    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);
+}
+
+} // namespace mozilla
new file mode 100644
--- /dev/null
+++ b/dom/media/MediaContentType.h
@@ -0,0 +1,60 @@
+/* -*- 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_
+
+#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
+{
+public:
+  // Parse UTF16 string to extract parameters.
+  explicit MediaContentType(const nsAString& aType);
+  // Parse UTF8 string to extract parameters.
+  explicit MediaContentType(const nsACString& aType);
+
+  bool IsValid() const { return !GetMIMEType().IsEmpty(); }
+
+  // MIME type. Empty if construction arguments could not be parsed.
+  const nsACString& GetMIMEType() 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:
+  void 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. Empty if parsing failed.
+  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.
+};
+
+} // namespace mozilla
+
+#endif // MediaContentType_h_
--- a/dom/media/moz.build
+++ b/dom/media/moz.build
@@ -100,16 +100,17 @@ EXPORTS += [
     'DecoderTraits.h',
     'DOMMediaStream.h',
     'EncodedBufferCache.h',
     'FileBlockCache.h',
     'FrameStatistics.h',
     'Intervals.h',
     'Latency.h',
     'MediaCache.h',
+    'MediaContentType.h',
     'MediaData.h',
     'MediaDataDemuxer.h',
     'MediaDecoder.h',
     'MediaDecoderOwner.h',
     'MediaDecoderReader.h',
     'MediaDecoderStateMachine.h',
     'MediaEventSource.h',
     'MediaFormatReader.h',
@@ -214,16 +215,17 @@ UNIFIED_SOURCES += [
     'DecoderDoctorDiagnostics.cpp',
     'DOMMediaStream.cpp',
     'EncodedBufferCache.cpp',
     'FileBlockCache.cpp',
     'GetUserMediaRequest.cpp',
     'GraphDriver.cpp',
     'Latency.cpp',
     'MediaCache.cpp',
+    'MediaContentType.cpp',
     'MediaData.cpp',
     'MediaDecoder.cpp',
     'MediaDecoderReader.cpp',
     'MediaDecoderReaderWrapper.cpp',
     'MediaDecoderStateMachine.cpp',
     'MediaDeviceInfo.cpp',
     'MediaDevices.cpp',
     'MediaFormatReader.cpp',