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

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: Addressing comments 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
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,mp4a.40,mp4a.67,mp4a.40.?,mp4a.67.?";
acolwell GONE FROM CHROMIUM 2014/05/22 20:21:42 mp4a is not RFC compliant.
473 static const char* kProprietaryCodecsExpression =
474 "avc1,avc3,avc1.??????,avc3.??????,"
475 "mp4a,mp4a.40,mp4a.67,mp4a.40.?,mp4a.67.?";
acolwell GONE FROM CHROMIUM 2014/05/22 20:21:42 mp4a is not RFC compliant.
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 DCHECK(!supported_codecs.empty());
507 for (size_t i = 0; i < codecs.size(); ++i) {
508 bool codec_matched = false;
509 for (size_t j = 0; j < supported_codecs.size(); ++j) {
510 if (!MatchPattern(static_cast<base::StringPiece>(codecs[i]),
acolwell GONE FROM CHROMIUM 2014/05/22 20:21:42 nit: static_cast doesn't seem like the right thing
511 static_cast<base::StringPiece>(supported_codecs[j]))) {
512 continue;
513 }
514 if (codecs[i].find('.') != std::string::npos) {
acolwell GONE FROM CHROMIUM 2014/05/22 20:21:42 You seem to be searching for '.' three different w
amogh.bihani 2014/05/23 10:57:29 Wow, the second approach is awesome. :)
515 if (EndsWith(codecs[i], ".", false))
516 return false;
517
518 // Check whether the suffix is hexadecimal.
519 std::vector<std::string> split_string;
520 base::SplitString(codecs[i], '.', &split_string);
521 const std::string& number = split_string.back();
522 for (size_t k = 0; k < number.length(); ++k) {
523 // Don't enforce case sensitivity, even though it's called for, as it
524 // would break some websites.
525 if (!IsHexDigit(number.at(k)))
526 return false;
527 }
528 }
529 codec_matched = true;
530 break;
531 }
532 if (!codec_matched)
533 return false;
534 }
535 return !codecs.empty();
536 }
537
480 void MimeUtil::InitializeMimeTypeMaps() { 538 void MimeUtil::InitializeMimeTypeMaps() {
481 for (size_t i = 0; i < arraysize(supported_image_types); ++i) 539 for (size_t i = 0; i < arraysize(supported_image_types); ++i)
482 image_map_.insert(supported_image_types[i]); 540 image_map_.insert(supported_image_types[i]);
483 541
484 // Initialize the supported non-image types. 542 // Initialize the supported non-image types.
485 for (size_t i = 0; i < arraysize(supported_non_image_types); ++i) 543 for (size_t i = 0; i < arraysize(supported_non_image_types); ++i)
486 non_image_map_.insert(supported_non_image_types[i]); 544 non_image_map_.insert(supported_non_image_types[i]);
487 for (size_t i = 0; i < arraysize(supported_certificate_types); ++i) 545 for (size_t i = 0; i < arraysize(supported_certificate_types); ++i)
488 non_image_map_.insert(supported_certificate_types[i].mime_type); 546 non_image_map_.insert(supported_certificate_types[i].mime_type);
489 for (size_t i = 0; i < arraysize(unsupported_text_types); ++i) 547 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; 598 MimeMappings codecs;
541 for (size_t j = 0; j < mime_type_codecs.size(); ++j) { 599 for (size_t j = 0; j < mime_type_codecs.size(); ++j) {
542 #if defined(OS_ANDROID) 600 #if defined(OS_ANDROID)
543 if (!IsCodecSupportedOnAndroid(mime_type_codecs[j])) 601 if (!IsCodecSupportedOnAndroid(mime_type_codecs[j]))
544 continue; 602 continue;
545 #endif 603 #endif
546 codecs.insert(mime_type_codecs[j]); 604 codecs.insert(mime_type_codecs[j]);
547 } 605 }
548 strict_format_map_[format_codec_mappings[i].mime_type] = codecs; 606 strict_format_map_[format_codec_mappings[i].mime_type] = codecs;
549 } 607 }
608 for (size_t i = 0; i < arraysize(format_mp4_codec_mappings); ++i) {
609 std::vector<std::string> mime_type_codecs;
610 ParseCodecString(
611 format_mp4_codec_mappings[i].codecs_list, &mime_type_codecs, false);
612
613 MimeExpressionMappings codecs;
614 for (size_t j = 0; j < mime_type_codecs.size(); ++j) {
615 codecs.push_back(mime_type_codecs[j]);
616 }
617 strict_mp4_format_map_[format_mp4_codec_mappings[i].mime_type] = codecs;
618 }
550 } 619 }
551 620
552 bool MimeUtil::IsSupportedImageMimeType(const std::string& mime_type) const { 621 bool MimeUtil::IsSupportedImageMimeType(const std::string& mime_type) const {
553 return image_map_.find(mime_type) != image_map_.end(); 622 return image_map_.find(mime_type) != image_map_.end();
554 } 623 }
555 624
556 bool MimeUtil::IsSupportedMediaMimeType(const std::string& mime_type) const { 625 bool MimeUtil::IsSupportedMediaMimeType(const std::string& mime_type) const {
557 return media_map_.find(mime_type) != media_map_.end(); 626 return media_map_.find(mime_type) != media_map_.end();
558 } 627 }
559 628
(...skipping 162 matching lines...) Expand 10 before | Expand all | Expand 10 after
722 for (std::vector<std::string>::iterator it = codecs_out->begin(); 791 for (std::vector<std::string>::iterator it = codecs_out->begin();
723 it != codecs_out->end(); 792 it != codecs_out->end();
724 ++it) { 793 ++it) {
725 size_t found = it->find_first_of('.'); 794 size_t found = it->find_first_of('.');
726 if (found != std::string::npos) 795 if (found != std::string::npos)
727 it->resize(found); 796 it->resize(found);
728 } 797 }
729 } 798 }
730 799
731 bool MimeUtil::IsStrictMediaMimeType(const std::string& mime_type) const { 800 bool MimeUtil::IsStrictMediaMimeType(const std::string& mime_type) const {
732 if (strict_format_map_.find(mime_type) == strict_format_map_.end()) 801 if (strict_format_map_.find(mime_type) == strict_format_map_.end() &&
802 strict_mp4_format_map_.find(mime_type) == strict_mp4_format_map_.end())
733 return false; 803 return false;
734 return true; 804 return true;
735 } 805 }
736 806
737 bool MimeUtil::IsSupportedStrictMediaMimeType( 807 CanPlayType MimeUtil::IsSupportedStrictMediaMimeType(
738 const std::string& mime_type, 808 const std::string& mime_type,
739 const std::vector<std::string>& codecs) const { 809 const std::vector<std::string>& codecs) const {
740 StrictMappings::const_iterator it = strict_format_map_.find(mime_type); 810 StrictMappings::const_iterator it_strict_map =
741 return (it != strict_format_map_.end()) && 811 strict_format_map_.find(mime_type);
742 AreSupportedCodecs(it->second, codecs); 812 if ((it_strict_map != strict_format_map_.end()) &&
813 AreSupportedCodecs(it_strict_map->second, codecs)) {
814 return IsSupported;
815 }
816
817 StrictExpressionMappings::const_iterator it_expression_map =
818 strict_mp4_format_map_.find(mime_type);
819 if ((it_expression_map != strict_mp4_format_map_.end()) &&
820 AreSupportedCodecsWithProfile(it_expression_map->second, codecs)) {
821 return MayBeSupported;
822 }
823
824 if (codecs.empty())
825 return MayBeSupported;
826
827 return IsNotSupported;
743 } 828 }
744 829
745 void MimeUtil::RemoveProprietaryMediaTypesAndCodecsForTests() { 830 void MimeUtil::RemoveProprietaryMediaTypesAndCodecsForTests() {
746 for (size_t i = 0; i < arraysize(proprietary_media_types); ++i) { 831 for (size_t i = 0; i < arraysize(proprietary_media_types); ++i) {
747 non_image_map_.erase(proprietary_media_types[i]); 832 non_image_map_.erase(proprietary_media_types[i]);
748 media_map_.erase(proprietary_media_types[i]); 833 media_map_.erase(proprietary_media_types[i]);
749 } 834 }
750 for (size_t i = 0; i < arraysize(proprietary_media_codecs); ++i) 835 for (size_t i = 0; i < arraysize(proprietary_media_codecs); ++i)
751 codecs_map_.erase(proprietary_media_codecs[i]); 836 codecs_map_.erase(proprietary_media_codecs[i]);
752 } 837 }
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after
810 } 895 }
811 896
812 bool AreSupportedMediaCodecs(const std::vector<std::string>& codecs) { 897 bool AreSupportedMediaCodecs(const std::vector<std::string>& codecs) {
813 return g_mime_util.Get().AreSupportedMediaCodecs(codecs); 898 return g_mime_util.Get().AreSupportedMediaCodecs(codecs);
814 } 899 }
815 900
816 bool IsStrictMediaMimeType(const std::string& mime_type) { 901 bool IsStrictMediaMimeType(const std::string& mime_type) {
817 return g_mime_util.Get().IsStrictMediaMimeType(mime_type); 902 return g_mime_util.Get().IsStrictMediaMimeType(mime_type);
818 } 903 }
819 904
820 bool IsSupportedStrictMediaMimeType(const std::string& mime_type, 905 CanPlayType IsSupportedStrictMediaMimeType(
821 const std::vector<std::string>& codecs) { 906 const std::string& mime_type,
907 const std::vector<std::string>& codecs) {
822 return g_mime_util.Get().IsSupportedStrictMediaMimeType(mime_type, codecs); 908 return g_mime_util.Get().IsSupportedStrictMediaMimeType(mime_type, codecs);
823 } 909 }
824 910
825 void ParseCodecString(const std::string& codecs, 911 void ParseCodecString(const std::string& codecs,
826 std::vector<std::string>* codecs_out, 912 std::vector<std::string>* codecs_out,
827 const bool strip) { 913 const bool strip) {
828 g_mime_util.Get().ParseCodecString(codecs, codecs_out, strip); 914 g_mime_util.Get().ParseCodecString(codecs, codecs_out, strip);
829 } 915 }
830 916
831 namespace { 917 namespace {
(...skipping 228 matching lines...) Expand 10 before | Expand all | Expand 10 after
1060 post_data->append("\r\n" + value + "\r\n"); 1146 post_data->append("\r\n" + value + "\r\n");
1061 } 1147 }
1062 1148
1063 void AddMultipartFinalDelimiterForUpload(const std::string& mime_boundary, 1149 void AddMultipartFinalDelimiterForUpload(const std::string& mime_boundary,
1064 std::string* post_data) { 1150 std::string* post_data) {
1065 DCHECK(post_data); 1151 DCHECK(post_data);
1066 post_data->append("--" + mime_boundary + "--\r\n"); 1152 post_data->append("--" + mime_boundary + "--\r\n");
1067 } 1153 }
1068 1154
1069 } // namespace net 1155 } // namespace net
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698