Bug 1329568 - MediaExtendedMIMEType - r=jya

This patch factors out all data handling of MIME strings from MediaContentType
to MediaExtendedMIMEType.

MediaExtendedMIMEType is pretty much a copy of the old MediaContentType, as the
functionality was fine (but will be modified in upcoming patches).
MediaContentType then just delegates the work to its embedded
MediaExtendedMIMEType field.
The main difference is that the default constructor and Populate() method have
been replaced with a single constructor that takes all the arguments at once.

MozReview-Commit-ID: GBAgPDT2DUW

--HG--
rename : dom/media/MediaContentType.cpp => dom/media/MediaMIMETypes.cpp
rename : dom/media/MediaContentType.h => dom/media/MediaMIMETypes.h
extra : rebase_source : cefaeeb2a81b60c575e350cc8399af038a021dc2
This commit is contained in:
Gerald Squelart 2016-12-01 13:05:45 +11:00
Родитель 5945087c01
Коммит d6f0cf3f9c
5 изменённых файлов: 182 добавлений и 75 удалений

Просмотреть файл

@ -10,56 +10,14 @@
namespace mozilla {
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;
}
bool
MediaContentType::Populate(const nsAString& aType)
{
MOZ_ASSERT(NS_IsMainThread());
nsContentTypeParser parser(aType);
nsAutoString mime;
nsresult rv = parser.GetType(mime);
if (!NS_SUCCEEDED(rv) || mime.IsEmpty()) {
return false;
}
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);
return true;
}
Maybe<MediaContentType>
MakeMediaContentType(const nsAString& aType)
{
Maybe<MediaContentType> type{Some(MediaContentType{})};
if (!type->Populate(aType)) {
type.reset();
Maybe<MediaExtendedMIMEType> mime = MakeMediaExtendedMIMEType(aType);
if (mime) {
return Some(MediaContentType(Move(*mime)));
}
return type;
return Nothing();
}
Maybe<MediaContentType>

Просмотреть файл

@ -7,46 +7,43 @@
#ifndef MediaContentType_h_
#define MediaContentType_h_
#include "MediaMIMETypes.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 containing media type information for containers.
class MediaContentType
{
public:
// MIME type. Guaranteed not to be empty.
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:
friend Maybe<MediaContentType> MakeMediaContentType(const nsAString& aType);
bool Populate(const nsAString& aType);
Maybe<int32_t> GetMaybeNumber(int32_t aNumber) const
explicit MediaContentType(const MediaExtendedMIMEType& aType)
: mExtendedMIMEType(aType)
{
}
explicit MediaContentType(MediaExtendedMIMEType&& aType)
: mExtendedMIMEType(Move(aType))
{
return (aNumber < 0) ? Maybe<int32_t>(Nothing()) : Some(int32_t(aNumber));
}
nsCString mMIMEType; // UTF8 MIME type.
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.
const MediaExtendedMIMEType& ExtendedType() const { return mExtendedMIMEType; }
// MIME "type/subtype". Guaranteed not to be empty.
const nsACString& GetMIMEType() const { return mExtendedMIMEType.Type(); }
// 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(); }
// 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:
MediaExtendedMIMEType mExtendedMIMEType;
};
Maybe<MediaContentType> MakeMediaContentType(const nsAString& aType);

Просмотреть файл

@ -0,0 +1,89 @@
/* -*- 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"
namespace mozilla {
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;
}
MediaExtendedMIMEType::MediaExtendedMIMEType(const nsACString& aMIMEType,
bool aHaveCodecs,
const nsAString& aCodecs,
int32_t aWidth, int32_t aHeight,
int32_t aFramerate, int32_t aBitrate)
: mMIMEType(Move(aMIMEType))
, mHaveCodecs(aHaveCodecs)
, mCodecs(Move(aCodecs))
, mWidth(aWidth)
, mHeight(aHeight)
, mFramerate(aFramerate)
, mBitrate(aBitrate)
{
}
Maybe<MediaExtendedMIMEType>
MakeMediaExtendedMIMEType(const nsAString& aType)
{
MOZ_ASSERT(NS_IsMainThread());
nsContentTypeParser parser(aType);
nsAutoString mime;
nsresult rv = parser.GetType(mime);
if (!NS_SUCCEEDED(rv) || mime.IsEmpty()) {
return Nothing();
}
NS_ConvertUTF16toUTF8 mime8{mime};
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);
int32_t framerate = GetParameterAsNumber(parser, "framerate", -1);
int32_t bitrate = GetParameterAsNumber(parser, "bitrate", -1);
return Some(MediaExtendedMIMEType(mime8,
haveCodecs, codecs,
width, height,
framerate, bitrate));
}
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

Просмотреть файл

@ -0,0 +1,61 @@
/* -*- 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 MediaMIMETypes_h_
#define MediaMIMETypes_h_
#include "mozilla/Maybe.h"
#include "nsString.h"
namespace mozilla {
// Class containing pre-parsed media MIME type parameters, e.g.:
// MIME type/subtype, optional codecs, etc.
class MediaExtendedMIMEType
{
public:
// MIME "type/subtype".
const nsACString& 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; }
// 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:
friend Maybe<MediaExtendedMIMEType> MakeMediaExtendedMIMEType(const nsAString& aType);
MediaExtendedMIMEType(const nsACString& aMIMEType,
bool aHaveCodecs, const nsAString& aCodecs,
int32_t aWidth, int32_t aHeight,
int32_t aFramerate, int32_t aBitrate);
Maybe<int32_t> GetMaybeNumber(int32_t aNumber) const
{
return (aNumber < 0) ? Maybe<int32_t>(Nothing()) : Some(int32_t(aNumber));
}
nsCString mMIMEType; // UTF8 MIME type.
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.
};
Maybe<MediaExtendedMIMEType> MakeMediaExtendedMIMEType(const nsAString& aType);
Maybe<MediaExtendedMIMEType> MakeMediaExtendedMIMEType(const nsACString& aType);
Maybe<MediaExtendedMIMEType> MakeMediaExtendedMIMEType(const char* aType);
} // namespace mozilla
#endif // MediaMIMETypes_h_

Просмотреть файл

@ -111,6 +111,7 @@ EXPORTS += [
'MediaFormatReader.h',
'MediaInfo.h',
'MediaMetadataManager.h',
'MediaMIMETypes.h',
'MediaPrefs.h',
'MediaQueue.h',
'MediaRecorder.h',
@ -218,6 +219,7 @@ UNIFIED_SOURCES += [
'MediaFormatReader.cpp',
'MediaInfo.cpp',
'MediaManager.cpp',
'MediaMIMETypes.cpp',
'MediaPrefs.cpp',
'MediaRecorder.cpp',
'MediaResource.cpp',