Line data Source code
1 : /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 : /* vim:set ts=2 sw=2 sts=2 et cindent: */
3 : /* This Source Code Form is subject to the terms of the Mozilla Public
4 : * License, v. 2.0. If a copy of the MPL was not distributed with this
5 : * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 :
7 : #ifndef MediaMIMETypes_h_
8 : #define MediaMIMETypes_h_
9 :
10 : #include "VideoUtils.h"
11 : #include "mozilla/Maybe.h"
12 : #include "nsString.h"
13 :
14 : namespace mozilla {
15 :
16 : // Class containing pointing at a media MIME "type/subtype" string literal.
17 : // See IsMediaMIMEType for restrictions.
18 : // Mainly used to help construct a MediaMIMEType through the statically-checked
19 : // MEDIAMIMETYPE macro, or to compare a MediaMIMEType to a literal.
20 0 : class DependentMediaMIMEType
21 : {
22 : public:
23 : // Construction from a literal. Checked in debug builds.
24 : // Use MEDIAMIMETYPE macro instead, for static checking.
25 : template <size_t N>
26 0 : explicit DependentMediaMIMEType(const char (&aType)[N])
27 0 : : mMIMEType(aType, N - 1)
28 : {
29 0 : MOZ_ASSERT(IsMediaMIMEType(aType, N - 1), "Invalid media MIME type");
30 0 : }
31 :
32 : // MIME "type/subtype".
33 0 : const nsDependentCString& AsDependentString() const { return mMIMEType; }
34 :
35 : private:
36 : nsDependentCString mMIMEType;
37 : };
38 :
39 : // Instantiate a DependentMediaMIMEType from a literal. Statically checked.
40 : #define MEDIAMIMETYPE(LIT) \
41 : static_cast<const DependentMediaMIMEType&>( \
42 : []() { \
43 : static_assert(IsMediaMIMEType(LIT), "Invalid media MIME type"); \
44 : return DependentMediaMIMEType(LIT); \
45 : }())
46 :
47 : // Class containing only pre-parsed lowercase media MIME type/subtype.
48 0 : class MediaMIMEType
49 : {
50 : public:
51 : // Construction from a DependentMediaMIMEType, with its inherent checks.
52 : // Implicit so MEDIAMIMETYPE can be used wherever a MediaMIMEType is expected.
53 0 : MOZ_IMPLICIT MediaMIMEType(const DependentMediaMIMEType& aType)
54 0 : : mMIMEType(aType.AsDependentString())
55 : {
56 0 : }
57 :
58 : // MIME "type/subtype", always lowercase.
59 0 : const nsACString& AsString() const { return mMIMEType; }
60 :
61 : // Comparison with DependentMediaMIMEType.
62 : // Useful to compare to MEDIAMIMETYPE literals.
63 0 : bool operator==(const DependentMediaMIMEType& aOther) const
64 : {
65 0 : return mMIMEType.Equals(aOther.AsDependentString());
66 : }
67 0 : bool operator!=(const DependentMediaMIMEType& aOther) const
68 : {
69 0 : return !mMIMEType.Equals(aOther.AsDependentString());
70 : }
71 :
72 : bool operator==(const MediaMIMEType& aOther) const
73 : {
74 : return mMIMEType.Equals(aOther.mMIMEType);
75 : }
76 : bool operator!=(const MediaMIMEType& aOther) const
77 : {
78 : return !mMIMEType.Equals(aOther.mMIMEType);
79 : }
80 :
81 : // True if type starts with "application/".
82 : bool HasApplicationMajorType() const;
83 : // True if type starts with "audio/".
84 : // Note that some audio content could be stored in a "video/..." container!
85 : bool HasAudioMajorType() const;
86 : // True if type starts with "video/".
87 : // Note that this does not guarantee 100% that the content is actually video!
88 : // (e.g., "video/webm" could contain a vorbis audio track.)
89 : bool HasVideoMajorType() const;
90 :
91 : size_t SizeOfExcludingThis(mozilla::MallocSizeOf aMallocSizeOf) const;
92 :
93 : private:
94 : friend Maybe<MediaMIMEType> MakeMediaMIMEType(const nsAString& aType);
95 : friend class MediaExtendedMIMEType;
96 : explicit MediaMIMEType(const nsACString& aType);
97 :
98 : nsCString mMIMEType; // UTF8 MIME "type/subtype".
99 : };
100 :
101 : Maybe<MediaMIMEType> MakeMediaMIMEType(const nsAString& aType);
102 : Maybe<MediaMIMEType> MakeMediaMIMEType(const nsACString& aType);
103 : Maybe<MediaMIMEType> MakeMediaMIMEType(const char* aType);
104 :
105 :
106 : // A list of case-sensitive codecs attached to a MediaExtendedMIMEType.
107 0 : class MediaCodecs
108 : {
109 : public:
110 0 : MediaCodecs() { }
111 : // Construction from a comma-separated list of codecs. Unchecked.
112 0 : explicit MediaCodecs(const nsAString& aCodecs) : mCodecs(aCodecs) { }
113 : // Construction from a literal comma-separated list of codecs. Unchecked.
114 : template <size_t N>
115 : explicit MediaCodecs(const char (&aCodecs)[N])
116 : : mCodecs(NS_ConvertUTF8toUTF16(aCodecs, N - 1))
117 : {
118 : }
119 :
120 0 : bool IsEmpty() const { return mCodecs.IsEmpty(); }
121 0 : const nsAString& AsString() const { return mCodecs; }
122 :
123 : using RangeType =
124 : const StringListRange<nsString, StringListRangeEmptyItems::ProcessEmptyItems>;
125 :
126 : // Produces a range object with begin()&end(), can be used in range-for loops.
127 : // This will iterate through all codecs, even empty ones (except if the
128 : // original list was an empty string). Iterators dereference to
129 : // 'const nsDependentString', valid for as long as this MediaCodecs object.
130 0 : RangeType Range() const
131 : {
132 0 : return RangeType(mCodecs);
133 : };
134 :
135 : // Does this list of codecs contain the given aCodec?
136 : bool Contains(const nsAString& aCodec) const;
137 : // Does this list of codecs contain *all* the codecs in the given list?
138 : bool ContainsAll(const MediaCodecs& aCodecs) const;
139 :
140 : // Does this list of codecs contain a codec starting with the given prefix?
141 : bool ContainsPrefix(const nsAString& aCodecPrefix) const;
142 :
143 : template <size_t N>
144 0 : bool operator==(const char (&aType)[N]) const
145 : {
146 0 : return mCodecs.EqualsASCII(aType, N - 1);
147 : }
148 :
149 : size_t SizeOfExcludingThis(mozilla::MallocSizeOf aMallocSizeOf) const;
150 :
151 : private:
152 : // UTF16 comma-separated list of codecs.
153 : // See http://www.rfc-editor.org/rfc/rfc4281.txt for the description
154 : // of the 'codecs' parameter.
155 : nsString mCodecs;
156 : };
157 :
158 :
159 : // Class containing pre-parsed media MIME type parameters, e.g.:
160 : // MIME type/subtype, optional codecs, etc.
161 0 : class MediaExtendedMIMEType
162 : {
163 : public:
164 : explicit MediaExtendedMIMEType(const MediaMIMEType& aType);
165 : explicit MediaExtendedMIMEType(MediaMIMEType&& aType);
166 :
167 : // MIME "type/subtype".
168 0 : const MediaMIMEType& Type() const { return mMIMEType; }
169 :
170 : // Was there an explicit 'codecs' parameter provided?
171 0 : bool HaveCodecs() const { return mHaveCodecs; }
172 : // Codecs. May be empty if not provided or explicitly provided as empty.
173 0 : const MediaCodecs& Codecs() const { return mCodecs; }
174 :
175 : // Sizes and rates.
176 0 : Maybe<int32_t> GetWidth() const { return GetMaybeNumber(mWidth); }
177 0 : Maybe<int32_t> GetHeight() const { return GetMaybeNumber(mHeight); }
178 : Maybe<int32_t> GetFramerate() const { return GetMaybeNumber(mFramerate); }
179 : Maybe<int32_t> GetBitrate() const { return GetMaybeNumber(mBitrate); }
180 :
181 : // Original string. Note that "type/subtype" may not be lowercase,
182 : // use Type().AsString() instead to get the normalized "type/subtype".
183 0 : const nsACString& OriginalString() const { return mOriginalString; }
184 :
185 : size_t SizeOfExcludingThis(mozilla::MallocSizeOf aMallocSizeOf) const;
186 :
187 : private:
188 : friend Maybe<MediaExtendedMIMEType> MakeMediaExtendedMIMEType(const nsAString& aType);
189 : MediaExtendedMIMEType(const nsACString& aOriginalString,
190 : const nsACString& aMIMEType,
191 : bool aHaveCodecs, const nsAString& aCodecs,
192 : int32_t aWidth, int32_t aHeight,
193 : int32_t aFramerate, int32_t aBitrate);
194 :
195 0 : Maybe<int32_t> GetMaybeNumber(int32_t aNumber) const
196 : {
197 0 : return (aNumber < 0) ? Maybe<int32_t>(Nothing()) : Some(int32_t(aNumber));
198 : }
199 :
200 : nsCString mOriginalString; // Original full string.
201 : MediaMIMEType mMIMEType; // MIME type/subtype.
202 : bool mHaveCodecs = false; // If false, mCodecs must be empty.
203 : MediaCodecs mCodecs;
204 : int32_t mWidth = -1; // -1 if not provided.
205 : int32_t mHeight = -1; // -1 if not provided.
206 : int32_t mFramerate = -1; // -1 if not provided.
207 : int32_t mBitrate = -1; // -1 if not provided.
208 : };
209 :
210 : Maybe<MediaExtendedMIMEType> MakeMediaExtendedMIMEType(const nsAString& aType);
211 : Maybe<MediaExtendedMIMEType> MakeMediaExtendedMIMEType(const nsACString& aType);
212 : Maybe<MediaExtendedMIMEType> MakeMediaExtendedMIMEType(const char* aType);
213 :
214 : } // namespace mozilla
215 :
216 : #endif // MediaMIMETypes_h_
|