supported_cdm_versions.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. // Copyright 2013 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_CDM_SUPPORTED_CDM_VERSIONS_H_
  5. #define MEDIA_CDM_SUPPORTED_CDM_VERSIONS_H_
  6. #include <stddef.h>
  7. #include <array>
  8. #include "media/base/media_export.h"
  9. #include "media/cdm/api/content_decryption_module.h"
  10. // A library CDM interface is "supported" if it's implemented by CdmAdapter and
  11. // CdmWrapper. Typically multiple CDM interfaces are supported:
  12. // - The latest stable CDM interface.
  13. // - Previous stable CDM interface(s), for supporting older CDMs.
  14. // - Experimental CDM interface(s), for development.
  15. //
  16. // A library CDM interface is "enabled" if it's enabled at runtime, e.g. being
  17. // able to be registered and creating CDM instances. Experimental CDM interfaces
  18. // must not be enabled by default.
  19. //
  20. // Whether a CDM interface is enabled can also be overridden by using command
  21. // line switch switches::kOverrideEnabledCdmInterfaceVersion for finer control
  22. // in a test environment or for local debugging, including enabling experimental
  23. // CDM interfaces.
  24. namespace media {
  25. struct SupportedVersion {
  26. int version;
  27. bool enabled;
  28. };
  29. constexpr std::array<SupportedVersion, 2> kSupportedCdmInterfaceVersions = {{
  30. {10, true},
  31. {11, false},
  32. }};
  33. // In most cases CdmInterface::kVersion == CdmInterface::Host::kVersion. However
  34. // this is not guaranteed. For example, a newer CDM interface may use an
  35. // existing CDM host. So we keep CDM host support separate from CDM interface
  36. // support. In CdmInterfaceTraits we also static assert that for supported CDM
  37. // interface, CdmInterface::Host::kVersion must also be supported.
  38. constexpr int kMinSupportedCdmHostVersion = 10;
  39. constexpr int kMaxSupportedCdmHostVersion = 11;
  40. constexpr bool IsSupportedCdmModuleVersion(int version) {
  41. return version == CDM_MODULE_VERSION;
  42. }
  43. // Returns whether the CDM interface of |version| is supported in the
  44. // implementation.
  45. constexpr bool IsSupportedCdmInterfaceVersion(int version) {
  46. for (size_t i = 0; i < kSupportedCdmInterfaceVersions.size(); ++i) {
  47. if (kSupportedCdmInterfaceVersions[i].version == version)
  48. return true;
  49. }
  50. return false;
  51. }
  52. // Returns whether the CDM host interface of |version| is supported in the
  53. // implementation. Currently there's no way to disable a supported CDM host
  54. // interface at run time.
  55. constexpr bool IsSupportedCdmHostVersion(int version) {
  56. return kMinSupportedCdmHostVersion <= version &&
  57. version <= kMaxSupportedCdmHostVersion;
  58. }
  59. // Returns whether the CDM interface of |version| is enabled by default.
  60. constexpr bool IsCdmInterfaceVersionEnabledByDefault(int version) {
  61. for (size_t i = 0; i < kSupportedCdmInterfaceVersions.size(); ++i) {
  62. if (kSupportedCdmInterfaceVersions[i].version == version)
  63. return kSupportedCdmInterfaceVersions[i].enabled;
  64. }
  65. return false;
  66. }
  67. // Returns whether the CDM interface of |version| is supported in the
  68. // implementation and enabled at runtime.
  69. MEDIA_EXPORT bool IsSupportedAndEnabledCdmInterfaceVersion(int version);
  70. typedef bool (*VersionCheckFunc)(int version);
  71. // Returns true if all versions in the range [min_version, max_version] and no
  72. // versions outside the range are supported, and false otherwise.
  73. constexpr bool CheckVersions(VersionCheckFunc check_func,
  74. int min_version,
  75. int max_version) {
  76. // For simplicity, only check one version out of the range boundary.
  77. if (check_func(min_version - 1) || check_func(max_version + 1))
  78. return false;
  79. for (int version = min_version; version <= max_version; ++version) {
  80. if (!check_func(version))
  81. return false;
  82. }
  83. return true;
  84. }
  85. // Ensures CDM interface versions in and only in the range [min_version,
  86. // max_version] are supported in the implementation.
  87. constexpr bool CheckSupportedCdmInterfaceVersions(int min_version,
  88. int max_version) {
  89. return CheckVersions(IsSupportedCdmInterfaceVersion, min_version,
  90. max_version);
  91. }
  92. // Ensures CDM host interface versions in and only in the range [min_version,
  93. // max_version] are supported in the implementation.
  94. constexpr bool CheckSupportedCdmHostVersions(int min_version, int max_version) {
  95. return CheckVersions(IsSupportedCdmHostVersion, min_version, max_version);
  96. }
  97. // Traits for CDM Interfaces
  98. template <int CdmInterfaceVersion>
  99. struct CdmInterfaceTraits {};
  100. // TODO(xhwang): CDM_9 support has been removed; consider to use a macro to
  101. // help define CdmInterfaceTraits specializations.
  102. template <>
  103. struct CdmInterfaceTraits<10> {
  104. using CdmInterface = cdm::ContentDecryptionModule_10;
  105. static_assert(CdmInterface::kVersion == 10, "CDM interface version mismatch");
  106. static_assert(IsSupportedCdmHostVersion(CdmInterface::Host::kVersion),
  107. "Host not supported");
  108. static_assert(
  109. CdmInterface::kIsStable ||
  110. !IsCdmInterfaceVersionEnabledByDefault(CdmInterface::kVersion),
  111. "Experimental CDM interface should not be enabled by default");
  112. };
  113. template <>
  114. struct CdmInterfaceTraits<11> {
  115. using CdmInterface = cdm::ContentDecryptionModule_11;
  116. static_assert(CdmInterface::kVersion == 11, "CDM interface version mismatch");
  117. static_assert(IsSupportedCdmHostVersion(CdmInterface::Host::kVersion),
  118. "Host not supported");
  119. static_assert(
  120. CdmInterface::kIsStable ||
  121. !IsCdmInterfaceVersionEnabledByDefault(CdmInterface::kVersion),
  122. "Experimental CDM interface should not be enabled by default");
  123. };
  124. } // namespace media
  125. #endif // MEDIA_CDM_SUPPORTED_CDM_VERSIONS_H_