2016-12-01 05:05:45 +03:00
|
|
|
/* -*- 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 "MediaMIMETypes.h"
|
|
|
|
|
|
|
|
#include "nsContentTypeParser.h"
|
2018-06-07 17:18:08 +03:00
|
|
|
#include "mozilla/dom/MediaCapabilitiesBinding.h"
|
2016-12-01 05:05:45 +03:00
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
|
2016-12-17 09:43:02 +03:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool MediaMIMEType::HasApplicationMajorType() const {
|
|
|
|
return StartsWith(mMIMEType, "application/");
|
|
|
|
}
|
|
|
|
|
|
|
|
bool MediaMIMEType::HasAudioMajorType() const {
|
|
|
|
return StartsWith(mMIMEType, "audio/");
|
|
|
|
}
|
|
|
|
|
|
|
|
bool MediaMIMEType::HasVideoMajorType() const {
|
|
|
|
return StartsWith(mMIMEType, "video/");
|
|
|
|
}
|
|
|
|
|
2016-12-22 03:54:56 +03:00
|
|
|
size_t MediaMIMEType::SizeOfExcludingThis(MallocSizeOf aMallocSizeOf) const {
|
|
|
|
return mMIMEType.SizeOfExcludingThisIfUnshared(aMallocSizeOf);
|
|
|
|
}
|
|
|
|
|
2016-12-01 08:57:31 +03:00
|
|
|
MediaMIMEType::MediaMIMEType(const nsACString& aType) : mMIMEType(aType) {}
|
|
|
|
|
|
|
|
Maybe<MediaMIMEType> MakeMediaMIMEType(const nsAString& aType) {
|
|
|
|
nsContentTypeParser parser(aType);
|
|
|
|
nsAutoString mime;
|
|
|
|
nsresult rv = parser.GetType(mime);
|
|
|
|
if (!NS_SUCCEEDED(rv) || mime.IsEmpty()) {
|
|
|
|
return Nothing();
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_ConvertUTF16toUTF8 mime8{mime};
|
2017-01-03 14:07:11 +03:00
|
|
|
if (!IsMediaMIMEType(mime8)) {
|
|
|
|
return Nothing();
|
|
|
|
}
|
2016-12-01 08:57:31 +03:00
|
|
|
|
|
|
|
return Some(MediaMIMEType(mime8));
|
|
|
|
}
|
|
|
|
|
|
|
|
Maybe<MediaMIMEType> MakeMediaMIMEType(const nsACString& aType) {
|
|
|
|
return MakeMediaMIMEType(NS_ConvertUTF8toUTF16(aType));
|
|
|
|
}
|
|
|
|
|
|
|
|
Maybe<MediaMIMEType> MakeMediaMIMEType(const char* aType) {
|
|
|
|
if (!aType) {
|
|
|
|
return Nothing();
|
|
|
|
}
|
|
|
|
return MakeMediaMIMEType(nsDependentCString(aType));
|
|
|
|
}
|
|
|
|
|
2016-12-28 02:48:27 +03:00
|
|
|
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;
|
|
|
|
}
|
2016-12-01 08:57:31 +03:00
|
|
|
|
2017-06-29 21:17:56 +03:00
|
|
|
bool MediaCodecs::ContainsPrefix(const nsAString& aCodecPrefix) const {
|
|
|
|
const size_t prefixLength = aCodecPrefix.Length();
|
|
|
|
for (const auto& myCodec : Range()) {
|
|
|
|
if (myCodec.Length() >= prefixLength &&
|
2019-10-06 21:29:55 +03:00
|
|
|
memcmp(myCodec.Data(), aCodecPrefix.Data(),
|
|
|
|
prefixLength * sizeof(char16_t)) == 0) {
|
2017-06-29 21:17:56 +03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-12-22 03:54:56 +03:00
|
|
|
size_t MediaCodecs::SizeOfExcludingThis(MallocSizeOf aMallocSizeOf) const {
|
|
|
|
return mCodecs.SizeOfExcludingThisIfUnshared(aMallocSizeOf);
|
|
|
|
}
|
|
|
|
|
2016-12-01 05:05:45 +03:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2016-12-28 10:56:22 +03:00
|
|
|
MediaExtendedMIMEType::MediaExtendedMIMEType(
|
|
|
|
const nsACString& aOriginalString, const nsACString& aMIMEType,
|
2016-12-01 05:05:45 +03:00
|
|
|
bool aHaveCodecs, const nsAString& aCodecs, int32_t aWidth, int32_t aHeight,
|
2019-10-25 01:50:35 +03:00
|
|
|
double aFramerate, int32_t aBitrate)
|
2016-12-28 10:56:22 +03:00
|
|
|
: mOriginalString(aOriginalString),
|
|
|
|
mMIMEType(aMIMEType),
|
2016-12-01 05:05:45 +03:00
|
|
|
mHaveCodecs(aHaveCodecs),
|
2016-12-01 08:57:31 +03:00
|
|
|
mCodecs(aCodecs),
|
2016-12-01 05:05:45 +03:00
|
|
|
mWidth(aWidth),
|
|
|
|
mHeight(aHeight),
|
|
|
|
mFramerate(aFramerate),
|
|
|
|
mBitrate(aBitrate) {}
|
2018-11-19 16:25:37 +03:00
|
|
|
|
2018-06-07 17:18:08 +03:00
|
|
|
MediaExtendedMIMEType::MediaExtendedMIMEType(
|
|
|
|
const nsACString& aOriginalString, const nsACString& aMIMEType,
|
|
|
|
bool aHaveCodecs, const nsAString& aCodecs, int32_t aChannels,
|
|
|
|
int32_t aSamplerate, int32_t aBitrate)
|
|
|
|
: mOriginalString(aOriginalString),
|
|
|
|
mMIMEType(aMIMEType),
|
|
|
|
mHaveCodecs(aHaveCodecs),
|
|
|
|
mCodecs(aCodecs),
|
|
|
|
mChannels(aChannels),
|
|
|
|
mSamplerate(aSamplerate),
|
|
|
|
mBitrate(aBitrate) {}
|
|
|
|
|
2016-12-01 08:57:31 +03:00
|
|
|
MediaExtendedMIMEType::MediaExtendedMIMEType(const MediaMIMEType& aType)
|
2016-12-28 10:56:22 +03:00
|
|
|
: mOriginalString(aType.AsString()), mMIMEType(aType) {}
|
2016-12-01 08:57:31 +03:00
|
|
|
|
|
|
|
MediaExtendedMIMEType::MediaExtendedMIMEType(MediaMIMEType&& aType)
|
2016-12-28 10:56:22 +03:00
|
|
|
: mOriginalString(aType.AsString()), mMIMEType(std::move(aType)) {}
|
2016-12-01 08:57:31 +03:00
|
|
|
|
2019-02-26 01:05:29 +03:00
|
|
|
/* static */
|
|
|
|
Maybe<double> MediaExtendedMIMEType::ComputeFractionalString(
|
2018-06-07 17:18:08 +03:00
|
|
|
const nsAString& aFrac) {
|
|
|
|
nsAutoString frac(aFrac);
|
|
|
|
nsresult error;
|
|
|
|
double result = frac.ToDouble(&error);
|
|
|
|
if (NS_SUCCEEDED(error)) {
|
|
|
|
if (result <= 0) {
|
|
|
|
return Nothing();
|
|
|
|
}
|
|
|
|
return Some(result);
|
|
|
|
}
|
|
|
|
|
|
|
|
int32_t slashPos = frac.Find(NS_LITERAL_STRING("/"));
|
|
|
|
if (slashPos == kNotFound) {
|
|
|
|
return Nothing();
|
|
|
|
}
|
|
|
|
|
|
|
|
nsAutoString firstPart(Substring(frac, 0, slashPos - 1));
|
|
|
|
double first = firstPart.ToDouble(&error);
|
|
|
|
if (NS_FAILED(error)) {
|
|
|
|
return Nothing();
|
|
|
|
}
|
|
|
|
nsAutoString secondPart(Substring(frac, slashPos + 1));
|
|
|
|
double second = secondPart.ToDouble(&error);
|
|
|
|
if (NS_FAILED(error) || second == 0) {
|
|
|
|
return Nothing();
|
|
|
|
}
|
|
|
|
|
|
|
|
result = first / second;
|
|
|
|
if (result <= 0) {
|
|
|
|
return Nothing();
|
|
|
|
}
|
|
|
|
|
|
|
|
return Some(result);
|
|
|
|
}
|
|
|
|
|
2016-12-01 05:05:45 +03:00
|
|
|
Maybe<MediaExtendedMIMEType> MakeMediaExtendedMIMEType(const nsAString& aType) {
|
|
|
|
nsContentTypeParser parser(aType);
|
|
|
|
nsAutoString mime;
|
|
|
|
nsresult rv = parser.GetType(mime);
|
|
|
|
if (!NS_SUCCEEDED(rv) || mime.IsEmpty()) {
|
|
|
|
return Nothing();
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_ConvertUTF16toUTF8 mime8{mime};
|
2017-01-03 14:07:11 +03:00
|
|
|
if (!IsMediaMIMEType(mime8)) {
|
|
|
|
return Nothing();
|
|
|
|
}
|
2016-12-01 05:05:45 +03:00
|
|
|
|
|
|
|
nsAutoString codecs;
|
|
|
|
rv = parser.GetParameter("codecs", codecs);
|
|
|
|
bool haveCodecs = NS_SUCCEEDED(rv);
|
|
|
|
|
|
|
|
int32_t width = GetParameterAsNumber(parser, "width", -1);
|
|
|
|
int32_t height = GetParameterAsNumber(parser, "height", -1);
|
2018-06-07 17:18:08 +03:00
|
|
|
double framerate = GetParameterAsNumber(parser, "framerate", -1);
|
2016-12-01 05:05:45 +03:00
|
|
|
int32_t bitrate = GetParameterAsNumber(parser, "bitrate", -1);
|
|
|
|
|
2016-12-28 10:56:22 +03:00
|
|
|
return Some(MediaExtendedMIMEType(NS_ConvertUTF16toUTF8(aType), mime8,
|
2016-12-01 05:05:45 +03:00
|
|
|
haveCodecs, codecs, width, height,
|
2019-10-25 01:50:35 +03:00
|
|
|
framerate, bitrate));
|
2016-12-01 05:05:45 +03:00
|
|
|
}
|
|
|
|
|
2018-06-07 17:18:08 +03:00
|
|
|
Maybe<MediaExtendedMIMEType> MakeMediaExtendedMIMEType(
|
|
|
|
const dom::VideoConfiguration& aConfig) {
|
2018-09-26 22:19:47 +03:00
|
|
|
if (aConfig.mContentType.IsEmpty()) {
|
2018-06-07 17:18:08 +03:00
|
|
|
return Nothing();
|
|
|
|
}
|
2018-09-26 22:19:47 +03:00
|
|
|
nsContentTypeParser parser(aConfig.mContentType);
|
2018-06-07 17:18:08 +03:00
|
|
|
nsAutoString mime;
|
|
|
|
nsresult rv = parser.GetType(mime);
|
|
|
|
if (!NS_SUCCEEDED(rv) || mime.IsEmpty()) {
|
|
|
|
return Nothing();
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_ConvertUTF16toUTF8 mime8{mime};
|
|
|
|
if (!IsMediaMIMEType(mime8)) {
|
|
|
|
return Nothing();
|
|
|
|
}
|
|
|
|
|
|
|
|
nsAutoString codecs;
|
|
|
|
rv = parser.GetParameter("codecs", codecs);
|
|
|
|
bool haveCodecs = NS_SUCCEEDED(rv);
|
|
|
|
|
|
|
|
auto framerate =
|
2018-09-26 22:19:47 +03:00
|
|
|
MediaExtendedMIMEType::ComputeFractionalString(aConfig.mFramerate);
|
2018-06-07 17:18:08 +03:00
|
|
|
if (!framerate) {
|
|
|
|
return Nothing();
|
|
|
|
}
|
|
|
|
|
2019-10-25 01:50:35 +03:00
|
|
|
return Some(MediaExtendedMIMEType(
|
|
|
|
NS_ConvertUTF16toUTF8(aConfig.mContentType), mime8, haveCodecs, codecs,
|
|
|
|
aConfig.mWidth, aConfig.mHeight, framerate.ref(), aConfig.mBitrate));
|
2018-06-07 17:18:08 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
Maybe<MediaExtendedMIMEType> MakeMediaExtendedMIMEType(
|
|
|
|
const dom::AudioConfiguration& aConfig) {
|
2018-09-26 22:19:47 +03:00
|
|
|
if (aConfig.mContentType.IsEmpty()) {
|
2018-06-07 17:18:08 +03:00
|
|
|
return Nothing();
|
|
|
|
}
|
2018-09-26 22:19:47 +03:00
|
|
|
nsContentTypeParser parser(aConfig.mContentType);
|
2018-06-07 17:18:08 +03:00
|
|
|
nsAutoString mime;
|
|
|
|
nsresult rv = parser.GetType(mime);
|
|
|
|
if (!NS_SUCCEEDED(rv) || mime.IsEmpty()) {
|
|
|
|
return Nothing();
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_ConvertUTF16toUTF8 mime8{mime};
|
|
|
|
if (!IsMediaMIMEType(mime8)) {
|
|
|
|
return Nothing();
|
|
|
|
}
|
|
|
|
|
|
|
|
nsAutoString codecs;
|
|
|
|
rv = parser.GetParameter("codecs", codecs);
|
|
|
|
bool haveCodecs = NS_SUCCEEDED(rv);
|
|
|
|
|
|
|
|
int32_t channels = 2; // use a stereo config if not known.
|
|
|
|
if (aConfig.mChannels.WasPassed()) {
|
|
|
|
// A channels string was passed. Make sure it is valid.
|
|
|
|
nsresult error;
|
|
|
|
double value = aConfig.mChannels.Value().ToDouble(&error);
|
|
|
|
if (NS_FAILED(error)) {
|
|
|
|
return Nothing();
|
|
|
|
}
|
|
|
|
// Value is a channel configuration such as 5.1. We want to treat this as 6.
|
|
|
|
channels = value;
|
|
|
|
double fp = value - channels;
|
|
|
|
// round up as .1 and .2 aren't exactly expressible in binary.
|
|
|
|
channels += (fp * 10) + .5;
|
|
|
|
}
|
|
|
|
|
|
|
|
return Some(MediaExtendedMIMEType(
|
2018-09-26 22:19:47 +03:00
|
|
|
NS_ConvertUTF16toUTF8(aConfig.mContentType), mime8, haveCodecs, codecs,
|
2018-06-07 17:18:08 +03:00
|
|
|
channels,
|
|
|
|
aConfig.mSamplerate.WasPassed() ? aConfig.mSamplerate.Value() : 48000,
|
|
|
|
aConfig.mBitrate.WasPassed() ? aConfig.mBitrate.Value() : 131072));
|
|
|
|
}
|
|
|
|
|
2016-12-22 03:54:56 +03:00
|
|
|
size_t MediaExtendedMIMEType::SizeOfExcludingThis(
|
|
|
|
MallocSizeOf aMallocSizeOf) const {
|
2016-12-28 10:56:22 +03:00
|
|
|
return mOriginalString.SizeOfExcludingThisIfUnshared(aMallocSizeOf) +
|
|
|
|
mMIMEType.SizeOfExcludingThis(aMallocSizeOf) +
|
2016-12-22 03:54:56 +03:00
|
|
|
mCodecs.SizeOfExcludingThis(aMallocSizeOf);
|
|
|
|
}
|
|
|
|
|
2016-12-01 05:05:45 +03:00
|
|
|
Maybe<MediaExtendedMIMEType> MakeMediaExtendedMIMEType(
|
|
|
|
const nsACString& aType) {
|
|
|
|
return MakeMediaExtendedMIMEType(NS_ConvertUTF8toUTF16(aType));
|
|
|
|
}
|
|
|
|
|
|
|
|
Maybe<MediaExtendedMIMEType> MakeMediaExtendedMIMEType(const char* aType) {
|
|
|
|
if (!aType) {
|
|
|
|
return Nothing();
|
|
|
|
}
|
|
|
|
return MakeMediaExtendedMIMEType(nsDependentCString(aType));
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace mozilla
|