BookmarkManager: Fix 'new folder text field size changes on clicking it' issue.
[chromium-blink-merge.git] / media / base / mime_util.cc
blob367953946d05522932fe583c4d0f543b0343c4cc
1 // Copyright 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include <map>
7 #include "base/containers/hash_tables.h"
8 #include "base/lazy_instance.h"
9 #include "base/strings/string_number_conversions.h"
10 #include "base/strings/string_split.h"
11 #include "base/strings/string_util.h"
12 #include "build/build_config.h"
13 #include "media/base/mime_util.h"
15 #if defined(OS_ANDROID)
16 #include "base/android/build_info.h"
17 #endif
19 namespace media {
21 // Singleton utility class for mime types.
22 class MimeUtil {
23 public:
24 enum Codec {
25 INVALID_CODEC,
26 PCM,
27 MP3,
28 MPEG2_AAC_LC,
29 MPEG2_AAC_MAIN,
30 MPEG2_AAC_SSR,
31 MPEG4_AAC_LC,
32 MPEG4_AAC_SBR_v1,
33 MPEG4_AAC_SBR_PS_v2,
34 VORBIS,
35 OPUS,
36 H264_BASELINE,
37 H264_MAIN,
38 H264_HIGH,
39 HEVC_MAIN,
40 VP8,
41 VP9,
42 THEORA
45 bool IsSupportedMediaMimeType(const std::string& mime_type) const;
47 bool AreSupportedMediaCodecs(const std::vector<std::string>& codecs) const;
49 void ParseCodecString(const std::string& codecs,
50 std::vector<std::string>* codecs_out,
51 bool strip);
53 bool IsStrictMediaMimeType(const std::string& mime_type) const;
54 SupportsType IsSupportedStrictMediaMimeType(
55 const std::string& mime_type,
56 const std::vector<std::string>& codecs) const;
58 void RemoveProprietaryMediaTypesAndCodecsForTests();
60 private:
61 friend struct base::DefaultLazyInstanceTraits<MimeUtil>;
63 typedef base::hash_set<int> CodecSet;
64 typedef std::map<std::string, CodecSet> StrictMappings;
65 struct CodecEntry {
66 CodecEntry() : codec(INVALID_CODEC), is_ambiguous(true) {}
67 CodecEntry(Codec c, bool ambiguous) : codec(c), is_ambiguous(ambiguous) {}
68 Codec codec;
69 bool is_ambiguous;
71 typedef std::map<std::string, CodecEntry> StringToCodecMappings;
73 MimeUtil();
75 // For faster lookup, keep hash sets.
76 void InitializeMimeTypeMaps();
78 // Returns IsSupported if all codec IDs in |codecs| are unambiguous
79 // and are supported by the platform. MayBeSupported is returned if
80 // at least one codec ID in |codecs| is ambiguous but all the codecs
81 // are supported by the platform. IsNotSupported is returned if at
82 // least one codec ID is not supported by the platform.
83 SupportsType AreSupportedCodecs(
84 const CodecSet& supported_codecs,
85 const std::vector<std::string>& codecs) const;
87 // Converts a codec ID into an Codec enum value and indicates
88 // whether the conversion was ambiguous.
89 // Returns true if this method was able to map |codec_id| to a specific
90 // Codec enum value. |codec| and |is_ambiguous| are only valid if true
91 // is returned. Otherwise their value is undefined after the call.
92 // |is_ambiguous| is true if |codec_id| did not have enough information to
93 // unambiguously determine the proper Codec enum value. If |is_ambiguous|
94 // is true |codec| contains the best guess for the intended Codec enum value.
95 bool StringToCodec(const std::string& codec_id,
96 Codec* codec,
97 bool* is_ambiguous) const;
99 // Returns true if |codec| is supported by the platform.
100 // Note: This method will return false if the platform supports proprietary
101 // codecs but |allow_proprietary_codecs_| is set to false.
102 bool IsCodecSupported(Codec codec) const;
104 // Returns true if |codec| refers to a proprietary codec.
105 bool IsCodecProprietary(Codec codec) const;
107 // Returns true and sets |*default_codec| if |mime_type| has a default codec
108 // associated with it. Returns false otherwise and the value of
109 // |*default_codec| is undefined.
110 bool GetDefaultCodecLowerCase(const std::string& mime_type_lower_case,
111 Codec* default_codec) const;
113 // Returns true if |mime_type_lower_case| has a default codec associated with
114 // it and IsCodecSupported() returns true for that particular codec.
115 bool IsDefaultCodecSupportedLowerCase(
116 const std::string& mime_type_lower_case) const;
118 using MimeTypes = base::hash_set<std::string>;
119 MimeTypes media_map_;
121 // A map of mime_types and hash map of the supported codecs for the mime_type.
122 StrictMappings strict_format_map_;
124 // Keeps track of whether proprietary codec support should be
125 // advertised to callers.
126 bool allow_proprietary_codecs_;
128 // Lookup table for string compare based string -> Codec mappings.
129 StringToCodecMappings string_to_codec_map_;
131 DISALLOW_COPY_AND_ASSIGN(MimeUtil);
132 }; // class MimeUtil
134 // This variable is Leaky because it is accessed from WorkerPool threads.
135 static base::LazyInstance<MimeUtil>::Leaky g_media_mime_util =
136 LAZY_INSTANCE_INITIALIZER;
139 // A list of media types: http://en.wikipedia.org/wiki/Internet_media_type
140 // A comprehensive mime type list: http://plugindoc.mozdev.org/winmime.php
141 // This set of codecs is supported by all variations of Chromium.
142 static const char* const common_media_types[] = {
143 // Ogg.
144 "audio/ogg",
145 "application/ogg",
146 #if !defined(OS_ANDROID) // Android doesn't support Ogg Theora.
147 "video/ogg",
148 #endif
150 // WebM.
151 "video/webm",
152 "audio/webm",
154 // Wav.
155 "audio/wav",
156 "audio/x-wav",
158 #if defined(OS_ANDROID)
159 // HLS.
160 "application/vnd.apple.mpegurl",
161 "application/x-mpegurl",
162 #endif
165 // List of proprietary types only supported by Google Chrome.
166 static const char* const proprietary_media_types[] = {
167 // MPEG-4.
168 "video/mp4",
169 "video/x-m4v",
170 "audio/mp4",
171 "audio/x-m4a",
173 // MP3.
174 "audio/mp3",
175 "audio/x-mp3",
176 "audio/mpeg",
178 // AAC / ADTS
179 "audio/aac",
181 #if defined(ENABLE_MPEG2TS_STREAM_PARSER)
182 // MPEG-2 TS.
183 "video/mp2t",
184 #endif
187 #if defined(OS_ANDROID)
188 static bool IsCodecSupportedOnAndroid(MimeUtil::Codec codec) {
189 switch (codec) {
190 case MimeUtil::INVALID_CODEC:
191 return false;
193 case MimeUtil::PCM:
194 case MimeUtil::MP3:
195 case MimeUtil::MPEG4_AAC_LC:
196 case MimeUtil::MPEG4_AAC_SBR_v1:
197 case MimeUtil::MPEG4_AAC_SBR_PS_v2:
198 case MimeUtil::H264_BASELINE:
199 case MimeUtil::H264_MAIN:
200 case MimeUtil::H264_HIGH:
201 case MimeUtil::VP8:
202 case MimeUtil::VORBIS:
203 return true;
205 case MimeUtil::HEVC_MAIN:
206 #if defined(ENABLE_HEVC_DEMUXING)
207 // HEVC/H.265 is supported in Lollipop+ (API Level 21), according to
208 // http://developer.android.com/reference/android/media/MediaFormat.html
209 return base::android::BuildInfo::GetInstance()->sdk_int() >= 21;
210 #else
211 return false;
212 #endif
214 case MimeUtil::MPEG2_AAC_LC:
215 case MimeUtil::MPEG2_AAC_MAIN:
216 case MimeUtil::MPEG2_AAC_SSR:
217 // MPEG-2 variants of AAC are not supported on Android.
218 return false;
220 case MimeUtil::VP9:
221 // VP9 is supported only in KitKat+ (API Level 19).
222 return base::android::BuildInfo::GetInstance()->sdk_int() >= 19;
224 case MimeUtil::OPUS:
225 // Opus is supported only in Lollipop+ (API Level 21).
226 return base::android::BuildInfo::GetInstance()->sdk_int() >= 21;
228 case MimeUtil::THEORA:
229 return false;
232 return false;
234 #endif
236 struct MediaFormatStrict {
237 const char* const mime_type;
238 const char* const codecs_list;
241 // Following is the list of RFC 6381 compliant codecs:
242 // mp4a.66 - MPEG-2 AAC MAIN
243 // mp4a.67 - MPEG-2 AAC LC
244 // mp4a.68 - MPEG-2 AAC SSR
245 // mp4a.69 - MPEG-2 extension to MPEG-1
246 // mp4a.6B - MPEG-1 audio
247 // mp4a.40.2 - MPEG-4 AAC LC
248 // mp4a.40.02 - MPEG-4 AAC LC (leading 0 in aud-oti for compatibility)
249 // mp4a.40.5 - MPEG-4 HE-AAC v1 (AAC LC + SBR)
250 // mp4a.40.05 - MPEG-4 HE-AAC v1 (AAC LC + SBR) (leading 0 in aud-oti for
251 // compatibility)
252 // mp4a.40.29 - MPEG-4 HE-AAC v2 (AAC LC + SBR + PS)
254 // avc1.42E0xx - H.264 Baseline
255 // avc1.4D40xx - H.264 Main
256 // avc1.6400xx - H.264 High
257 static const char kMP4AudioCodecsExpression[] =
258 "mp4a.66,mp4a.67,mp4a.68,mp4a.69,mp4a.6B,mp4a.40.2,mp4a.40.02,mp4a.40.5,"
259 "mp4a.40.05,mp4a.40.29";
260 static const char kMP4VideoCodecsExpression[] =
261 // This is not a complete list of supported avc1 codecs. It is simply used
262 // to register support for the corresponding Codec enum. Instead of using
263 // strings in these three arrays, we should use the Codec enum values.
264 // This will avoid confusion and unnecessary parsing at runtime.
265 // kUnambiguousCodecStringMap/kAmbiguousCodecStringMap should be the only
266 // mapping from strings to codecs. See crbug.com/461009.
267 "avc1.42E00A,avc1.4D400A,avc1.64000A,"
268 #if defined(ENABLE_HEVC_DEMUXING)
269 // Any valid unambiguous HEVC codec id will work here, since these strings
270 // are parsed and mapped to MimeUtil::Codec enum values.
271 "hev1.1.6.L93.B0,"
272 #endif
273 "mp4a.66,mp4a.67,mp4a.68,mp4a.69,mp4a.6B,mp4a.40.2,mp4a.40.02,mp4a.40.5,"
274 "mp4a.40.05,mp4a.40.29";
276 // These containers are also included in
277 // common_media_types/proprietary_media_types. See crbug.com/461012.
278 static const MediaFormatStrict format_codec_mappings[] = {
279 {"video/webm", "opus,vorbis,vp8,vp8.0,vp9,vp9.0"},
280 {"audio/webm", "opus,vorbis"},
281 {"audio/wav", "1"},
282 {"audio/x-wav", "1"},
283 // Android does not support Opus in Ogg container.
284 #if defined(OS_ANDROID)
285 {"video/ogg", "theora,vorbis"},
286 {"audio/ogg", "vorbis"},
287 {"application/ogg", "theora,vorbis"},
288 #else
289 {"video/ogg", "opus,theora,vorbis"},
290 {"audio/ogg", "opus,vorbis"},
291 {"application/ogg", "opus,theora,vorbis"},
292 #endif
293 {"audio/mpeg", "mp3"},
294 {"audio/mp3", ""},
295 {"audio/x-mp3", ""},
296 {"audio/aac", ""},
297 {"audio/mp4", kMP4AudioCodecsExpression},
298 {"audio/x-m4a", kMP4AudioCodecsExpression},
299 {"video/mp4", kMP4VideoCodecsExpression},
300 {"video/x-m4v", kMP4VideoCodecsExpression},
301 {"application/x-mpegurl", kMP4VideoCodecsExpression},
302 {"application/vnd.apple.mpegurl", kMP4VideoCodecsExpression}};
304 struct CodecIDMappings {
305 const char* const codec_id;
306 MimeUtil::Codec codec;
309 // List of codec IDs that provide enough information to determine the
310 // codec and profile being requested.
312 // The "mp4a" strings come from RFC 6381.
313 static const CodecIDMappings kUnambiguousCodecStringMap[] = {
314 {"1", MimeUtil::PCM}, // We only allow this for WAV so it isn't ambiguous.
315 // avc1/avc3.XXXXXX may be unambiguous; handled by ParseH264CodecID().
316 {"mp3", MimeUtil::MP3},
317 {"mp4a.66", MimeUtil::MPEG2_AAC_MAIN},
318 {"mp4a.67", MimeUtil::MPEG2_AAC_LC},
319 {"mp4a.68", MimeUtil::MPEG2_AAC_SSR},
320 {"mp4a.69", MimeUtil::MP3},
321 {"mp4a.6B", MimeUtil::MP3},
322 {"mp4a.40.2", MimeUtil::MPEG4_AAC_LC},
323 {"mp4a.40.02", MimeUtil::MPEG4_AAC_LC},
324 {"mp4a.40.5", MimeUtil::MPEG4_AAC_SBR_v1},
325 {"mp4a.40.05", MimeUtil::MPEG4_AAC_SBR_v1},
326 {"mp4a.40.29", MimeUtil::MPEG4_AAC_SBR_PS_v2},
327 {"vorbis", MimeUtil::VORBIS},
328 {"opus", MimeUtil::OPUS},
329 {"vp8", MimeUtil::VP8},
330 {"vp8.0", MimeUtil::VP8},
331 {"vp9", MimeUtil::VP9},
332 {"vp9.0", MimeUtil::VP9},
333 {"theora", MimeUtil::THEORA}};
335 // List of codec IDs that are ambiguous and don't provide
336 // enough information to determine the codec and profile.
337 // The codec in these entries indicate the codec and profile
338 // we assume the user is trying to indicate.
339 static const CodecIDMappings kAmbiguousCodecStringMap[] = {
340 {"mp4a.40", MimeUtil::MPEG4_AAC_LC},
341 {"avc1", MimeUtil::H264_BASELINE},
342 {"avc3", MimeUtil::H264_BASELINE},
343 // avc1/avc3.XXXXXX may be ambiguous; handled by ParseH264CodecID().
346 MimeUtil::MimeUtil() : allow_proprietary_codecs_(false) {
347 InitializeMimeTypeMaps();
350 SupportsType MimeUtil::AreSupportedCodecs(
351 const CodecSet& supported_codecs,
352 const std::vector<std::string>& codecs) const {
353 DCHECK(!supported_codecs.empty());
354 DCHECK(!codecs.empty());
356 SupportsType result = IsSupported;
357 for (size_t i = 0; i < codecs.size(); ++i) {
358 bool is_ambiguous = true;
359 Codec codec = INVALID_CODEC;
360 if (!StringToCodec(codecs[i], &codec, &is_ambiguous))
361 return IsNotSupported;
363 if (!IsCodecSupported(codec) ||
364 supported_codecs.find(codec) == supported_codecs.end()) {
365 return IsNotSupported;
368 if (is_ambiguous)
369 result = MayBeSupported;
372 return result;
375 void MimeUtil::InitializeMimeTypeMaps() {
376 // Initialize the supported media types.
377 for (size_t i = 0; i < arraysize(common_media_types); ++i)
378 media_map_.insert(common_media_types[i]);
379 #if defined(USE_PROPRIETARY_CODECS)
380 allow_proprietary_codecs_ = true;
382 for (size_t i = 0; i < arraysize(proprietary_media_types); ++i)
383 media_map_.insert(proprietary_media_types[i]);
384 #endif
386 for (size_t i = 0; i < arraysize(kUnambiguousCodecStringMap); ++i) {
387 string_to_codec_map_[kUnambiguousCodecStringMap[i].codec_id] =
388 CodecEntry(kUnambiguousCodecStringMap[i].codec, false);
391 for (size_t i = 0; i < arraysize(kAmbiguousCodecStringMap); ++i) {
392 string_to_codec_map_[kAmbiguousCodecStringMap[i].codec_id] =
393 CodecEntry(kAmbiguousCodecStringMap[i].codec, true);
396 // Initialize the strict supported media types.
397 for (size_t i = 0; i < arraysize(format_codec_mappings); ++i) {
398 std::vector<std::string> mime_type_codecs;
399 ParseCodecString(format_codec_mappings[i].codecs_list,
400 &mime_type_codecs,
401 false);
403 CodecSet codecs;
404 for (size_t j = 0; j < mime_type_codecs.size(); ++j) {
405 Codec codec = INVALID_CODEC;
406 bool is_ambiguous = true;
407 CHECK(StringToCodec(mime_type_codecs[j], &codec, &is_ambiguous));
408 DCHECK(!is_ambiguous);
409 codecs.insert(codec);
412 strict_format_map_[format_codec_mappings[i].mime_type] = codecs;
416 bool MimeUtil::IsSupportedMediaMimeType(const std::string& mime_type) const {
417 return media_map_.find(base::ToLowerASCII(mime_type)) != media_map_.end();
421 bool MimeUtil::AreSupportedMediaCodecs(
422 const std::vector<std::string>& codecs) const {
423 for (size_t i = 0; i < codecs.size(); ++i) {
424 Codec codec = INVALID_CODEC;
425 bool is_ambiguous = true;
426 if (!StringToCodec(codecs[i], &codec, &is_ambiguous) ||
427 !IsCodecSupported(codec)) {
428 return false;
431 return true;
434 void MimeUtil::ParseCodecString(const std::string& codecs,
435 std::vector<std::string>* codecs_out,
436 bool strip) {
437 *codecs_out = base::SplitString(
438 base::TrimString(codecs, "\"", base::TRIM_ALL),
439 ",", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL);
441 // Convert empty or all-whitespace input to 0 results.
442 if (codecs_out->size() == 1 && (*codecs_out)[0].empty())
443 codecs_out->clear();
445 if (!strip)
446 return;
448 // Strip everything past the first '.'
449 for (std::vector<std::string>::iterator it = codecs_out->begin();
450 it != codecs_out->end();
451 ++it) {
452 size_t found = it->find_first_of('.');
453 if (found != std::string::npos)
454 it->resize(found);
458 bool MimeUtil::IsStrictMediaMimeType(const std::string& mime_type) const {
459 return strict_format_map_.find(base::ToLowerASCII(mime_type)) !=
460 strict_format_map_.end();
463 SupportsType MimeUtil::IsSupportedStrictMediaMimeType(
464 const std::string& mime_type,
465 const std::vector<std::string>& codecs) const {
466 const std::string mime_type_lower_case = base::ToLowerASCII(mime_type);
467 StrictMappings::const_iterator it_strict_map =
468 strict_format_map_.find(mime_type_lower_case);
469 if (it_strict_map == strict_format_map_.end())
470 return codecs.empty() ? MayBeSupported : IsNotSupported;
472 if (it_strict_map->second.empty()) {
473 // We get here if the mimetype does not expect a codecs parameter.
474 return (codecs.empty() &&
475 IsDefaultCodecSupportedLowerCase(mime_type_lower_case))
476 ? IsSupported
477 : IsNotSupported;
480 if (codecs.empty()) {
481 // We get here if the mimetype expects to get a codecs parameter,
482 // but didn't get one. If |mime_type_lower_case| does not have a default
483 // codec the best we can do is say "maybe" because we don't have enough
484 // information.
485 Codec default_codec = INVALID_CODEC;
486 if (!GetDefaultCodecLowerCase(mime_type_lower_case, &default_codec))
487 return MayBeSupported;
489 return IsCodecSupported(default_codec) ? IsSupported : IsNotSupported;
492 return AreSupportedCodecs(it_strict_map->second, codecs);
495 void MimeUtil::RemoveProprietaryMediaTypesAndCodecsForTests() {
496 for (size_t i = 0; i < arraysize(proprietary_media_types); ++i)
497 media_map_.erase(proprietary_media_types[i]);
498 allow_proprietary_codecs_ = false;
501 static bool IsValidH264Level(const std::string& level_str) {
502 uint32 level;
503 if (level_str.size() != 2 || !base::HexStringToUInt(level_str, &level))
504 return false;
506 // Valid levels taken from Table A-1 in ISO-14496-10.
507 // Essentially |level_str| is toHex(10 * level).
508 return ((level >= 10 && level <= 13) ||
509 (level >= 20 && level <= 22) ||
510 (level >= 30 && level <= 32) ||
511 (level >= 40 && level <= 42) ||
512 (level >= 50 && level <= 51));
515 // Handle parsing H.264 codec IDs as outlined in RFC 6381 and ISO-14496-10.
516 // avc1.42x0yy - H.264 Baseline
517 // avc1.4Dx0yy - H.264 Main
518 // avc1.64x0yy - H.264 High
520 // avc1.xxxxxx & avc3.xxxxxx are considered ambiguous forms that are trying to
521 // signal H.264 Baseline. For example, the idc_level, profile_idc and
522 // constraint_set3_flag pieces may explicitly require decoder to conform to
523 // baseline profile at the specified level (see Annex A and constraint_set0 in
524 // ISO-14496-10).
525 static bool ParseH264CodecID(const std::string& codec_id,
526 MimeUtil::Codec* codec,
527 bool* is_ambiguous) {
528 // Make sure we have avc1.xxxxxx or avc3.xxxxxx , where xxxxxx are hex digits
529 if (!base::StartsWith(codec_id, "avc1.", base::CompareCase::SENSITIVE) &&
530 !base::StartsWith(codec_id, "avc3.", base::CompareCase::SENSITIVE)) {
531 return false;
533 if (codec_id.size() != 11 ||
534 !base::IsHexDigit(codec_id[5]) || !base::IsHexDigit(codec_id[6]) ||
535 !base::IsHexDigit(codec_id[7]) || !base::IsHexDigit(codec_id[8]) ||
536 !base::IsHexDigit(codec_id[9]) || !base::IsHexDigit(codec_id[10])) {
537 return false;
540 // Validate constraint flags and reserved bits.
541 if (!base::IsHexDigit(codec_id[7]) || codec_id[8] != '0') {
542 *codec = MimeUtil::H264_BASELINE;
543 *is_ambiguous = true;
544 return true;
547 // Extract the profile.
548 std::string profile = base::ToUpperASCII(codec_id.substr(5, 2));
549 if (profile == "42") {
550 *codec = MimeUtil::H264_BASELINE;
551 } else if (profile == "4D") {
552 *codec = MimeUtil::H264_MAIN;
553 } else if (profile == "64") {
554 *codec = MimeUtil::H264_HIGH;
555 } else {
556 *codec = MimeUtil::H264_BASELINE;
557 *is_ambiguous = true;
558 return true;
561 // Validate level.
562 *is_ambiguous = !IsValidH264Level(base::ToUpperASCII(codec_id.substr(9)));
563 return true;
566 #if defined(ENABLE_HEVC_DEMUXING)
567 // ISO/IEC FDIS 14496-15 standard section E.3 describes the syntax of codec ids
568 // reserved for HEVC. According to that spec HEVC codec id must start with
569 // either "hev1." or "hvc1.". We don't yet support full parsing of HEVC codec
570 // ids, but since no other codec id starts with those string we'll just treat
571 // any string starting with "hev1." or "hvc1." as valid HEVC codec ids.
572 // crbug.com/482761
573 static bool ParseHEVCCodecID(const std::string& codec_id,
574 MimeUtil::Codec* codec,
575 bool* is_ambiguous) {
576 if (base::StartsWith(codec_id, "hev1.", base::CompareCase::SENSITIVE) ||
577 base::StartsWith(codec_id, "hvc1.", base::CompareCase::SENSITIVE)) {
578 *codec = MimeUtil::HEVC_MAIN;
580 // TODO(servolk): Full HEVC codec id parsing is not implemented yet (see
581 // crbug.com/482761). So treat HEVC codec ids as ambiguous for now.
582 *is_ambiguous = true;
584 // TODO(servolk): Most HEVC codec ids are treated as ambiguous (see above),
585 // but we need to recognize at least one valid unambiguous HEVC codec id,
586 // which is added into kMP4VideoCodecsExpression. We need it to be
587 // unambiguous to avoid DCHECK(!is_ambiguous) in InitializeMimeTypeMaps. We
588 // also use these in unit tests (see
589 // content/browser/media/media_canplaytype_browsertest.cc).
590 // Remove this workaround after crbug.com/482761 is fixed.
591 if (codec_id == "hev1.1.6.L93.B0" || codec_id == "hvc1.1.6.L93.B0") {
592 *is_ambiguous = false;
595 return true;
598 return false;
600 #endif
602 bool MimeUtil::StringToCodec(const std::string& codec_id,
603 Codec* codec,
604 bool* is_ambiguous) const {
605 StringToCodecMappings::const_iterator itr =
606 string_to_codec_map_.find(codec_id);
607 if (itr != string_to_codec_map_.end()) {
608 *codec = itr->second.codec;
609 *is_ambiguous = itr->second.is_ambiguous;
610 return true;
613 // If |codec_id| is not in |string_to_codec_map_|, then we assume that it is
614 // either H.264 or HEVC/H.265 codec ID because currently those are the only
615 // ones that are not added to the |string_to_codec_map_| and require parsing.
616 #if defined(ENABLE_HEVC_DEMUXING)
617 if (ParseHEVCCodecID(codec_id, codec, is_ambiguous)) {
618 return true;
620 #endif
621 return ParseH264CodecID(codec_id, codec, is_ambiguous);
624 bool MimeUtil::IsCodecSupported(Codec codec) const {
625 DCHECK_NE(codec, INVALID_CODEC);
627 #if defined(OS_ANDROID)
628 if (!IsCodecSupportedOnAndroid(codec))
629 return false;
630 #endif
632 return allow_proprietary_codecs_ || !IsCodecProprietary(codec);
635 bool MimeUtil::IsCodecProprietary(Codec codec) const {
636 switch (codec) {
637 case INVALID_CODEC:
638 case MP3:
639 case MPEG2_AAC_LC:
640 case MPEG2_AAC_MAIN:
641 case MPEG2_AAC_SSR:
642 case MPEG4_AAC_LC:
643 case MPEG4_AAC_SBR_v1:
644 case MPEG4_AAC_SBR_PS_v2:
645 case H264_BASELINE:
646 case H264_MAIN:
647 case H264_HIGH:
648 case HEVC_MAIN:
649 return true;
651 case PCM:
652 case VORBIS:
653 case OPUS:
654 case VP8:
655 case VP9:
656 case THEORA:
657 return false;
660 return true;
663 bool MimeUtil::GetDefaultCodecLowerCase(const std::string& mime_type_lower_case,
664 Codec* default_codec) const {
665 if (mime_type_lower_case == "audio/mpeg" ||
666 mime_type_lower_case == "audio/mp3" ||
667 mime_type_lower_case == "audio/x-mp3") {
668 *default_codec = MimeUtil::MP3;
669 return true;
672 if (mime_type_lower_case == "audio/aac") {
673 *default_codec = MimeUtil::MPEG4_AAC_LC;
674 return true;
677 return false;
680 bool MimeUtil::IsDefaultCodecSupportedLowerCase(
681 const std::string& mime_type_lower_case) const {
682 Codec default_codec = Codec::INVALID_CODEC;
683 if (!GetDefaultCodecLowerCase(mime_type_lower_case, &default_codec))
684 return false;
685 return IsCodecSupported(default_codec);
688 bool IsSupportedMediaMimeType(const std::string& mime_type) {
689 return g_media_mime_util.Get().IsSupportedMediaMimeType(mime_type);
692 bool AreSupportedMediaCodecs(const std::vector<std::string>& codecs) {
693 return g_media_mime_util.Get().AreSupportedMediaCodecs(codecs);
696 bool IsStrictMediaMimeType(const std::string& mime_type) {
697 return g_media_mime_util.Get().IsStrictMediaMimeType(mime_type);
700 SupportsType IsSupportedStrictMediaMimeType(
701 const std::string& mime_type,
702 const std::vector<std::string>& codecs) {
703 return g_media_mime_util.Get().IsSupportedStrictMediaMimeType(
704 mime_type, codecs);
707 void ParseCodecString(const std::string& codecs,
708 std::vector<std::string>* codecs_out,
709 const bool strip) {
710 g_media_mime_util.Get().ParseCodecString(codecs, codecs_out, strip);
713 void RemoveProprietaryMediaTypesAndCodecsForTests() {
714 g_media_mime_util.Get().RemoveProprietaryMediaTypesAndCodecsForTests();
717 } // namespace media