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

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: 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
« 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/containers/hash_tables.h" 10 #include "base/containers/hash_tables.h"
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after
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 bool 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 bool IsSupportedStrictMP4MediaMimeType(
85 const std::string& mime_type,
86 const std::vector<std::string>& codecs) const;
84 87
85 void RemoveProprietaryMediaTypesAndCodecsForTests(); 88 void RemoveProprietaryMediaTypesAndCodecsForTests();
86 89
87 private: 90 private:
88 friend struct base::DefaultLazyInstanceTraits<MimeUtil>; 91 friend struct base::DefaultLazyInstanceTraits<MimeUtil>;
89 92
90 typedef base::hash_set<std::string> MimeMappings; 93 typedef base::hash_set<std::string> MimeMappings;
91 typedef std::map<std::string, MimeMappings> StrictMappings; 94 typedef std::map<std::string, MimeMappings> StrictMappings;
92 95
96 typedef std::vector<std::string> MimeExpressionMappings;
97 typedef std::map<std::string, MimeExpressionMappings>
98 StrictExpressionMappings;
Ryan Sleevi 2014/04/28 22:02:55 style: Formatting is wrong here. Use git-cl format
99
93 MimeUtil(); 100 MimeUtil();
94 101
95 // Returns true if |codecs| is nonempty and all the items in it are present in 102 // Returns true if |codecs| is nonempty and all the items in it are present in
96 // |supported_codecs|. 103 // |supported_codecs|.
97 static bool AreSupportedCodecs(const MimeMappings& supported_codecs, 104 static bool AreSupportedCodecs(const MimeMappings& supported_codecs,
98 const std::vector<std::string>& codecs); 105 const std::vector<std::string>& codecs);
106 static bool AreSupportedCodecsWithProfile(
107 const MimeExpressionMappings& supported_codecs,
108 const std::vector<std::string>& codecs);
Ryan Sleevi 2014/04/28 22:02:55 Style: Formatting is wrong here
99 109
100 // For faster lookup, keep hash sets. 110 // For faster lookup, keep hash sets.
101 void InitializeMimeTypeMaps(); 111 void InitializeMimeTypeMaps();
102 112
103 bool GetMimeTypeFromExtensionHelper(const base::FilePath::StringType& ext, 113 bool GetMimeTypeFromExtensionHelper(const base::FilePath::StringType& ext,
104 bool include_platform_types, 114 bool include_platform_types,
105 std::string* mime_type) const; 115 std::string* mime_type) const;
106 116
107 MimeMappings image_map_; 117 MimeMappings image_map_;
108 MimeMappings media_map_; 118 MimeMappings media_map_;
109 MimeMappings non_image_map_; 119 MimeMappings non_image_map_;
110 MimeMappings unsupported_text_map_; 120 MimeMappings unsupported_text_map_;
111 MimeMappings javascript_map_; 121 MimeMappings javascript_map_;
112 MimeMappings codecs_map_; 122 MimeMappings codecs_map_;
113 123
114 StrictMappings strict_format_map_; 124 StrictMappings strict_format_map_;
125 StrictExpressionMappings strict_mp4_format_map_;
115 }; // class MimeUtil 126 }; // class MimeUtil
116 127
117 // This variable is Leaky because we need to access it from WorkerPool threads. 128 // This variable is Leaky because we need to access it from WorkerPool threads.
118 static base::LazyInstance<MimeUtil>::Leaky g_mime_util = 129 static base::LazyInstance<MimeUtil>::Leaky g_mime_util =
119 LAZY_INSTANCE_INITIALIZER; 130 LAZY_INSTANCE_INITIALIZER;
120 131
121 struct MimeInfo { 132 struct MimeInfo {
122 const char* mime_type; 133 const char* mime_type;
123 const char* extensions; // comma separated list 134 const char* extensions; // comma separated list
124 }; 135 };
(...skipping 312 matching lines...) Expand 10 before | Expand all | Expand 10 after
437 { "audio/wav", "1" }, 448 { "audio/wav", "1" },
438 { "audio/x-wav", "1" }, 449 { "audio/x-wav", "1" },
439 { "video/ogg", "opus,theora,vorbis" }, 450 { "video/ogg", "opus,theora,vorbis" },
440 { "audio/ogg", "opus,vorbis" }, 451 { "audio/ogg", "opus,vorbis" },
441 { "application/ogg", "opus,theora,vorbis" }, 452 { "application/ogg", "opus,theora,vorbis" },
442 { "audio/mpeg", "" }, 453 { "audio/mpeg", "" },
443 { "audio/mp3", "" }, 454 { "audio/mp3", "" },
444 { "audio/x-mp3", "" } 455 { "audio/x-mp3", "" }
445 }; 456 };
446 457
458 static const MediaFormatStrict format_mp4_codec_mappings[] = {
459 { "audio/mp4", "mp4a,mp4a.40,mp4a.40.?" },
460 { "audio/x-m4a", "mp4a,mp4a.40,mp4a.40.?" },
461 { "video/mp4", "avc1,avc3,avc1.??????,avc3.??????,mp4a,mp4a.40,mp4a.40.?" },
462 { "video/x-m4v", "avc1,avc3,avc1.??????,avc3.??????,mp4a,mp4a.40,mp4a.40.?" },
463 { "application/x-mpegurl",
464 "avc1,avc3,avc1.??????,avc3.??????,mp4a,mp4a.40,mp4a.40.?" },
465 { "application/vnd.apple.mpegurl",
466 "avc1,avc3,avc1.??????,avc3.??????,mp4a,mp4a.40,mp4a.40.?" }
Ryan Sleevi 2014/04/28 22:02:55 Why ? instead of *?
amogh.bihani 2014/04/29 03:56:09 Because '*' wildcard can take any number of charac
467 };
468
447 MimeUtil::MimeUtil() { 469 MimeUtil::MimeUtil() {
448 InitializeMimeTypeMaps(); 470 InitializeMimeTypeMaps();
449 } 471 }
450 472
451 // static 473 // static
452 bool MimeUtil::AreSupportedCodecs(const MimeMappings& supported_codecs, 474 bool MimeUtil::AreSupportedCodecs(const MimeMappings& supported_codecs,
453 const std::vector<std::string>& codecs) { 475 const std::vector<std::string>& codecs) {
454 if (supported_codecs.empty()) 476 if (supported_codecs.empty())
455 return codecs.empty(); 477 return codecs.empty();
456 478
457 for (size_t i = 0; i < codecs.size(); ++i) { 479 for (size_t i = 0; i < codecs.size(); ++i) {
458 if (supported_codecs.find(codecs[i]) == supported_codecs.end()) 480 if (supported_codecs.find(codecs[i]) == supported_codecs.end())
459 return false; 481 return false;
460 } 482 }
461 return !codecs.empty(); 483 return !codecs.empty();
462 } 484 }
463 485
486 bool MimeUtil::AreSupportedCodecsWithProfile(
487 const MimeExpressionMappings& supported_codecs,
488 const std::vector<std::string>& codecs) {
489 for (size_t i = 0; i < codecs.size(); ++i) {
490 bool codec_matched = false;
491 for (size_t j = 0; j < supported_codecs.size(); ++j) {
492 if (MatchPattern(static_cast <base::StringPiece> (codecs[i]),
493 static_cast <base::StringPiece> (supported_codecs[j]))) {
494 codec_matched = true;
495 std::vector<std::string> split_string;
496 base::SplitString(codecs[i], '.', &split_string);
497 if (split_string.size() > 1 &&
498 !IsHigherCaseHexNumber(split_string.back())) {
499 codec_matched = false;
500 }
501 break;
502 }
503 }
504 // Return false if any of the codecs is not matching.
505 if (!codec_matched)
506 return false;
507 }
508 return !codecs.empty();
509 }
510
464 void MimeUtil::InitializeMimeTypeMaps() { 511 void MimeUtil::InitializeMimeTypeMaps() {
465 for (size_t i = 0; i < arraysize(supported_image_types); ++i) 512 for (size_t i = 0; i < arraysize(supported_image_types); ++i)
466 image_map_.insert(supported_image_types[i]); 513 image_map_.insert(supported_image_types[i]);
467 514
468 // Initialize the supported non-image types. 515 // Initialize the supported non-image types.
469 for (size_t i = 0; i < arraysize(supported_non_image_types); ++i) 516 for (size_t i = 0; i < arraysize(supported_non_image_types); ++i)
470 non_image_map_.insert(supported_non_image_types[i]); 517 non_image_map_.insert(supported_non_image_types[i]);
471 for (size_t i = 0; i < arraysize(supported_certificate_types); ++i) 518 for (size_t i = 0; i < arraysize(supported_certificate_types); ++i)
472 non_image_map_.insert(supported_certificate_types[i].mime_type); 519 non_image_map_.insert(supported_certificate_types[i].mime_type);
473 for (size_t i = 0; i < arraysize(unsupported_text_types); ++i) 520 for (size_t i = 0; i < arraysize(unsupported_text_types); ++i)
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
514 MimeMappings codecs; 561 MimeMappings codecs;
515 for (size_t j = 0; j < mime_type_codecs.size(); ++j) { 562 for (size_t j = 0; j < mime_type_codecs.size(); ++j) {
516 #if defined(OS_ANDROID) 563 #if defined(OS_ANDROID)
517 if (!IsCodecSupportedOnAndroid(mime_type_codecs[j])) 564 if (!IsCodecSupportedOnAndroid(mime_type_codecs[j]))
518 continue; 565 continue;
519 #endif 566 #endif
520 codecs.insert(mime_type_codecs[j]); 567 codecs.insert(mime_type_codecs[j]);
521 } 568 }
522 strict_format_map_[format_codec_mappings[i].mime_type] = codecs; 569 strict_format_map_[format_codec_mappings[i].mime_type] = codecs;
523 } 570 }
571 for (size_t i = 0; i < arraysize(format_mp4_codec_mappings); ++i) {
572 std::vector<std::string> mime_type_codecs;
573 ParseCodecString(format_mp4_codec_mappings[i].codecs_list,
574 &mime_type_codecs,
575 false);
576
577 MimeExpressionMappings codecs;
578 for (size_t j = 0; j < mime_type_codecs.size(); ++j) {
579 codecs.push_back(mime_type_codecs[j]);
580 }
581 strict_mp4_format_map_[format_mp4_codec_mappings[i].mime_type] = codecs;
582 }
524 } 583 }
525 584
526 bool MimeUtil::IsSupportedImageMimeType(const std::string& mime_type) const { 585 bool MimeUtil::IsSupportedImageMimeType(const std::string& mime_type) const {
527 return image_map_.find(mime_type) != image_map_.end(); 586 return image_map_.find(mime_type) != image_map_.end();
528 } 587 }
529 588
530 bool MimeUtil::IsSupportedMediaMimeType(const std::string& mime_type) const { 589 bool MimeUtil::IsSupportedMediaMimeType(const std::string& mime_type) const {
531 return media_map_.find(mime_type) != media_map_.end(); 590 return media_map_.find(mime_type) != media_map_.end();
532 } 591 }
533 592
(...skipping 162 matching lines...) Expand 10 before | Expand all | Expand 10 after
696 for (std::vector<std::string>::iterator it = codecs_out->begin(); 755 for (std::vector<std::string>::iterator it = codecs_out->begin();
697 it != codecs_out->end(); 756 it != codecs_out->end();
698 ++it) { 757 ++it) {
699 size_t found = it->find_first_of('.'); 758 size_t found = it->find_first_of('.');
700 if (found != std::string::npos) 759 if (found != std::string::npos)
701 it->resize(found); 760 it->resize(found);
702 } 761 }
703 } 762 }
704 763
705 bool MimeUtil::IsStrictMediaMimeType(const std::string& mime_type) const { 764 bool MimeUtil::IsStrictMediaMimeType(const std::string& mime_type) const {
706 if (strict_format_map_.find(mime_type) == strict_format_map_.end()) 765 if (strict_format_map_.find(mime_type) == strict_format_map_.end() &&
766 strict_mp4_format_map_.find(mime_type) == strict_mp4_format_map_.end())
707 return false; 767 return false;
708 return true; 768 return true;
709 } 769 }
710 770
711 bool MimeUtil::IsSupportedStrictMediaMimeType( 771 bool MimeUtil::IsSupportedStrictMediaMimeType(
712 const std::string& mime_type, 772 const std::string& mime_type,
713 const std::vector<std::string>& codecs) const { 773 const std::vector<std::string>& codecs) const {
714 StrictMappings::const_iterator it = strict_format_map_.find(mime_type); 774 StrictMappings::const_iterator it = strict_format_map_.find(mime_type);
715 return (it != strict_format_map_.end()) && 775 return (it != strict_format_map_.end()) &&
716 AreSupportedCodecs(it->second, codecs); 776 AreSupportedCodecs(it->second, codecs);
717 } 777 }
718 778
779 bool MimeUtil::IsSupportedStrictMP4MediaMimeType(
780 const std::string& mime_type,
781 const std::vector<std::string>& codecs) const {
782 StrictExpressionMappings::const_iterator it =
783 strict_mp4_format_map_.find(mime_type);
784 return (it != strict_mp4_format_map_.end() &&
785 AreSupportedCodecsWithProfile(it->second, codecs));
786 }
787
719 void MimeUtil::RemoveProprietaryMediaTypesAndCodecsForTests() { 788 void MimeUtil::RemoveProprietaryMediaTypesAndCodecsForTests() {
720 for (size_t i = 0; i < arraysize(proprietary_media_types); ++i) { 789 for (size_t i = 0; i < arraysize(proprietary_media_types); ++i) {
721 non_image_map_.erase(proprietary_media_types[i]); 790 non_image_map_.erase(proprietary_media_types[i]);
722 media_map_.erase(proprietary_media_types[i]); 791 media_map_.erase(proprietary_media_types[i]);
723 } 792 }
724 for (size_t i = 0; i < arraysize(proprietary_media_codecs); ++i) 793 for (size_t i = 0; i < arraysize(proprietary_media_codecs); ++i)
725 codecs_map_.erase(proprietary_media_codecs[i]); 794 codecs_map_.erase(proprietary_media_codecs[i]);
726 } 795 }
727 796
728 //---------------------------------------------------------------------------- 797 //----------------------------------------------------------------------------
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after
789 858
790 bool IsStrictMediaMimeType(const std::string& mime_type) { 859 bool IsStrictMediaMimeType(const std::string& mime_type) {
791 return g_mime_util.Get().IsStrictMediaMimeType(mime_type); 860 return g_mime_util.Get().IsStrictMediaMimeType(mime_type);
792 } 861 }
793 862
794 bool IsSupportedStrictMediaMimeType(const std::string& mime_type, 863 bool IsSupportedStrictMediaMimeType(const std::string& mime_type,
795 const std::vector<std::string>& codecs) { 864 const std::vector<std::string>& codecs) {
796 return g_mime_util.Get().IsSupportedStrictMediaMimeType(mime_type, codecs); 865 return g_mime_util.Get().IsSupportedStrictMediaMimeType(mime_type, codecs);
797 } 866 }
798 867
868 bool IsSupportedStrictMP4MediaMimeType(const std::string& mime_type,
869 const std::vector<std::string>& codecs) {
870 return g_mime_util.Get().IsSupportedStrictMP4MediaMimeType(mime_type, codecs);
871 }
872
799 void ParseCodecString(const std::string& codecs, 873 void ParseCodecString(const std::string& codecs,
800 std::vector<std::string>* codecs_out, 874 std::vector<std::string>* codecs_out,
801 const bool strip) { 875 const bool strip) {
802 g_mime_util.Get().ParseCodecString(codecs, codecs_out, strip); 876 g_mime_util.Get().ParseCodecString(codecs, codecs_out, strip);
803 } 877 }
804 878
805 namespace { 879 namespace {
806 880
807 // From http://www.w3schools.com/media/media_mimeref.asp and 881 // From http://www.w3schools.com/media/media_mimeref.asp and
808 // http://plugindoc.mozdev.org/winmime.php 882 // http://plugindoc.mozdev.org/winmime.php
(...skipping 225 matching lines...) Expand 10 before | Expand all | Expand 10 after
1034 post_data->append("\r\n" + value + "\r\n"); 1108 post_data->append("\r\n" + value + "\r\n");
1035 } 1109 }
1036 1110
1037 void AddMultipartFinalDelimiterForUpload(const std::string& mime_boundary, 1111 void AddMultipartFinalDelimiterForUpload(const std::string& mime_boundary,
1038 std::string* post_data) { 1112 std::string* post_data) {
1039 DCHECK(post_data); 1113 DCHECK(post_data);
1040 post_data->append("--" + mime_boundary + "--\r\n"); 1114 post_data->append("--" + mime_boundary + "--\r\n");
1041 } 1115 }
1042 1116
1043 } // namespace net 1117 } // 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