Bug 1176218 - p2. New MediaContentType class to parse content type string - r=jya
authorGerald Squelart <gsquelart@mozilla.com>
Fri, 07 Oct 2016 17:38:44 +1100
changeset 317111 0bed1d9a4bb27ea2fa4e1361c35226b3fa9c4347
parent 317110 5c82a43c23a87e75bf60bdbb8cce8d081734e6e4
child 317112 989afc4cbc443fbbe175dbd4c808efa64f29ca61
push id30792
push userphilringnalda@gmail.com
push dateSat, 08 Oct 2016 23:47:45 +0000
treeherdermozilla-central@313a2d049350 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersjya
bugs1176218
milestone52.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 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',