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

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

Issue 254983006: Fix: Adding list of supported codecs for MP4 containers (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Making IsSupportedStrictMediaMimeType return enum Created 6 years, 7 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
« net/base/mime_util.h ('K') | « 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/containers/hash_tables.h" 10 #include "base/containers/hash_tables.h"
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after
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
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.40,mp4a.67,mp4a.40.?,mp4a.67.?";
473 static const char* kProprietaryCodecsExpression =
474 "avc1,avc3,avc1.??????,avc3.??????,mp4a.40,mp4a.67,mp4a.40.?,mp4a.67.?";
475
476 static const MediaFormatStrict format_mp4_codec_mappings[] = {
477 { "audio/mp4", kProprietaryAudioCodecsExpression },
478 { "audio/x-m4a", kProprietaryAudioCodecsExpression },
479 { "video/mp4", kProprietaryCodecsExpression },
480 { "video/x-m4v", kProprietaryCodecsExpression },
481 { "application/x-mpegurl", kProprietaryCodecsExpression },
482 { "application/vnd.apple.mpegurl", kProprietaryCodecsExpression }
483 };
484
463 MimeUtil::MimeUtil() { 485 MimeUtil::MimeUtil() {
464 InitializeMimeTypeMaps(); 486 InitializeMimeTypeMaps();
465 } 487 }
466 488
467 // static 489 // static
468 bool MimeUtil::AreSupportedCodecs(const MimeMappings& supported_codecs, 490 bool MimeUtil::AreSupportedCodecs(const MimeMappings& supported_codecs,
469 const std::vector<std::string>& codecs) { 491 const std::vector<std::string>& codecs) {
470 if (supported_codecs.empty()) 492 if (supported_codecs.empty())
471 return codecs.empty(); 493 return codecs.empty();
472 494
473 for (size_t i = 0; i < codecs.size(); ++i) { 495 for (size_t i = 0; i < codecs.size(); ++i) {
474 if (supported_codecs.find(codecs[i]) == supported_codecs.end()) 496 if (supported_codecs.find(codecs[i]) == supported_codecs.end())
475 return false; 497 return false;
476 } 498 }
477 return !codecs.empty(); 499 return !codecs.empty();
478 } 500 }
479 501
502 bool MimeUtil::AreSupportedCodecsWithProfile(
503 const MimeExpressionMappings& supported_codecs,
504 const std::vector<std::string>& codecs) {
505 for (size_t i = 0; i < codecs.size(); ++i) {
506 bool codec_matched = false;
507 for (size_t j = 0; j < supported_codecs.size(); ++j) {
508 if (!MatchPattern(static_cast<base::StringPiece>(codecs[i]),
509 static_cast<base::StringPiece>(supported_codecs[j]))) {
510 continue;
511 }
512 if (codecs[i].find('.') != std::string::npos) {
513 if (EndsWith(codecs[i], ".", false))
514 return false;
515
516 // We check whether the suffix is hexadecimal or not.
Ryan Sleevi 2014/05/15 22:48:35 Pronouns in comments considered harmful - "We", in
amogh.bihani 2014/05/16 11:25:29 Done.
517 std::vector<std::string> split_string;
518 base::SplitString(codecs[i], '.', &split_string);
519 std::string number = split_string.back();
Ryan Sleevi 2014/05/15 22:48:35 const std::string&
amogh.bihani 2014/05/16 11:25:29 Done.
520 for (size_t k = 0; k < number.length(); ++k) {
521 // We are neglecting case sensitivity as it might break some websites.
Ryan Sleevi 2014/05/15 22:48:35 // Don't enforce case sensitivity, even though it'
amogh.bihani 2014/05/16 11:25:29 Done.
522 if (!IsHexDigit(number.at(k)))
523 return false;
524 }
525 }
526 // We have a match! Now no need to compare it with other supported codecs.
Ryan Sleevi 2014/05/15 22:48:35 update
amogh.bihani 2014/05/16 11:25:29 removed it. The "break" explains this.
527 codec_matched = true;
528 break;
529 }
530 // Return false if any of the codecs is not matching.
Ryan Sleevi 2014/05/15 22:48:35 This is perhaps too descriptive - the next two lin
amogh.bihani 2014/05/16 11:25:29 Done.
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
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
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 =
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())
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
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
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
OLDNEW
« net/base/mime_util.h ('K') | « net/base/mime_util.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698