OLD | NEW |
---|---|
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/containers/hash_tables.h" | 10 #include "base/containers/hash_tables.h" |
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
71 | 71 |
72 bool IsMimeType(const std::string& type_string) const; | 72 bool IsMimeType(const std::string& type_string) const; |
73 | 73 |
74 bool AreSupportedMediaCodecs(const std::vector<std::string>& codecs) const; | 74 bool AreSupportedMediaCodecs(const std::vector<std::string>& codecs) const; |
75 | 75 |
76 void ParseCodecString(const std::string& codecs, | 76 void ParseCodecString(const std::string& codecs, |
77 std::vector<std::string>* codecs_out, | 77 std::vector<std::string>* codecs_out, |
78 bool strip); | 78 bool strip); |
79 | 79 |
80 bool IsStrictMediaMimeType(const std::string& mime_type) const; | 80 bool IsStrictMediaMimeType(const std::string& mime_type) const; |
81 bool IsSupportedStrictMediaMimeType( | 81 CanPlayType IsSupportedStrictMediaMimeType( |
82 const std::string& mime_type, | 82 const std::string& mime_type, |
83 const std::vector<std::string>& codecs) const; | 83 const std::vector<std::string>& codecs) const; |
84 | 84 |
85 void RemoveProprietaryMediaTypesAndCodecsForTests(); | 85 void RemoveProprietaryMediaTypesAndCodecsForTests(); |
86 | 86 |
87 private: | 87 private: |
88 friend struct base::DefaultLazyInstanceTraits<MimeUtil>; | 88 friend struct base::DefaultLazyInstanceTraits<MimeUtil>; |
89 | 89 |
90 typedef base::hash_set<std::string> MimeMappings; | 90 typedef base::hash_set<std::string> MimeMappings; |
91 typedef std::map<std::string, MimeMappings> StrictMappings; | 91 typedef std::map<std::string, MimeMappings> StrictMappings; |
92 | 92 |
93 typedef std::vector<std::string> MimeExpressionMappings; | |
94 typedef std::map<std::string, MimeExpressionMappings> | |
95 StrictExpressionMappings; | |
96 | |
93 MimeUtil(); | 97 MimeUtil(); |
94 | 98 |
95 // Returns true if |codecs| is nonempty and all the items in it are present in | 99 // Returns true if |codecs| is nonempty and all the items in it are present in |
96 // |supported_codecs|. | 100 // |supported_codecs|. |
97 static bool AreSupportedCodecs(const MimeMappings& supported_codecs, | 101 static bool AreSupportedCodecs(const MimeMappings& supported_codecs, |
98 const std::vector<std::string>& codecs); | 102 const std::vector<std::string>& codecs); |
103 static bool AreSupportedCodecsWithProfile( | |
104 const MimeExpressionMappings& supported_codecs, | |
105 const std::vector<std::string>& codecs); | |
99 | 106 |
100 // For faster lookup, keep hash sets. | 107 // For faster lookup, keep hash sets. |
101 void InitializeMimeTypeMaps(); | 108 void InitializeMimeTypeMaps(); |
102 | 109 |
103 bool GetMimeTypeFromExtensionHelper(const base::FilePath::StringType& ext, | 110 bool GetMimeTypeFromExtensionHelper(const base::FilePath::StringType& ext, |
104 bool include_platform_types, | 111 bool include_platform_types, |
105 std::string* mime_type) const; | 112 std::string* mime_type) const; |
106 | 113 |
107 MimeMappings image_map_; | 114 MimeMappings image_map_; |
108 MimeMappings media_map_; | 115 MimeMappings media_map_; |
109 MimeMappings non_image_map_; | 116 MimeMappings non_image_map_; |
110 MimeMappings unsupported_text_map_; | 117 MimeMappings unsupported_text_map_; |
111 MimeMappings javascript_map_; | 118 MimeMappings javascript_map_; |
112 MimeMappings codecs_map_; | 119 MimeMappings codecs_map_; |
113 | 120 |
114 StrictMappings strict_format_map_; | 121 StrictMappings strict_format_map_; |
122 StrictExpressionMappings strict_mp4_format_map_; | |
115 }; // class MimeUtil | 123 }; // class MimeUtil |
116 | 124 |
117 // This variable is Leaky because we need to access it from WorkerPool threads. | 125 // This variable is Leaky because we need to access it from WorkerPool threads. |
118 static base::LazyInstance<MimeUtil>::Leaky g_mime_util = | 126 static base::LazyInstance<MimeUtil>::Leaky g_mime_util = |
119 LAZY_INSTANCE_INITIALIZER; | 127 LAZY_INSTANCE_INITIALIZER; |
120 | 128 |
121 struct MimeInfo { | 129 struct MimeInfo { |
122 const char* mime_type; | 130 const char* mime_type; |
123 const char* extensions; // comma separated list | 131 const char* extensions; // comma separated list |
124 }; | 132 }; |
(...skipping 328 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
453 { "audio/wav", "1" }, | 461 { "audio/wav", "1" }, |
454 { "audio/x-wav", "1" }, | 462 { "audio/x-wav", "1" }, |
455 { "video/ogg", "opus,theora,vorbis" }, | 463 { "video/ogg", "opus,theora,vorbis" }, |
456 { "audio/ogg", "opus,vorbis" }, | 464 { "audio/ogg", "opus,vorbis" }, |
457 { "application/ogg", "opus,theora,vorbis" }, | 465 { "application/ogg", "opus,theora,vorbis" }, |
458 { "audio/mpeg", "" }, | 466 { "audio/mpeg", "" }, |
459 { "audio/mp3", "" }, | 467 { "audio/mp3", "" }, |
460 { "audio/x-mp3", "" } | 468 { "audio/x-mp3", "" } |
461 }; | 469 }; |
462 | 470 |
471 static const char* kProprietaryAudioCodecsExpression = | |
472 "mp4a,mp4a.40,mp4a.67,mp4a.40.?,mp4a.67.?"; | |
amogh.bihani
2014/05/16 11:25:29
acolwell@
I think we should keep mp4a in our list,
| |
473 static const char* kProprietaryCodecsExpression = | |
474 "avc1,avc3,avc1.??????,avc3.??????," | |
475 "mp4a,mp4a.40,mp4a.67,mp4a.40.?,mp4a.67.?"; | |
476 | |
477 static const MediaFormatStrict format_mp4_codec_mappings[] = { | |
478 { "audio/mp4", kProprietaryAudioCodecsExpression }, | |
479 { "audio/x-m4a", kProprietaryAudioCodecsExpression }, | |
480 { "video/mp4", kProprietaryCodecsExpression }, | |
481 { "video/x-m4v", kProprietaryCodecsExpression }, | |
482 { "application/x-mpegurl", kProprietaryCodecsExpression }, | |
483 { "application/vnd.apple.mpegurl", kProprietaryCodecsExpression } | |
484 }; | |
485 | |
463 MimeUtil::MimeUtil() { | 486 MimeUtil::MimeUtil() { |
464 InitializeMimeTypeMaps(); | 487 InitializeMimeTypeMaps(); |
465 } | 488 } |
466 | 489 |
467 // static | 490 // static |
468 bool MimeUtil::AreSupportedCodecs(const MimeMappings& supported_codecs, | 491 bool MimeUtil::AreSupportedCodecs(const MimeMappings& supported_codecs, |
469 const std::vector<std::string>& codecs) { | 492 const std::vector<std::string>& codecs) { |
470 if (supported_codecs.empty()) | 493 if (supported_codecs.empty()) |
471 return codecs.empty(); | 494 return codecs.empty(); |
472 | 495 |
473 for (size_t i = 0; i < codecs.size(); ++i) { | 496 for (size_t i = 0; i < codecs.size(); ++i) { |
474 if (supported_codecs.find(codecs[i]) == supported_codecs.end()) | 497 if (supported_codecs.find(codecs[i]) == supported_codecs.end()) |
475 return false; | 498 return false; |
476 } | 499 } |
477 return !codecs.empty(); | 500 return !codecs.empty(); |
478 } | 501 } |
479 | 502 |
503 bool MimeUtil::AreSupportedCodecsWithProfile( | |
504 const MimeExpressionMappings& supported_codecs, | |
505 const std::vector<std::string>& codecs) { | |
506 for (size_t i = 0; i < codecs.size(); ++i) { | |
ddorwin
2014/05/16 18:49:49
If the above function checks supported_codecs.empt
AmoghBihani
2014/05/16 19:18:01
This method is called only for MP4 and HLS contain
ddorwin
2014/05/20 17:47:27
Then I recommend DCHECKing this condition. Also, l
| |
507 bool codec_matched = false; | |
508 for (size_t j = 0; j < supported_codecs.size(); ++j) { | |
509 if (!MatchPattern(static_cast<base::StringPiece>(codecs[i]), | |
510 static_cast<base::StringPiece>(supported_codecs[j]))) { | |
511 continue; | |
512 } | |
513 if (codecs[i].find('.') != std::string::npos) { | |
514 if (EndsWith(codecs[i], ".", false)) | |
515 return false; | |
516 | |
517 // Check whether the suffix is hexadecimal. | |
518 std::vector<std::string> split_string; | |
519 base::SplitString(codecs[i], '.', &split_string); | |
520 const std::string& number = split_string.back(); | |
521 for (size_t k = 0; k < number.length(); ++k) { | |
522 // Don't enforce case sensitivity, even though it's called for, as it | |
523 // would break some websites. | |
524 if (!IsHexDigit(number.at(k))) | |
525 return false; | |
526 } | |
527 } | |
528 codec_matched = true; | |
529 break; | |
530 } | |
531 if (!codec_matched) | |
532 return false; | |
533 } | |
534 return !codecs.empty(); | |
535 } | |
536 | |
480 void MimeUtil::InitializeMimeTypeMaps() { | 537 void MimeUtil::InitializeMimeTypeMaps() { |
481 for (size_t i = 0; i < arraysize(supported_image_types); ++i) | 538 for (size_t i = 0; i < arraysize(supported_image_types); ++i) |
482 image_map_.insert(supported_image_types[i]); | 539 image_map_.insert(supported_image_types[i]); |
483 | 540 |
484 // Initialize the supported non-image types. | 541 // Initialize the supported non-image types. |
485 for (size_t i = 0; i < arraysize(supported_non_image_types); ++i) | 542 for (size_t i = 0; i < arraysize(supported_non_image_types); ++i) |
486 non_image_map_.insert(supported_non_image_types[i]); | 543 non_image_map_.insert(supported_non_image_types[i]); |
487 for (size_t i = 0; i < arraysize(supported_certificate_types); ++i) | 544 for (size_t i = 0; i < arraysize(supported_certificate_types); ++i) |
488 non_image_map_.insert(supported_certificate_types[i].mime_type); | 545 non_image_map_.insert(supported_certificate_types[i].mime_type); |
489 for (size_t i = 0; i < arraysize(unsupported_text_types); ++i) | 546 for (size_t i = 0; i < arraysize(unsupported_text_types); ++i) |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
540 MimeMappings codecs; | 597 MimeMappings codecs; |
541 for (size_t j = 0; j < mime_type_codecs.size(); ++j) { | 598 for (size_t j = 0; j < mime_type_codecs.size(); ++j) { |
542 #if defined(OS_ANDROID) | 599 #if defined(OS_ANDROID) |
543 if (!IsCodecSupportedOnAndroid(mime_type_codecs[j])) | 600 if (!IsCodecSupportedOnAndroid(mime_type_codecs[j])) |
544 continue; | 601 continue; |
545 #endif | 602 #endif |
546 codecs.insert(mime_type_codecs[j]); | 603 codecs.insert(mime_type_codecs[j]); |
547 } | 604 } |
548 strict_format_map_[format_codec_mappings[i].mime_type] = codecs; | 605 strict_format_map_[format_codec_mappings[i].mime_type] = codecs; |
549 } | 606 } |
607 for (size_t i = 0; i < arraysize(format_mp4_codec_mappings); ++i) { | |
608 std::vector<std::string> mime_type_codecs; | |
609 ParseCodecString( | |
610 format_mp4_codec_mappings[i].codecs_list, &mime_type_codecs, false); | |
611 | |
612 MimeExpressionMappings codecs; | |
613 for (size_t j = 0; j < mime_type_codecs.size(); ++j) { | |
614 codecs.push_back(mime_type_codecs[j]); | |
615 } | |
616 strict_mp4_format_map_[format_mp4_codec_mappings[i].mime_type] = codecs; | |
617 } | |
550 } | 618 } |
551 | 619 |
552 bool MimeUtil::IsSupportedImageMimeType(const std::string& mime_type) const { | 620 bool MimeUtil::IsSupportedImageMimeType(const std::string& mime_type) const { |
553 return image_map_.find(mime_type) != image_map_.end(); | 621 return image_map_.find(mime_type) != image_map_.end(); |
554 } | 622 } |
555 | 623 |
556 bool MimeUtil::IsSupportedMediaMimeType(const std::string& mime_type) const { | 624 bool MimeUtil::IsSupportedMediaMimeType(const std::string& mime_type) const { |
557 return media_map_.find(mime_type) != media_map_.end(); | 625 return media_map_.find(mime_type) != media_map_.end(); |
558 } | 626 } |
559 | 627 |
(...skipping 162 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
722 for (std::vector<std::string>::iterator it = codecs_out->begin(); | 790 for (std::vector<std::string>::iterator it = codecs_out->begin(); |
723 it != codecs_out->end(); | 791 it != codecs_out->end(); |
724 ++it) { | 792 ++it) { |
725 size_t found = it->find_first_of('.'); | 793 size_t found = it->find_first_of('.'); |
726 if (found != std::string::npos) | 794 if (found != std::string::npos) |
727 it->resize(found); | 795 it->resize(found); |
728 } | 796 } |
729 } | 797 } |
730 | 798 |
731 bool MimeUtil::IsStrictMediaMimeType(const std::string& mime_type) const { | 799 bool MimeUtil::IsStrictMediaMimeType(const std::string& mime_type) const { |
732 if (strict_format_map_.find(mime_type) == strict_format_map_.end()) | 800 if (strict_format_map_.find(mime_type) == strict_format_map_.end() && |
801 strict_mp4_format_map_.find(mime_type) == strict_mp4_format_map_.end()) | |
733 return false; | 802 return false; |
734 return true; | 803 return true; |
735 } | 804 } |
736 | 805 |
737 bool MimeUtil::IsSupportedStrictMediaMimeType( | 806 CanPlayType MimeUtil::IsSupportedStrictMediaMimeType( |
738 const std::string& mime_type, | 807 const std::string& mime_type, |
739 const std::vector<std::string>& codecs) const { | 808 const std::vector<std::string>& codecs) const { |
740 StrictMappings::const_iterator it = strict_format_map_.find(mime_type); | 809 StrictMappings::const_iterator it = strict_format_map_.find(mime_type); |
741 return (it != strict_format_map_.end()) && | 810 if (it != strict_format_map_.end() && AreSupportedCodecs(it->second, codecs)) |
742 AreSupportedCodecs(it->second, codecs); | 811 return IsSupported; |
812 | |
813 StrictExpressionMappings::const_iterator iter = | |
ddorwin
2014/05/16 18:49:49
I don't think "it" and "iter" should be used in th
| |
814 strict_mp4_format_map_.find(mime_type); | |
815 if ((iter != strict_mp4_format_map_.end()) && | |
816 AreSupportedCodecsWithProfile(iter->second, codecs)) { | |
817 return MayBeSupported; | |
818 } | |
819 | |
820 if (codecs.empty()) | |
ddorwin
2014/05/16 18:49:49
Ideally, this should bail early, but we also have
AmoghBihani
2014/05/16 19:18:01
This was done specifically because mp3 containers
| |
821 return MayBeSupported; | |
822 | |
823 return IsNotSupported; | |
743 } | 824 } |
744 | 825 |
745 void MimeUtil::RemoveProprietaryMediaTypesAndCodecsForTests() { | 826 void MimeUtil::RemoveProprietaryMediaTypesAndCodecsForTests() { |
746 for (size_t i = 0; i < arraysize(proprietary_media_types); ++i) { | 827 for (size_t i = 0; i < arraysize(proprietary_media_types); ++i) { |
747 non_image_map_.erase(proprietary_media_types[i]); | 828 non_image_map_.erase(proprietary_media_types[i]); |
748 media_map_.erase(proprietary_media_types[i]); | 829 media_map_.erase(proprietary_media_types[i]); |
749 } | 830 } |
750 for (size_t i = 0; i < arraysize(proprietary_media_codecs); ++i) | 831 for (size_t i = 0; i < arraysize(proprietary_media_codecs); ++i) |
751 codecs_map_.erase(proprietary_media_codecs[i]); | 832 codecs_map_.erase(proprietary_media_codecs[i]); |
752 } | 833 } |
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
810 } | 891 } |
811 | 892 |
812 bool AreSupportedMediaCodecs(const std::vector<std::string>& codecs) { | 893 bool AreSupportedMediaCodecs(const std::vector<std::string>& codecs) { |
813 return g_mime_util.Get().AreSupportedMediaCodecs(codecs); | 894 return g_mime_util.Get().AreSupportedMediaCodecs(codecs); |
814 } | 895 } |
815 | 896 |
816 bool IsStrictMediaMimeType(const std::string& mime_type) { | 897 bool IsStrictMediaMimeType(const std::string& mime_type) { |
817 return g_mime_util.Get().IsStrictMediaMimeType(mime_type); | 898 return g_mime_util.Get().IsStrictMediaMimeType(mime_type); |
818 } | 899 } |
819 | 900 |
820 bool IsSupportedStrictMediaMimeType(const std::string& mime_type, | 901 CanPlayType IsSupportedStrictMediaMimeType( |
821 const std::vector<std::string>& codecs) { | 902 const std::string& mime_type, |
903 const std::vector<std::string>& codecs) { | |
822 return g_mime_util.Get().IsSupportedStrictMediaMimeType(mime_type, codecs); | 904 return g_mime_util.Get().IsSupportedStrictMediaMimeType(mime_type, codecs); |
823 } | 905 } |
824 | 906 |
825 void ParseCodecString(const std::string& codecs, | 907 void ParseCodecString(const std::string& codecs, |
826 std::vector<std::string>* codecs_out, | 908 std::vector<std::string>* codecs_out, |
827 const bool strip) { | 909 const bool strip) { |
828 g_mime_util.Get().ParseCodecString(codecs, codecs_out, strip); | 910 g_mime_util.Get().ParseCodecString(codecs, codecs_out, strip); |
829 } | 911 } |
830 | 912 |
831 namespace { | 913 namespace { |
(...skipping 228 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1060 post_data->append("\r\n" + value + "\r\n"); | 1142 post_data->append("\r\n" + value + "\r\n"); |
1061 } | 1143 } |
1062 | 1144 |
1063 void AddMultipartFinalDelimiterForUpload(const std::string& mime_boundary, | 1145 void AddMultipartFinalDelimiterForUpload(const std::string& mime_boundary, |
1064 std::string* post_data) { | 1146 std::string* post_data) { |
1065 DCHECK(post_data); | 1147 DCHECK(post_data); |
1066 post_data->append("--" + mime_boundary + "--\r\n"); | 1148 post_data->append("--" + mime_boundary + "--\r\n"); |
1067 } | 1149 } |
1068 | 1150 |
1069 } // namespace net | 1151 } // namespace net |
OLD | NEW |