Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(155)

Side by Side Diff: net/base/mime_util.cc

Issue 1054943004: Refactoring media mime type and supported codec checks (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Make *CodecSupported* functions public (to support external overrides) Created 5 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « net/base/mime_util.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include <algorithm> 5 #include <algorithm>
6 #include <iterator> 6 #include <iterator>
7 #include <map> 7 #include <map>
8 #include <string> 8 #include <string>
9 9
10 #include "base/bind.h"
10 #include "base/containers/hash_tables.h" 11 #include "base/containers/hash_tables.h"
11 #include "base/lazy_instance.h" 12 #include "base/lazy_instance.h"
12 #include "base/logging.h" 13 #include "base/logging.h"
13 #include "base/stl_util.h" 14 #include "base/stl_util.h"
14 #include "base/strings/string_number_conversions.h" 15 #include "base/strings/string_number_conversions.h"
15 #include "base/strings/string_split.h" 16 #include "base/strings/string_split.h"
16 #include "base/strings/string_util.h" 17 #include "base/strings/string_util.h"
17 #include "base/strings/utf_string_conversions.h" 18 #include "base/strings/utf_string_conversions.h"
18 #include "net/base/mime_util.h" 19 #include "net/base/mime_util.h"
19 #include "net/base/platform_mime_util.h" 20 #include "net/base/platform_mime_util.h"
20 #include "net/http/http_util.h" 21 #include "net/http/http_util.h"
21 22
22 #if defined(OS_ANDROID) 23 #if defined(OS_ANDROID)
23 #include "base/android/build_info.h" 24 #include "base/android/build_info.h"
24 #endif 25 #endif
25 26
26 using std::string; 27 using std::string;
27 28
28 namespace net { 29 namespace net {
29 30
30 // Singleton utility class for mime types. 31 // Singleton utility class for mime types.
31 class MimeUtil : public PlatformMimeUtil { 32 class MimeUtil : public PlatformMimeUtil {
32 public: 33 public:
33 enum Codec {
34 INVALID_CODEC,
35 PCM,
36 MP3,
37 MPEG2_AAC_LC,
38 MPEG2_AAC_MAIN,
39 MPEG2_AAC_SSR,
40 MPEG4_AAC_LC,
41 MPEG4_AAC_SBR_v1,
42 MPEG4_AAC_SBR_PS_v2,
43 VORBIS,
44 OPUS,
45 H264_BASELINE,
46 H264_MAIN,
47 H264_HIGH,
48 VP8,
49 VP9,
50 THEORA
51 };
52
53 bool GetMimeTypeFromExtension(const base::FilePath::StringType& ext, 34 bool GetMimeTypeFromExtension(const base::FilePath::StringType& ext,
54 std::string* mime_type) const; 35 std::string* mime_type) const;
55 36
56 bool GetMimeTypeFromFile(const base::FilePath& file_path, 37 bool GetMimeTypeFromFile(const base::FilePath& file_path,
57 std::string* mime_type) const; 38 std::string* mime_type) const;
58 39
59 bool GetWellKnownMimeTypeFromExtension(const base::FilePath::StringType& ext, 40 bool GetWellKnownMimeTypeFromExtension(const base::FilePath::StringType& ext,
60 std::string* mime_type) const; 41 std::string* mime_type) const;
61 42
62 bool IsSupportedImageMimeType(const std::string& mime_type) const; 43 bool IsSupportedImageMimeType(const std::string& mime_type) const;
(...skipping 19 matching lines...) Expand all
82 std::vector<std::string>* codecs_out, 63 std::vector<std::string>* codecs_out,
83 bool strip); 64 bool strip);
84 65
85 bool IsStrictMediaMimeType(const std::string& mime_type) const; 66 bool IsStrictMediaMimeType(const std::string& mime_type) const;
86 SupportsType IsSupportedStrictMediaMimeType( 67 SupportsType IsSupportedStrictMediaMimeType(
87 const std::string& mime_type, 68 const std::string& mime_type,
88 const std::vector<std::string>& codecs) const; 69 const std::vector<std::string>& codecs) const;
89 70
90 void RemoveProprietaryMediaTypesAndCodecsForTests(); 71 void RemoveProprietaryMediaTypesAndCodecsForTests();
91 72
73 // Returns true if the given |codec_id|, identified by a codec id string
74 // described in RFC 6381, is supported. Returns false if the codec id is not
75 // recognized or if that codec is not supported.
76 // Note: This method will return false if the platform supports proprietary
77 // codecs but |allow_proprietary_codecs_| is set to false.
78 bool IsCodecSupported(const std::string& codec) const;
79
80 bool DefaultIsCodecSupported(const std::string& codec) const;
81
82 typedef base::Callback<bool(const std::string&)> IsCodecSupportedCB;
83 void SetIsCodecSupportedCB(IsCodecSupportedCB is_codec_supported_cb);
84
92 private: 85 private:
93 friend struct base::DefaultLazyInstanceTraits<MimeUtil>; 86 friend struct base::DefaultLazyInstanceTraits<MimeUtil>;
94 87
95 typedef base::hash_set<std::string> MimeMappings; 88 typedef base::hash_set<std::string> MimeMappings;
96 89
97 typedef base::hash_set<int> CodecSet; 90 typedef std::string CodecSet;
98 typedef std::map<std::string, CodecSet> StrictMappings; 91 typedef std::map<std::string, CodecSet> StrictMappings;
99 struct CodecEntry {
100 CodecEntry() : codec(INVALID_CODEC), is_ambiguous(true) {}
101 CodecEntry(Codec c, bool ambiguous) : codec(c), is_ambiguous(ambiguous) {}
102 Codec codec;
103 bool is_ambiguous;
104 };
105 typedef std::map<std::string, CodecEntry> StringToCodecMappings;
106 92
107 MimeUtil(); 93 MimeUtil();
108 94
109 // Returns IsSupported if all codec IDs in |codecs| are unambiguous 95 // Returns IsSupported if all codec IDs in |codecs| are unambiguous
110 // and are supported by the platform. MayBeSupported is returned if 96 // and are supported by the platform. MayBeSupported is returned if
111 // at least one codec ID in |codecs| is ambiguous but all the codecs 97 // at least one codec ID in |codecs| is ambiguous but all the codecs
112 // are supported by the platform. IsNotSupported is returned if at 98 // are supported by the platform. IsNotSupported is returned if at
113 // least one codec ID is not supported by the platform. 99 // least one codec ID is not supported by the platform.
114 SupportsType AreSupportedCodecs( 100 SupportsType AreSupportedCodecs(
115 const CodecSet& supported_codecs, 101 const CodecSet& supported_codecs,
116 const std::vector<std::string>& codecs) const; 102 const std::vector<std::string>& codecs) const;
117 103
118 // For faster lookup, keep hash sets. 104 // For faster lookup, keep hash sets.
119 void InitializeMimeTypeMaps(); 105 void InitializeMimeTypeMaps();
120 106
121 bool GetMimeTypeFromExtensionHelper(const base::FilePath::StringType& ext, 107 bool GetMimeTypeFromExtensionHelper(const base::FilePath::StringType& ext,
122 bool include_platform_types, 108 bool include_platform_types,
123 std::string* mime_type) const; 109 std::string* mime_type) const;
124 110
125 // Converts a codec ID into an Codec enum value and indicates 111 // Returns true if |codec| refers to a proprietary codec.
126 // whether the conversion was ambiguous. 112 bool IsCodecProprietary(const std::string& codec) const;
127 // Returns true if this method was able to map |codec_id| to a specific
128 // Codec enum value. |codec| and |is_ambiguous| are only valid if true
129 // is returned. Otherwise their value is undefined after the call.
130 // |is_ambiguous| is true if |codec_id| did not have enough information to
131 // unambiguously determine the proper Codec enum value. If |is_ambiguous|
132 // is true |codec| contains the best guess for the intended Codec enum value.
133 bool StringToCodec(const std::string& codec_id,
134 Codec* codec,
135 bool* is_ambiguous) const;
136 113
137 // Returns true if |codec| is supported by the platform. 114 // Returns true if |codec| is an ambiguous codec id.
138 // Note: This method will return false if the platform supports proprietary 115 bool IsCodecAmbiguous(const std::string& codec) const;
139 // codecs but |allow_proprietary_codecs_| is set to false.
140 bool IsCodecSupported(Codec codec) const;
141
142 // Returns true if |codec| refers to a proprietary codec.
143 bool IsCodecProprietary(Codec codec) const;
144 116
145 // Returns true and sets |*default_codec| if |mime_type| has a default codec 117 // Returns true and sets |*default_codec| if |mime_type| has a default codec
146 // associated with it. Returns false otherwise and the value of 118 // associated with it. Returns false otherwise and the value of
147 // |*default_codec| is undefined. 119 // |*default_codec| is undefined.
148 bool GetDefaultCodecLowerCase(const std::string& mime_type_lower_case, 120 bool GetDefaultCodecLowerCase(const std::string& mime_type_lower_case,
149 Codec* default_codec) const; 121 std::string* default_codec) const;
150
151 // Returns true if |mime_type_lower_case| has a default codec associated with
152 // it and IsCodecSupported() returns true for that particular codec.
153 bool IsDefaultCodecSupportedLowerCase(
154 const std::string& mime_type_lower_case) const;
155 122
156 MimeMappings image_map_; 123 MimeMappings image_map_;
157 MimeMappings media_map_; 124 MimeMappings media_map_;
158 MimeMappings non_image_map_; 125 MimeMappings non_image_map_;
159 MimeMappings unsupported_text_map_; 126 MimeMappings unsupported_text_map_;
160 MimeMappings javascript_map_; 127 MimeMappings javascript_map_;
161 128
162 // A map of mime_types and hash map of the supported codecs for the mime_type. 129 // A map of mime_types and hash map of the supported codecs for the mime_type.
163 StrictMappings strict_format_map_; 130 StrictMappings strict_format_map_;
164 131
165 // Keeps track of whether proprietary codec support should be 132 // Keeps track of whether proprietary codec support should be
166 // advertised to callers. 133 // advertised to callers.
167 bool allow_proprietary_codecs_; 134 bool allow_proprietary_codecs_;
168 135
169 // Lookup table for string compare based string -> Codec mappings. 136 IsCodecSupportedCB is_codec_supported_cb_;
170 StringToCodecMappings string_to_codec_map_;
171 }; // class MimeUtil 137 }; // class MimeUtil
172 138
173 // This variable is Leaky because we need to access it from WorkerPool threads. 139 // This variable is Leaky because we need to access it from WorkerPool threads.
174 static base::LazyInstance<MimeUtil>::Leaky g_mime_util = 140 static base::LazyInstance<MimeUtil>::Leaky g_mime_util =
175 LAZY_INSTANCE_INITIALIZER; 141 LAZY_INSTANCE_INITIALIZER;
176 142
177 struct MimeInfo { 143 struct MimeInfo {
178 const char* const mime_type; 144 const char* const mime_type;
179 const char* const extensions; // comma separated list 145 const char* const extensions; // comma separated list
180 }; 146 };
(...skipping 262 matching lines...) Expand 10 before | Expand all | Expand 10 after
443 "application/ecmascript", 409 "application/ecmascript",
444 "application/x-javascript", 410 "application/x-javascript",
445 "text/javascript1.1", 411 "text/javascript1.1",
446 "text/javascript1.2", 412 "text/javascript1.2",
447 "text/javascript1.3", 413 "text/javascript1.3",
448 "text/jscript", 414 "text/jscript",
449 "text/livescript" 415 "text/livescript"
450 }; 416 };
451 417
452 #if defined(OS_ANDROID) 418 #if defined(OS_ANDROID)
453 static bool IsCodecSupportedOnAndroid(MimeUtil::Codec codec) { 419 bool IsCodecSupportedOnAndroid(const std::string& codec) {
454 switch (codec) { 420 // Theora is not supported
455 case MimeUtil::INVALID_CODEC: 421 if (codec == "theora")
456 return false; 422 return false;
457 423
458 case MimeUtil::PCM: 424 // VP9 is supported only in KitKat+ (API Level 19).
459 case MimeUtil::MP3: 425 if (codec == "vp9" || codec == "vp9.0")
460 case MimeUtil::MPEG4_AAC_LC: 426 return base::android::BuildInfo::GetInstance()->sdk_int() >= 19;
461 case MimeUtil::MPEG4_AAC_SBR_v1:
462 case MimeUtil::MPEG4_AAC_SBR_PS_v2:
463 case MimeUtil::H264_BASELINE:
464 case MimeUtil::H264_MAIN:
465 case MimeUtil::H264_HIGH:
466 case MimeUtil::VP8:
467 case MimeUtil::VORBIS:
468 return true;
469 427
470 case MimeUtil::MPEG2_AAC_LC: 428 // Opus is supported only in Lollipop+ (API Level 21).
471 case MimeUtil::MPEG2_AAC_MAIN: 429 if (codec == "opus")
472 case MimeUtil::MPEG2_AAC_SSR: 430 return base::android::BuildInfo::GetInstance()->sdk_int() >= 21;
473 // MPEG-2 variants of AAC are not supported on Android.
474 return false;
475 431
476 case MimeUtil::VP9: 432 // MPEG-2 variants of AAC are not supported on Android.
477 // VP9 is supported only in KitKat+ (API Level 19). 433 // MPEG2_AAC_MAIN / MPEG2_AAC_LC / MPEG2_AAC_SSR
478 return base::android::BuildInfo::GetInstance()->sdk_int() >= 19; 434 if (codec == "mp4a.66" || codec == "mp4a.67" || codec == "mp4a.68")
435 return false;
479 436
480 case MimeUtil::OPUS: 437 // For all other codecs use the default Chromium logic to decide
481 // Opus is supported only in Lollipop+ (API Level 21). 438 // whether it's supported or not
482 return base::android::BuildInfo::GetInstance()->sdk_int() >= 21; 439 return DefaultIsCodecSupported(codec);
DaleCurtis 2015/04/15 22:44:31 Did you run these tests on Android? I'd be surpris
483
484 case MimeUtil::THEORA:
485 return false;
486 }
487
488 return false;
489 } 440 }
490 #endif 441 #endif
491 442
492 struct MediaFormatStrict { 443 struct MediaFormatStrict {
493 const char* const mime_type; 444 const char* const mime_type;
494 const char* const codecs_list; 445 const char* const codecs_list;
495 }; 446 };
496 447
497 // Following is the list of RFC 6381 compliant codecs: 448 static const char kMP4AudioCodecsExpression[] = "mp4a.*";
498 // mp4a.66 - MPEG-2 AAC MAIN
499 // mp4a.67 - MPEG-2 AAC LC
500 // mp4a.68 - MPEG-2 AAC SSR
501 // mp4a.69 - MPEG-2 extension to MPEG-1
502 // mp4a.6B - MPEG-1 audio
503 // mp4a.40.2 - MPEG-4 AAC LC
504 // mp4a.40.02 - MPEG-4 AAC LC (leading 0 in aud-oti for compatibility)
505 // mp4a.40.5 - MPEG-4 HE-AAC v1 (AAC LC + SBR)
506 // mp4a.40.05 - MPEG-4 HE-AAC v1 (AAC LC + SBR) (leading 0 in aud-oti for
507 // compatibility)
508 // mp4a.40.29 - MPEG-4 HE-AAC v2 (AAC LC + SBR + PS)
509 //
510 // avc1.42E0xx - H.264 Baseline
511 // avc1.4D40xx - H.264 Main
512 // avc1.6400xx - H.264 High
513 static const char kMP4AudioCodecsExpression[] =
514 "mp4a.66,mp4a.67,mp4a.68,mp4a.69,mp4a.6B,mp4a.40.2,mp4a.40.02,mp4a.40.5,"
515 "mp4a.40.05,mp4a.40.29";
516 static const char kMP4VideoCodecsExpression[] = 449 static const char kMP4VideoCodecsExpression[] =
517 // This is not a complete list of supported avc1 codecs. It is simply used 450 "avc1,avc3,avc1.*,avc3.*,mp4a.*";
518 // to register support for the corresponding Codec enum. Instead of using
519 // strings in these three arrays, we should use the Codec enum values.
520 // This will avoid confusion and unnecessary parsing at runtime.
521 // kUnambiguousCodecStringMap/kAmbiguousCodecStringMap should be the only
522 // mapping from strings to codecs. See crbug.com/461009.
523 "avc1.42E00A,avc1.4D400A,avc1.64000A,"
524 "mp4a.66,mp4a.67,mp4a.68,mp4a.69,mp4a.6B,mp4a.40.2,mp4a.40.02,mp4a.40.5,"
525 "mp4a.40.05,mp4a.40.29";
526 451
527 // These containers are also included in 452 // These containers are also included in
528 // common_media_types/proprietary_media_types. See crbug.com/461012. 453 // common_media_types/proprietary_media_types. See crbug.com/461012.
529 static const MediaFormatStrict format_codec_mappings[] = { 454 static const MediaFormatStrict format_codec_mappings[] = {
530 {"video/webm", "opus,vorbis,vp8,vp8.0,vp9,vp9.0"}, 455 {"video/webm", "opus,vorbis,vp8,vp8.0,vp9,vp9.0"},
531 {"audio/webm", "opus,vorbis"}, 456 {"audio/webm", "opus,vorbis"},
532 {"audio/wav", "1"}, 457 {"audio/wav", "1"},
533 {"audio/x-wav", "1"}, 458 {"audio/x-wav", "1"},
534 // Android does not support Opus in Ogg container. 459 // Android does not support Opus in Ogg container.
535 #if defined(OS_ANDROID) 460 #if defined(OS_ANDROID)
536 {"video/ogg", "theora,vorbis"}, 461 {"video/ogg", "theora,vorbis"},
537 {"audio/ogg", "vorbis"}, 462 {"audio/ogg", "vorbis"},
538 {"application/ogg", "theora,vorbis"}, 463 {"application/ogg", "theora,vorbis"},
539 #else 464 #else
540 {"video/ogg", "opus,theora,vorbis"}, 465 {"video/ogg", "opus,theora,vorbis"},
541 {"audio/ogg", "opus,vorbis"}, 466 {"audio/ogg", "opus,vorbis"},
542 {"application/ogg", "opus,theora,vorbis"}, 467 {"application/ogg", "opus,theora,vorbis"},
543 #endif 468 #endif
544 {"audio/mpeg", "mp3"}, 469 {"audio/mpeg", "mp3"},
545 {"audio/mp3", ""}, 470 {"audio/mp3", ""},
546 {"audio/x-mp3", ""}, 471 {"audio/x-mp3", ""},
547 {"audio/mp4", kMP4AudioCodecsExpression}, 472 {"audio/mp4", kMP4AudioCodecsExpression},
548 {"audio/x-m4a", kMP4AudioCodecsExpression}, 473 {"audio/x-m4a", kMP4AudioCodecsExpression},
549 {"video/mp4", kMP4VideoCodecsExpression}, 474 {"video/mp4", kMP4VideoCodecsExpression},
550 {"video/x-m4v", kMP4VideoCodecsExpression}, 475 {"video/x-m4v", kMP4VideoCodecsExpression},
551 {"application/x-mpegurl", kMP4VideoCodecsExpression}, 476 {"application/x-mpegurl", kMP4VideoCodecsExpression},
552 {"application/vnd.apple.mpegurl", kMP4VideoCodecsExpression}}; 477 {"application/vnd.apple.mpegurl", kMP4VideoCodecsExpression}};
553 478
554 struct CodecIDMappings {
555 const char* const codec_id;
556 MimeUtil::Codec codec;
557 };
558
559 // List of codec IDs that provide enough information to determine the
560 // codec and profile being requested.
561 //
562 // The "mp4a" strings come from RFC 6381.
563 static const CodecIDMappings kUnambiguousCodecStringMap[] = {
564 {"1", MimeUtil::PCM}, // We only allow this for WAV so it isn't ambiguous.
565 // avc1/avc3.XXXXXX may be unambiguous; handled by ParseH264CodecID().
566 {"mp3", MimeUtil::MP3},
567 {"mp4a.66", MimeUtil::MPEG2_AAC_MAIN},
568 {"mp4a.67", MimeUtil::MPEG2_AAC_LC},
569 {"mp4a.68", MimeUtil::MPEG2_AAC_SSR},
570 {"mp4a.69", MimeUtil::MP3},
571 {"mp4a.6B", MimeUtil::MP3},
572 {"mp4a.40.2", MimeUtil::MPEG4_AAC_LC},
573 {"mp4a.40.02", MimeUtil::MPEG4_AAC_LC},
574 {"mp4a.40.5", MimeUtil::MPEG4_AAC_SBR_v1},
575 {"mp4a.40.05", MimeUtil::MPEG4_AAC_SBR_v1},
576 {"mp4a.40.29", MimeUtil::MPEG4_AAC_SBR_PS_v2},
577 {"vorbis", MimeUtil::VORBIS},
578 {"opus", MimeUtil::OPUS},
579 {"vp8", MimeUtil::VP8},
580 {"vp8.0", MimeUtil::VP8},
581 {"vp9", MimeUtil::VP9},
582 {"vp9.0", MimeUtil::VP9},
583 {"theora", MimeUtil::THEORA}};
584
585 // List of codec IDs that are ambiguous and don't provide
586 // enough information to determine the codec and profile.
587 // The codec in these entries indicate the codec and profile
588 // we assume the user is trying to indicate.
589 static const CodecIDMappings kAmbiguousCodecStringMap[] = {
590 {"mp4a.40", MimeUtil::MPEG4_AAC_LC},
591 {"avc1", MimeUtil::H264_BASELINE},
592 {"avc3", MimeUtil::H264_BASELINE},
593 // avc1/avc3.XXXXXX may be ambiguous; handled by ParseH264CodecID().
594 };
595
596 MimeUtil::MimeUtil() : allow_proprietary_codecs_(false) { 479 MimeUtil::MimeUtil() : allow_proprietary_codecs_(false) {
480 #if defined(OS_ANDROID)
481 SetIsCodecSupportedCB(base::Bind(&IsCodecSupportedOnAndroid));
482 #endif
597 InitializeMimeTypeMaps(); 483 InitializeMimeTypeMaps();
598 } 484 }
599 485
600 SupportsType MimeUtil::AreSupportedCodecs( 486 SupportsType MimeUtil::AreSupportedCodecs(
601 const CodecSet& supported_codecs, 487 const CodecSet& supported_codecs,
602 const std::vector<std::string>& codecs) const { 488 const std::vector<std::string>& codecs) const {
603 DCHECK(!supported_codecs.empty()); 489 DCHECK(!supported_codecs.empty());
604 DCHECK(!codecs.empty()); 490 DCHECK(!codecs.empty());
605 491
492 std::vector<std::string> split_supported_codecs;
493 base::SplitString(supported_codecs, ',', &split_supported_codecs);
606 SupportsType result = IsSupported; 494 SupportsType result = IsSupported;
607 for (size_t i = 0; i < codecs.size(); ++i) { 495 for (size_t i = 0; i < codecs.size(); ++i) {
608 bool is_ambiguous = true; 496 const std::string& codec_id = codecs[i];
609 Codec codec = INVALID_CODEC; 497 if (!IsCodecSupported(codec_id)) {
610 if (!StringToCodec(codecs[i], &codec, &is_ambiguous))
611 return IsNotSupported;
612
613 if (!IsCodecSupported(codec) ||
614 supported_codecs.find(codec) == supported_codecs.end()) {
615 return IsNotSupported; 498 return IsNotSupported;
616 } 499 }
617 500
618 if (is_ambiguous) 501 bool found_in_supported = false;
502 for (size_t j = 0; j < split_supported_codecs.size(); ++j) {
503 if (MatchPattern(codec_id, split_supported_codecs[j])) {
504 found_in_supported = true;
505 break;
506 }
507 }
508
509 if (!found_in_supported) {
510 return IsNotSupported;
511 }
512
513 if (IsCodecAmbiguous(codecs[i]))
619 result = MayBeSupported; 514 result = MayBeSupported;
620 } 515 }
621
622 return result; 516 return result;
623 } 517 }
624 518
625 void MimeUtil::InitializeMimeTypeMaps() { 519 void MimeUtil::InitializeMimeTypeMaps() {
626 for (size_t i = 0; i < arraysize(supported_image_types); ++i) 520 for (size_t i = 0; i < arraysize(supported_image_types); ++i)
627 image_map_.insert(supported_image_types[i]); 521 image_map_.insert(supported_image_types[i]);
628 522
629 // Initialize the supported non-image types. 523 // Initialize the supported non-image types.
630 for (size_t i = 0; i < arraysize(supported_non_image_types); ++i) 524 for (size_t i = 0; i < arraysize(supported_non_image_types); ++i)
631 non_image_map_.insert(supported_non_image_types[i]); 525 non_image_map_.insert(supported_non_image_types[i]);
(...skipping 18 matching lines...) Expand all
650 media_map_.insert(common_media_types[i]); 544 media_map_.insert(common_media_types[i]);
651 } 545 }
652 #if defined(USE_PROPRIETARY_CODECS) 546 #if defined(USE_PROPRIETARY_CODECS)
653 for (size_t i = 0; i < arraysize(proprietary_media_types); ++i) 547 for (size_t i = 0; i < arraysize(proprietary_media_types); ++i)
654 media_map_.insert(proprietary_media_types[i]); 548 media_map_.insert(proprietary_media_types[i]);
655 #endif 549 #endif
656 550
657 for (size_t i = 0; i < arraysize(supported_javascript_types); ++i) 551 for (size_t i = 0; i < arraysize(supported_javascript_types); ++i)
658 javascript_map_.insert(supported_javascript_types[i]); 552 javascript_map_.insert(supported_javascript_types[i]);
659 553
660 for (size_t i = 0; i < arraysize(kUnambiguousCodecStringMap); ++i) {
661 string_to_codec_map_[kUnambiguousCodecStringMap[i].codec_id] =
662 CodecEntry(kUnambiguousCodecStringMap[i].codec, false);
663 }
664
665 for (size_t i = 0; i < arraysize(kAmbiguousCodecStringMap); ++i) {
666 string_to_codec_map_[kAmbiguousCodecStringMap[i].codec_id] =
667 CodecEntry(kAmbiguousCodecStringMap[i].codec, true);
668 }
669
670 // Initialize the strict supported media types. 554 // Initialize the strict supported media types.
671 for (size_t i = 0; i < arraysize(format_codec_mappings); ++i) { 555 for (size_t i = 0; i < arraysize(format_codec_mappings); ++i) {
672 std::vector<std::string> mime_type_codecs; 556 strict_format_map_[format_codec_mappings[i].mime_type] =
673 ParseCodecString(format_codec_mappings[i].codecs_list, 557 format_codec_mappings[i].codecs_list;
674 &mime_type_codecs,
675 false);
676
677 CodecSet codecs;
678 for (size_t j = 0; j < mime_type_codecs.size(); ++j) {
679 Codec codec = INVALID_CODEC;
680 bool is_ambiguous = true;
681 CHECK(StringToCodec(mime_type_codecs[j], &codec, &is_ambiguous));
682 DCHECK(!is_ambiguous);
683 codecs.insert(codec);
684 }
685
686 strict_format_map_[format_codec_mappings[i].mime_type] = codecs;
687 } 558 }
688 } 559 }
689 560
690 bool MimeUtil::IsSupportedImageMimeType(const std::string& mime_type) const { 561 bool MimeUtil::IsSupportedImageMimeType(const std::string& mime_type) const {
691 return image_map_.find(base::StringToLowerASCII(mime_type)) != 562 return image_map_.find(base::StringToLowerASCII(mime_type)) !=
692 image_map_.end(); 563 image_map_.end();
693 } 564 }
694 565
695 bool MimeUtil::IsSupportedMediaMimeType(const std::string& mime_type) const { 566 bool MimeUtil::IsSupportedMediaMimeType(const std::string& mime_type) const {
696 return media_map_.find(base::StringToLowerASCII(mime_type)) != 567 return media_map_.find(base::StringToLowerASCII(mime_type)) !=
(...skipping 163 matching lines...) Expand 10 before | Expand all | Expand 10 after
860 if (lower_type.compare(legal_top_level_types[i]) == 0) 731 if (lower_type.compare(legal_top_level_types[i]) == 0)
861 return true; 732 return true;
862 } 733 }
863 734
864 return type_string.size() > 2 && StartsWithASCII(type_string, "x-", false); 735 return type_string.size() > 2 && StartsWithASCII(type_string, "x-", false);
865 } 736 }
866 737
867 bool MimeUtil::AreSupportedMediaCodecs( 738 bool MimeUtil::AreSupportedMediaCodecs(
868 const std::vector<std::string>& codecs) const { 739 const std::vector<std::string>& codecs) const {
869 for (size_t i = 0; i < codecs.size(); ++i) { 740 for (size_t i = 0; i < codecs.size(); ++i) {
870 Codec codec = INVALID_CODEC; 741 if (!IsCodecSupported(codecs[i])) {
871 bool is_ambiguous = true;
872 if (!StringToCodec(codecs[i], &codec, &is_ambiguous) ||
873 !IsCodecSupported(codec)) {
874 return false; 742 return false;
875 } 743 }
876 } 744 }
877 return true; 745 return true;
878 } 746 }
879 747
880 void MimeUtil::ParseCodecString(const std::string& codecs, 748 void MimeUtil::ParseCodecString(const std::string& codecs,
881 std::vector<std::string>* codecs_out, 749 std::vector<std::string>* codecs_out,
882 bool strip) { 750 bool strip) {
883 std::string no_quote_codecs; 751 std::string no_quote_codecs;
(...skipping 21 matching lines...) Expand all
905 SupportsType MimeUtil::IsSupportedStrictMediaMimeType( 773 SupportsType MimeUtil::IsSupportedStrictMediaMimeType(
906 const std::string& mime_type, 774 const std::string& mime_type,
907 const std::vector<std::string>& codecs) const { 775 const std::vector<std::string>& codecs) const {
908 const std::string mime_type_lower_case = base::StringToLowerASCII(mime_type); 776 const std::string mime_type_lower_case = base::StringToLowerASCII(mime_type);
909 StrictMappings::const_iterator it_strict_map = 777 StrictMappings::const_iterator it_strict_map =
910 strict_format_map_.find(mime_type_lower_case); 778 strict_format_map_.find(mime_type_lower_case);
911 if (it_strict_map == strict_format_map_.end()) 779 if (it_strict_map == strict_format_map_.end())
912 return codecs.empty() ? MayBeSupported : IsNotSupported; 780 return codecs.empty() ? MayBeSupported : IsNotSupported;
913 781
914 if (it_strict_map->second.empty()) { 782 if (it_strict_map->second.empty()) {
915 // We get here if the mimetype does not expect a codecs parameter. 783 // We get here if the mimetype does not expect a codecs parameter.
916 return (codecs.empty() && 784 std::string default_codec = "";
917 IsDefaultCodecSupportedLowerCase(mime_type_lower_case)) 785 if (codecs.empty() &&
918 ? IsSupported 786 GetDefaultCodecLowerCase(mime_type_lower_case, &default_codec) &&
919 : IsNotSupported; 787 IsCodecSupported(default_codec)) {
788 return IsSupported;
789 }
790 return IsNotSupported;
920 } 791 }
921 792
922 if (codecs.empty()) { 793 if (codecs.empty()) {
923 // We get here if the mimetype expects to get a codecs parameter, 794 // We get here if the mimetype expects to get a codecs parameter,
924 // but didn't get one. If |mime_type_lower_case| does not have a default 795 // but didn't get one. If |mime_type_lower_case| does not have a default
925 // codec the best we can do is say "maybe" because we don't have enough 796 // codec the best we can do is say "maybe" because we don't have enough
926 // information. 797 // information.
927 Codec default_codec = INVALID_CODEC; 798 std::string default_codec = "";
928 if (!GetDefaultCodecLowerCase(mime_type_lower_case, &default_codec)) 799 if (!GetDefaultCodecLowerCase(mime_type_lower_case, &default_codec))
929 return MayBeSupported; 800 return MayBeSupported;
930 801
931 return IsCodecSupported(default_codec) ? IsSupported : IsNotSupported; 802 return IsCodecSupported(default_codec) ? IsSupported : IsNotSupported;
932 } 803 }
933 804
934 return AreSupportedCodecs(it_strict_map->second, codecs); 805 return AreSupportedCodecs(it_strict_map->second, codecs);
935 } 806 }
936 807
937 void MimeUtil::RemoveProprietaryMediaTypesAndCodecsForTests() { 808 void MimeUtil::RemoveProprietaryMediaTypesAndCodecsForTests() {
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after
993 // avc1.42y0xx, y >= 8 - H.264 Baseline 864 // avc1.42y0xx, y >= 8 - H.264 Baseline
994 // avc1.4D40xx - H.264 Main 865 // avc1.4D40xx - H.264 Main
995 // avc1.6400xx - H.264 High 866 // avc1.6400xx - H.264 High
996 // 867 //
997 // avc1.xxxxxx & avc3.xxxxxx are considered ambiguous forms that are trying to 868 // avc1.xxxxxx & avc3.xxxxxx are considered ambiguous forms that are trying to
998 // signal H.264 Baseline. For example, the idc_level, profile_idc and 869 // signal H.264 Baseline. For example, the idc_level, profile_idc and
999 // constraint_set3_flag pieces may explicitly require decoder to conform to 870 // constraint_set3_flag pieces may explicitly require decoder to conform to
1000 // baseline profile at the specified level (see Annex A and constraint_set0 in 871 // baseline profile at the specified level (see Annex A and constraint_set0 in
1001 // ISO-14496-10). 872 // ISO-14496-10).
1002 static bool ParseH264CodecID(const std::string& codec_id, 873 static bool ParseH264CodecID(const std::string& codec_id,
1003 MimeUtil::Codec* codec,
1004 bool* is_ambiguous) { 874 bool* is_ambiguous) {
1005 // Make sure we have avc1.xxxxxx or avc3.xxxxxx 875 // Make sure we have avc1.xxxxxx or avc3.xxxxxx
1006 if (codec_id.size() != 11 || 876 if (codec_id.size() != 11 ||
1007 (!StartsWithASCII(codec_id, "avc1.", true) && 877 (!StartsWithASCII(codec_id, "avc1.", true) &&
1008 !StartsWithASCII(codec_id, "avc3.", true))) { 878 !StartsWithASCII(codec_id, "avc3.", true))) {
1009 return false; 879 return false;
1010 } 880 }
1011 881
1012 std::string profile = StringToUpperASCII(codec_id.substr(5, 4)); 882 std::string profile = StringToUpperASCII(codec_id.substr(5, 4));
1013 if (IsValidH264BaselineProfile(profile)) { 883 if (IsValidH264BaselineProfile(profile) ||
1014 *codec = MimeUtil::H264_BASELINE; 884 profile == "4D40" || profile == "6400") {
1015 } else if (profile == "4D40") { 885 *is_ambiguous = !IsValidH264Level(StringToUpperASCII(codec_id.substr(9)));
1016 *codec = MimeUtil::H264_MAIN;
1017 } else if (profile == "6400") {
1018 *codec = MimeUtil::H264_HIGH;
1019 } else { 886 } else {
1020 *codec = MimeUtil::H264_BASELINE;
1021 *is_ambiguous = true; 887 *is_ambiguous = true;
1022 return true;
1023 }
1024
1025 *is_ambiguous = !IsValidH264Level(StringToUpperASCII(codec_id.substr(9)));
1026 return true;
1027 }
1028
1029 bool MimeUtil::StringToCodec(const std::string& codec_id,
1030 Codec* codec,
1031 bool* is_ambiguous) const {
1032 StringToCodecMappings::const_iterator itr =
1033 string_to_codec_map_.find(codec_id);
1034 if (itr != string_to_codec_map_.end()) {
1035 *codec = itr->second.codec;
1036 *is_ambiguous = itr->second.is_ambiguous;
1037 return true;
1038 }
1039
1040 // If |codec_id| is not in |string_to_codec_map_|, then we assume that it is
1041 // an H.264 codec ID because currently those are the only ones that can't be
1042 // stored in the |string_to_codec_map_| and require parsing.
1043 return ParseH264CodecID(codec_id, codec, is_ambiguous);
1044 }
1045
1046 bool MimeUtil::IsCodecSupported(Codec codec) const {
1047 DCHECK_NE(codec, INVALID_CODEC);
1048
1049 #if defined(OS_ANDROID)
1050 if (!IsCodecSupportedOnAndroid(codec))
1051 return false;
1052 #endif
1053
1054 return allow_proprietary_codecs_ || !IsCodecProprietary(codec);
1055 }
1056
1057 bool MimeUtil::IsCodecProprietary(Codec codec) const {
1058 switch (codec) {
1059 case INVALID_CODEC:
1060 case MP3:
1061 case MPEG2_AAC_LC:
1062 case MPEG2_AAC_MAIN:
1063 case MPEG2_AAC_SSR:
1064 case MPEG4_AAC_LC:
1065 case MPEG4_AAC_SBR_v1:
1066 case MPEG4_AAC_SBR_PS_v2:
1067 case H264_BASELINE:
1068 case H264_MAIN:
1069 case H264_HIGH:
1070 return true;
1071
1072 case PCM:
1073 case VORBIS:
1074 case OPUS:
1075 case VP8:
1076 case VP9:
1077 case THEORA:
1078 return false;
1079 } 888 }
1080 889
1081 return true; 890 return true;
1082 } 891 }
1083 892
893 bool MimeUtil::IsCodecAmbiguous(const std::string& codec_id) const {
894 if (codec_id == "avc1" || codec_id == "avc3" || codec_id == "mp4a.40")
895 return true;
896 bool is_ambiguous = false;
897 if (ParseH264CodecID(codec_id, &is_ambiguous)) {
898 return is_ambiguous;
899 }
900 return false;
901 }
902
903 bool MimeUtil::IsCodecSupported(const std::string& codec) const {
904 if (!is_codec_supported_cb_.is_null())
905 return is_codec_supported_cb_.Run(codec);
906 return DefaultIsCodecSupported(codec);
907 }
908
909 void MimeUtil::SetIsCodecSupportedCB(IsCodecSupportedCB is_codec_supported_cb) {
910 is_codec_supported_cb_ = is_codec_supported_cb;
911 }
912
913 bool MimeUtil::IsCodecProprietary(const std::string& codec) const {
914 if (codec == "1" /*PCM*/ || codec == "vorbis" || codec == "opus" ||
915 codec == "theora" || codec == "vp8" || codec == "vp8.0" ||
916 codec == "vp9" || codec == "vp9.0")
917 return false;
918
919 return true;
920 }
921
922 bool MimeUtil::DefaultIsCodecSupported(const std::string& codec) const {
923 if (!allow_proprietary_codecs_ && IsCodecProprietary(codec))
924 return false;
925
926 if (codec == "1" /*PCM*/ || codec == "vorbis" || codec == "opus" ||
927 codec == "theora" || codec == "vp8" || codec == "vp8.0" ||
928 codec == "vp9" || codec == "vp9.0")
929 return true;
930
931 if (codec == "mp3" || codec == "mp4a.66" || codec == "mp4a.67" ||
932 codec == "mp4a.68" || codec == "mp4a.69" || codec == "mp4a.6B" ||
933 codec == "mp4a.40.2" || codec == "mp4a.40.02" || codec == "mp4a.40.29" ||
934 codec == "mp4a.40.5" || codec == "mp4a.40.05" || codec == "mp4a.40")
935 return true;
936
937 bool is_ambiguous = true;
938 if (codec == "avc1" || codec == "avc3" ||
939 ParseH264CodecID(codec, &is_ambiguous)) {
940 return true;
941 }
942
943 // Unknown codec id
944 return false;
945 }
946
1084 bool MimeUtil::GetDefaultCodecLowerCase(const std::string& mime_type_lower_case, 947 bool MimeUtil::GetDefaultCodecLowerCase(const std::string& mime_type_lower_case,
1085 Codec* default_codec) const { 948 std::string* default_codec) const {
1086 if (mime_type_lower_case == "audio/mpeg" || 949 if (mime_type_lower_case == "audio/mpeg" ||
1087 mime_type_lower_case == "audio/mp3" || 950 mime_type_lower_case == "audio/mp3" ||
1088 mime_type_lower_case == "audio/x-mp3") { 951 mime_type_lower_case == "audio/x-mp3") {
1089 *default_codec = MimeUtil::MP3; 952 DCHECK(default_codec);
953 *default_codec = "mp3";
1090 return true; 954 return true;
1091 } 955 }
1092 956
1093 return false; 957 return false;
1094 } 958 }
1095 959
1096 bool MimeUtil::IsDefaultCodecSupportedLowerCase(
1097 const std::string& mime_type_lower_case) const {
1098 Codec default_codec = Codec::INVALID_CODEC;
1099 if (!GetDefaultCodecLowerCase(mime_type_lower_case, &default_codec))
1100 return false;
1101 return IsCodecSupported(default_codec);
1102 }
1103
1104 //---------------------------------------------------------------------------- 960 //----------------------------------------------------------------------------
1105 // Wrappers for the singleton 961 // Wrappers for the singleton
1106 //---------------------------------------------------------------------------- 962 //----------------------------------------------------------------------------
1107 963
1108 bool GetMimeTypeFromExtension(const base::FilePath::StringType& ext, 964 bool GetMimeTypeFromExtension(const base::FilePath::StringType& ext,
1109 std::string* mime_type) { 965 std::string* mime_type) {
1110 return g_mime_util.Get().GetMimeTypeFromExtension(ext, mime_type); 966 return g_mime_util.Get().GetMimeTypeFromExtension(ext, mime_type);
1111 } 967 }
1112 968
1113 bool GetMimeTypeFromFile(const base::FilePath& file_path, 969 bool GetMimeTypeFromFile(const base::FilePath& file_path,
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after
1179 const std::vector<std::string>& codecs) { 1035 const std::vector<std::string>& codecs) {
1180 return g_mime_util.Get().IsSupportedStrictMediaMimeType(mime_type, codecs); 1036 return g_mime_util.Get().IsSupportedStrictMediaMimeType(mime_type, codecs);
1181 } 1037 }
1182 1038
1183 void ParseCodecString(const std::string& codecs, 1039 void ParseCodecString(const std::string& codecs,
1184 std::vector<std::string>* codecs_out, 1040 std::vector<std::string>* codecs_out,
1185 const bool strip) { 1041 const bool strip) {
1186 g_mime_util.Get().ParseCodecString(codecs, codecs_out, strip); 1042 g_mime_util.Get().ParseCodecString(codecs, codecs_out, strip);
1187 } 1043 }
1188 1044
1045 bool IsCodecSupported(const std::string& codec_id) {
1046 return g_mime_util.Get().IsCodecSupported(codec_id);
1047 }
1048
1049 bool DefaultIsCodecSupported(const std::string& codec_id) {
1050 return g_mime_util.Get().DefaultIsCodecSupported(codec_id);
1051 }
1052
1053 void SetIsCodecSupportedCB(IsCodecSupportedCB is_codec_supported_cb) {
1054 return g_mime_util.Get().SetIsCodecSupportedCB(is_codec_supported_cb);
1055 }
1056
1189 namespace { 1057 namespace {
1190 1058
1191 // From http://www.w3schools.com/media/media_mimeref.asp and 1059 // From http://www.w3schools.com/media/media_mimeref.asp and
1192 // http://plugindoc.mozdev.org/winmime.php 1060 // http://plugindoc.mozdev.org/winmime.php
1193 static const char* const kStandardImageTypes[] = { 1061 static const char* const kStandardImageTypes[] = {
1194 "image/bmp", 1062 "image/bmp",
1195 "image/cis-cod", 1063 "image/cis-cod",
1196 "image/gif", 1064 "image/gif",
1197 "image/ief", 1065 "image/ief",
1198 "image/jpeg", 1066 "image/jpeg",
(...skipping 211 matching lines...) Expand 10 before | Expand all | Expand 10 after
1410 post_data->append("\r\n" + value + "\r\n"); 1278 post_data->append("\r\n" + value + "\r\n");
1411 } 1279 }
1412 1280
1413 void AddMultipartFinalDelimiterForUpload(const std::string& mime_boundary, 1281 void AddMultipartFinalDelimiterForUpload(const std::string& mime_boundary,
1414 std::string* post_data) { 1282 std::string* post_data) {
1415 DCHECK(post_data); 1283 DCHECK(post_data);
1416 post_data->append("--" + mime_boundary + "--\r\n"); 1284 post_data->append("--" + mime_boundary + "--\r\n");
1417 } 1285 }
1418 1286
1419 } // namespace net 1287 } // namespace net
OLDNEW
« no previous file with comments | « net/base/mime_util.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698