Bug 1176218 - p10. VideoUtils' CreateTrackInfo*() - r=jya
Create a TrackInfo (VideoInfo or AudioInfo) from a codec MIME type, and
optionally with extra parameters from a MediaContentType.
MozReview-Commit-ID: JfDMQjVgCNT
--- a/dom/media/VideoUtils.cpp
+++ b/dom/media/VideoUtils.cpp
@@ -4,16 +4,17 @@
#include "VideoUtils.h"
#include "mozilla/Base64.h"
#include "mozilla/TaskQueue.h"
#include "mozilla/Telemetry.h"
#include "mozilla/Function.h"
+#include "MediaContentType.h"
#include "MediaPrefs.h"
#include "MediaResource.h"
#include "TimeUnits.h"
#include "nsMathUtils.h"
#include "nsSize.h"
#include "VorbisUtils.h"
#include "ImageContainer.h"
#include "mozilla/SharedThreadPool.h"
@@ -471,9 +472,55 @@ IsVP8CodecString(const nsAString& aCodec
bool
IsVP9CodecString(const nsAString& aCodec)
{
return aCodec.EqualsLiteral("vp9") ||
aCodec.EqualsLiteral("vp9.0");
}
+template <int N>
+static bool
+StartsWith(const nsACString& string, const char (&prefix)[N])
+{
+ if (N - 1 > string.Length()) {
+ return false;
+ }
+ return memcmp(string.Data(), prefix, N - 1) == 0;
+}
+
+UniquePtr<TrackInfo>
+CreateTrackInfoWithMIMEType(const nsACString& aCodecMIMEType)
+{
+ UniquePtr<TrackInfo> trackInfo;
+ if (StartsWith(aCodecMIMEType, "audio/")) {
+ trackInfo.reset(new AudioInfo());
+ trackInfo->mMimeType = aCodecMIMEType;
+ } else if (StartsWith(aCodecMIMEType, "video/")) {
+ trackInfo.reset(new VideoInfo());
+ trackInfo->mMimeType = aCodecMIMEType;
+ }
+ return trackInfo;
+}
+
+UniquePtr<TrackInfo>
+CreateTrackInfoWithMIMETypeAndContentTypeExtraParameters(
+ const nsACString& aCodecMIMEType,
+ const MediaContentType& aContentType)
+{
+ UniquePtr<TrackInfo> trackInfo = CreateTrackInfoWithMIMEType(aCodecMIMEType);
+ if (trackInfo) {
+ VideoInfo* videoInfo = trackInfo->GetAsVideoInfo();
+ if (videoInfo) {
+ Maybe<int32_t> maybeWidth = aContentType.GetWidth();
+ if (maybeWidth && *maybeWidth > 0) {
+ videoInfo->mImage.width = *maybeWidth;
+ }
+ Maybe<int32_t> maybeHeight = aContentType.GetHeight();
+ if (maybeHeight && *maybeHeight > 0) {
+ videoInfo->mImage.height = *maybeHeight;
+ }
+ }
+ }
+ return trackInfo;
+}
+
} // end namespace mozilla
--- a/dom/media/VideoUtils.h
+++ b/dom/media/VideoUtils.h
@@ -2,21 +2,23 @@
/* 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 VideoUtils_h
#define VideoUtils_h
+#include "MediaInfo.h"
#include "mozilla/Attributes.h"
#include "mozilla/CheckedInt.h"
#include "mozilla/MozPromise.h"
#include "mozilla/ReentrantMonitor.h"
#include "mozilla/RefPtr.h"
+#include "mozilla/UniquePtr.h"
#include "nsAutoPtr.h"
#include "nsIThread.h"
#include "nsSize.h"
#include "nsRect.h"
#include "nsThreadUtils.h"
#include "prtime.h"
@@ -34,16 +36,18 @@ using mozilla::CheckedUint32;
// dependent on other changes which we don't want to wait for. We plan to
// remove this file in the near future.
// This belongs in xpcom/monitor/Monitor.h, once we've made
// mozilla::Monitor non-reentrant.
namespace mozilla {
+class MediaContentType;
+
// EME Key System String.
extern const nsLiteralCString kEMEKeySystemClearkey;
extern const nsLiteralCString kEMEKeySystemWidevine;
extern const nsLiteralCString kEMEKeySystemPrimetime;
/**
* ReentrantMonitorConditionallyEnter
*
@@ -342,16 +346,27 @@ bool
IsAACCodecString(const nsAString& aCodec);
bool
IsVP8CodecString(const nsAString& aCodec);
bool
IsVP9CodecString(const nsAString& aCodec);
+// Try and create a TrackInfo with a given codec MIME type.
+UniquePtr<TrackInfo>
+CreateTrackInfoWithMIMEType(const nsACString& aCodecMIMEType);
+
+// Try and create a TrackInfo with a given codec MIME type, and optional extra
+// parameters from a content type (its MIME type and codecs are ignored).
+UniquePtr<TrackInfo>
+CreateTrackInfoWithMIMETypeAndContentTypeExtraParameters(
+ const nsACString& aCodecMIMEType,
+ const MediaContentType& aContentType);
+
template <typename String>
class StringListRange
{
typedef typename String::char_type CharType;
typedef const CharType* Pointer;
public:
// Iterator into range, trims items and skips empty items.