mime_util_internal.h 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. // Copyright 2012 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #ifndef MEDIA_BASE_MIME_UTIL_INTERNAL_H_
  5. #define MEDIA_BASE_MIME_UTIL_INTERNAL_H_
  6. #include <string>
  7. #include <vector>
  8. #include "base/containers/flat_map.h"
  9. #include "base/containers/flat_set.h"
  10. #include "build/build_config.h"
  11. #include "media/base/media_export.h"
  12. #include "media/base/mime_util.h"
  13. #include "media/base/video_codecs.h"
  14. #include "media/base/video_color_space.h"
  15. namespace media {
  16. namespace internal {
  17. // Internal utility class for handling mime types. Should only be invoked by
  18. // tests and the functions within mime_util.cc -- NOT for direct use by others.
  19. class MEDIA_EXPORT MimeUtil {
  20. public:
  21. MimeUtil();
  22. MimeUtil(const MimeUtil&) = delete;
  23. MimeUtil& operator=(const MimeUtil&) = delete;
  24. ~MimeUtil();
  25. enum Codec {
  26. INVALID_CODEC,
  27. PCM,
  28. MP3,
  29. AC3,
  30. EAC3,
  31. MPEG2_AAC,
  32. MPEG4_AAC,
  33. MPEG4_XHE_AAC,
  34. VORBIS,
  35. OPUS,
  36. FLAC,
  37. H264,
  38. HEVC,
  39. VP8,
  40. VP9,
  41. THEORA,
  42. DOLBY_VISION,
  43. AV1,
  44. MPEG_H_AUDIO,
  45. DTS,
  46. DTSXP2,
  47. LAST_CODEC = DTSXP2
  48. };
  49. // Platform configuration structure. Controls which codecs are supported at
  50. // runtime. Also used by tests to simulate platform differences.
  51. struct PlatformInfo {
  52. #if BUILDFLAG(ENABLE_PLATFORM_DOLBY_VISION)
  53. bool has_platform_dv_decoder = false;
  54. #endif
  55. bool has_platform_vp8_decoder = false;
  56. bool has_platform_vp9_decoder = false;
  57. #if BUILDFLAG(ENABLE_PLATFORM_HEVC)
  58. bool has_platform_hevc_decoder = false;
  59. #endif
  60. bool has_platform_opus_decoder = false;
  61. };
  62. struct ParsedCodecResult {
  63. Codec codec;
  64. bool is_ambiguous;
  65. VideoCodecProfile video_profile;
  66. uint8_t video_level;
  67. VideoColorSpace video_color_space;
  68. };
  69. // See mime_util.h for more information on these methods.
  70. bool IsSupportedMediaMimeType(const std::string& mime_type) const;
  71. void SplitCodecs(const std::string& codecs,
  72. std::vector<std::string>* codecs_out) const;
  73. void StripCodecs(std::vector<std::string>* codecs) const;
  74. bool ParseVideoCodecString(
  75. const std::string& mime_type, // fixme, make optional
  76. const std::string& codec_id,
  77. bool* out_is_ambiguous,
  78. VideoCodec* out_codec,
  79. VideoCodecProfile* out_profile,
  80. uint8_t* out_level,
  81. VideoColorSpace* out_color_space) const;
  82. bool ParseAudioCodecString(const std::string& mime_type,
  83. const std::string& codec_id,
  84. bool* out_is_ambiguous,
  85. AudioCodec* out_codec) const;
  86. SupportsType IsSupportedMediaFormat(const std::string& mime_type,
  87. const std::vector<std::string>& codecs,
  88. bool is_encrypted) const;
  89. // Checks android platform specific codec restrictions. Returns true if
  90. // |codec| is supported when contained in |mime_type_lower_case|.
  91. // |is_encrypted| means the codec will be used with encrypted blocks.
  92. // |platform_info| describes the availability of various platform features;
  93. // see PlatformInfo for more details.
  94. static bool IsCodecSupportedOnAndroid(Codec codec,
  95. const std::string& mime_type_lower_case,
  96. bool is_encrypted,
  97. VideoCodecProfile video_profile,
  98. const PlatformInfo& platform_info);
  99. private:
  100. typedef base::flat_set<int> CodecSet;
  101. typedef base::flat_map<std::string, CodecSet> MediaFormatMappings;
  102. // Initializes the supported media types into hash sets for faster lookup.
  103. void InitializeMimeTypeMaps();
  104. // Initializes the supported media formats (|media_format_map_|).
  105. void AddSupportedMediaFormats();
  106. // Adds |mime_type| with the specified codecs to |media_format_map_|.
  107. void AddContainerWithCodecs(const std::string& mime_type,
  108. const CodecSet& codecs_list);
  109. // Returns SupportsType::kSupported if all codec IDs in |codecs| are
  110. // unambiguous and are supported in |mime_type_lower_case|. kMaybeSupported is
  111. // returned if at least one codec ID in |codecs| is ambiguous but all the
  112. // codecs are supported. kNotSupported is returned if |mime_type_lower_case|
  113. // is not supported or at least one is not supported in
  114. // |mime_type_lower_case|. |is_encrypted| means the codec will be used with
  115. // encrypted blocks.
  116. SupportsType AreSupportedCodecs(
  117. const std::vector<ParsedCodecResult>& parsed_codecs,
  118. const std::string& mime_type_lower_case,
  119. bool is_encrypted) const;
  120. // Parse the combination of |mime_type_lower_case| and |codecs|. Returns true
  121. // when parsing succeeds and output is written to |out_results|. Returns false
  122. // when parsing fails. Failure may be caused by
  123. // - invalid/unrecognized codec strings and mime_types
  124. // - invalid combinations of codec strings and mime_types (e.g. H264 in WebM)
  125. // See comment for ParseCodecHelper().
  126. bool ParseCodecStrings(const std::string& mime_type_lower_case,
  127. const std::vector<std::string>& codecs,
  128. std::vector<ParsedCodecResult>* out_results) const;
  129. // Helper to ParseCodecStrings(). Parses a single |codec_id| with
  130. // |mime_type_lower_case| to populate the fields of |out_result|. This helper
  131. // method does not validate the combination of |mime_type_lower_case| and
  132. // |codec_id|, nor does it handle empty/unprovided codecs; See caller
  133. // ParseCodecStrings().
  134. //
  135. // |out_result| is only valid when this method returns true (parsing success).
  136. // |out_result->is_ambiguous| will be set to true when the codec string
  137. // matches one of a fixed number of *non-RFC compliant* strings (e.g. "avc").
  138. // Ambiguous video codec strings may fail to provide video profile and/or
  139. // level info. In these cases, we use the following values to indicate
  140. // "unspecified":
  141. // - out_result->video_profile = VIDEO_CODEC_PROFILE_UNKNOWN
  142. // - out_result->video_level = 0
  143. //
  144. // For unambiguous video codecs, |video_profile| and |video_level| will be
  145. // set in |out_result|.
  146. //
  147. // |out_result|'s |video_color_space| will report the codec strings color
  148. // space when provided. Most codec strings do not yet describe color, so this
  149. // will often be set to the default of REC709.
  150. bool ParseCodecHelper(const std::string& mime_type_lower_case,
  151. const std::string& codec_id,
  152. ParsedCodecResult* out_result) const;
  153. // Returns kSupported if |codec| when platform supports codec contained in
  154. // |mime_type_lower_case|. Returns kMaybeSupported when platform support is
  155. // unclear. Otherwise returns NotSupported. Note: This method will always
  156. // return NotSupported for proprietary codecs if |allow_proprietary_codecs_|
  157. // is set to false. |is_encrypted| means the codec will be used with encrypted
  158. // blocks.
  159. // TODO(chcunningham): Make this method return a bool. Platform support should
  160. // always be knowable for a fully specified codec.
  161. SupportsType IsCodecSupported(const std::string& mime_type_lower_case,
  162. Codec codec,
  163. VideoCodecProfile video_profile,
  164. uint8_t video_level,
  165. const VideoColorSpace& eotf,
  166. bool is_encrypted) const;
  167. // Returns true if |codec| refers to a proprietary codec.
  168. bool IsCodecProprietary(Codec codec) const;
  169. // Returns true and sets |*default_codec| if |mime_type_lower_case| has a
  170. // default codec associated with it. Returns false otherwise and the value of
  171. // |*default_codec| is undefined.
  172. bool GetDefaultCodec(const std::string& mime_type_lower_case,
  173. Codec* default_codec) const;
  174. #if BUILDFLAG(IS_ANDROID)
  175. // Indicates the support of various codecs within the platform.
  176. PlatformInfo platform_info_;
  177. #endif
  178. // A map of mime_types and hash map of the supported codecs for the mime_type.
  179. MediaFormatMappings media_format_map_;
  180. };
  181. } // namespace internal
  182. } // namespace media
  183. #endif // MEDIA_BASE_MIME_UTIL_INTERNAL_H_