Line data Source code
1 : /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 : /* vim: set ts=8 sts=2 et sw=2 tw=80: */
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 : #include "MediaMIMETypes.h"
8 :
9 : #include "nsContentTypeParser.h"
10 :
11 : namespace mozilla {
12 :
13 : template <int N>
14 : static bool
15 0 : StartsWith(const nsACString& string, const char (&prefix)[N])
16 : {
17 0 : if (N - 1 > string.Length()) {
18 0 : return false;
19 : }
20 0 : return memcmp(string.Data(), prefix, N - 1) == 0;
21 : }
22 :
23 : bool
24 0 : MediaMIMEType::HasApplicationMajorType() const
25 : {
26 0 : return StartsWith(mMIMEType, "application/");
27 : }
28 :
29 : bool
30 0 : MediaMIMEType::HasAudioMajorType() const
31 : {
32 0 : return StartsWith(mMIMEType, "audio/");
33 : }
34 :
35 : bool
36 0 : MediaMIMEType::HasVideoMajorType() const
37 : {
38 0 : return StartsWith(mMIMEType, "video/");
39 : }
40 :
41 : size_t
42 0 : MediaMIMEType::SizeOfExcludingThis(MallocSizeOf aMallocSizeOf) const
43 : {
44 0 : return mMIMEType.SizeOfExcludingThisIfUnshared(aMallocSizeOf);
45 : }
46 :
47 0 : MediaMIMEType::MediaMIMEType(const nsACString& aType)
48 0 : : mMIMEType(aType)
49 : {
50 0 : }
51 :
52 : Maybe<MediaMIMEType>
53 0 : MakeMediaMIMEType(const nsAString& aType)
54 : {
55 0 : MOZ_ASSERT(NS_IsMainThread());
56 :
57 0 : nsContentTypeParser parser(aType);
58 0 : nsAutoString mime;
59 0 : nsresult rv = parser.GetType(mime);
60 0 : if (!NS_SUCCEEDED(rv) || mime.IsEmpty()) {
61 0 : return Nothing();
62 : }
63 :
64 0 : NS_ConvertUTF16toUTF8 mime8{mime};
65 0 : if (!IsMediaMIMEType(mime8)) {
66 0 : return Nothing();
67 : }
68 :
69 0 : return Some(MediaMIMEType(mime8));
70 : }
71 :
72 : Maybe<MediaMIMEType>
73 0 : MakeMediaMIMEType(const nsACString& aType)
74 : {
75 0 : return MakeMediaMIMEType(NS_ConvertUTF8toUTF16(aType));
76 : }
77 :
78 : Maybe<MediaMIMEType>
79 0 : MakeMediaMIMEType(const char* aType)
80 : {
81 0 : if (!aType) {
82 0 : return Nothing();
83 : }
84 0 : return MakeMediaMIMEType(nsDependentCString(aType));
85 : }
86 :
87 : bool
88 0 : MediaCodecs::Contains(const nsAString& aCodec) const
89 : {
90 0 : for (const auto& myCodec : Range()) {
91 0 : if (myCodec == aCodec) {
92 0 : return true;
93 : }
94 : }
95 0 : return false;
96 : }
97 :
98 : bool
99 0 : MediaCodecs::ContainsAll(const MediaCodecs& aCodecs) const
100 : {
101 0 : const auto& codecsToTest = aCodecs.Range();
102 0 : for (const auto& codecToTest : codecsToTest) {
103 0 : if (!Contains(codecToTest)) {
104 0 : return false;
105 : }
106 : }
107 0 : return true;
108 : }
109 :
110 : bool
111 0 : MediaCodecs::ContainsPrefix(const nsAString& aCodecPrefix) const
112 : {
113 0 : const size_t prefixLength = aCodecPrefix.Length();
114 0 : for (const auto& myCodec : Range()) {
115 0 : if (myCodec.Length() >= prefixLength &&
116 0 : memcmp(myCodec.Data(), aCodecPrefix.Data(), prefixLength) == 0) {
117 0 : return true;
118 : }
119 : }
120 0 : return false;
121 : }
122 :
123 : size_t
124 0 : MediaCodecs::SizeOfExcludingThis(MallocSizeOf aMallocSizeOf) const
125 : {
126 0 : return mCodecs.SizeOfExcludingThisIfUnshared(aMallocSizeOf);
127 : }
128 :
129 :
130 : static int32_t
131 0 : GetParameterAsNumber(const nsContentTypeParser& aParser,
132 : const char* aParameter,
133 : const int32_t aErrorReturn)
134 : {
135 0 : nsAutoString parameterString;
136 0 : nsresult rv = aParser.GetParameter(aParameter, parameterString);
137 0 : if (NS_FAILED_impl(rv)) {
138 0 : return aErrorReturn;
139 : }
140 0 : int32_t number = parameterString.ToInteger(&rv);
141 0 : if (MOZ_UNLIKELY(NS_FAILED_impl(rv))) {
142 0 : return aErrorReturn;
143 : }
144 0 : return number;
145 : }
146 :
147 0 : MediaExtendedMIMEType::MediaExtendedMIMEType(const nsACString& aOriginalString,
148 : const nsACString& aMIMEType,
149 : bool aHaveCodecs,
150 : const nsAString& aCodecs,
151 : int32_t aWidth, int32_t aHeight,
152 0 : int32_t aFramerate, int32_t aBitrate)
153 : : mOriginalString(aOriginalString)
154 : , mMIMEType(aMIMEType)
155 : , mHaveCodecs(aHaveCodecs)
156 : , mCodecs(aCodecs)
157 : , mWidth(aWidth)
158 : , mHeight(aHeight)
159 : , mFramerate(aFramerate)
160 0 : , mBitrate(aBitrate)
161 : {
162 0 : }
163 :
164 0 : MediaExtendedMIMEType::MediaExtendedMIMEType(const MediaMIMEType& aType)
165 0 : : mOriginalString(aType.AsString())
166 0 : , mMIMEType(aType)
167 : {
168 0 : }
169 :
170 0 : MediaExtendedMIMEType::MediaExtendedMIMEType(MediaMIMEType&& aType)
171 0 : : mOriginalString(aType.AsString())
172 0 : , mMIMEType(Move(aType))
173 : {
174 0 : }
175 :
176 : Maybe<MediaExtendedMIMEType>
177 0 : MakeMediaExtendedMIMEType(const nsAString& aType)
178 : {
179 0 : MOZ_ASSERT(NS_IsMainThread());
180 :
181 0 : nsContentTypeParser parser(aType);
182 0 : nsAutoString mime;
183 0 : nsresult rv = parser.GetType(mime);
184 0 : if (!NS_SUCCEEDED(rv) || mime.IsEmpty()) {
185 0 : return Nothing();
186 : }
187 :
188 0 : NS_ConvertUTF16toUTF8 mime8{mime};
189 0 : if (!IsMediaMIMEType(mime8)) {
190 0 : return Nothing();
191 : }
192 :
193 0 : nsAutoString codecs;
194 0 : rv = parser.GetParameter("codecs", codecs);
195 0 : bool haveCodecs = NS_SUCCEEDED(rv);
196 :
197 0 : int32_t width = GetParameterAsNumber(parser, "width", -1);
198 0 : int32_t height = GetParameterAsNumber(parser, "height", -1);
199 0 : int32_t framerate = GetParameterAsNumber(parser, "framerate", -1);
200 0 : int32_t bitrate = GetParameterAsNumber(parser, "bitrate", -1);
201 :
202 0 : return Some(MediaExtendedMIMEType(NS_ConvertUTF16toUTF8(aType),
203 : mime8,
204 : haveCodecs, codecs,
205 : width, height,
206 0 : framerate, bitrate));
207 : }
208 :
209 : size_t
210 0 : MediaExtendedMIMEType::SizeOfExcludingThis(MallocSizeOf aMallocSizeOf) const
211 : {
212 0 : return mOriginalString.SizeOfExcludingThisIfUnshared(aMallocSizeOf)
213 0 : + mMIMEType.SizeOfExcludingThis(aMallocSizeOf)
214 0 : + mCodecs.SizeOfExcludingThis(aMallocSizeOf);
215 : }
216 :
217 : Maybe<MediaExtendedMIMEType>
218 0 : MakeMediaExtendedMIMEType(const nsACString& aType)
219 : {
220 0 : return MakeMediaExtendedMIMEType(NS_ConvertUTF8toUTF16(aType));
221 : }
222 :
223 : Maybe<MediaExtendedMIMEType>
224 0 : MakeMediaExtendedMIMEType(const char* aType)
225 : {
226 0 : if (!aType) {
227 0 : return Nothing();
228 : }
229 0 : return MakeMediaExtendedMIMEType(nsDependentCString(aType));
230 : }
231 :
232 : } // namespace mozilla
|