media_foundation_cdm_factory.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. // Copyright 2020 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_WIN_MEDIA_FOUNDATION_CDM_FACTORY_H_
  5. #define MEDIA_CDM_WIN_MEDIA_FOUNDATION_CDM_FACTORY_H_
  6. #include <mfcontentdecryptionmodule.h>
  7. #include <wrl.h>
  8. #include <map>
  9. #include <memory>
  10. #include <string>
  11. #include "base/callback.h"
  12. #include "base/files/file_path.h"
  13. #include "base/memory/weak_ptr.h"
  14. #include "base/unguessable_token.h"
  15. #include "base/win/scoped_com_initializer.h"
  16. #include "components/crash/core/common/crash_key.h"
  17. #include "media/base/cdm_factory.h"
  18. #include "media/base/media_export.h"
  19. #include "media/cdm/cdm_auxiliary_helper.h"
  20. namespace media {
  21. class MEDIA_EXPORT MediaFoundationCdmFactory final : public CdmFactory {
  22. public:
  23. MediaFoundationCdmFactory(std::unique_ptr<CdmAuxiliaryHelper> helper);
  24. MediaFoundationCdmFactory(const MediaFoundationCdmFactory&) = delete;
  25. MediaFoundationCdmFactory& operator=(const MediaFoundationCdmFactory&) =
  26. delete;
  27. ~MediaFoundationCdmFactory() override;
  28. // Provides a way to customize IMFContentDecryptionModuleFactory creation to
  29. // support different key systems and for testing.
  30. using CreateCdmFactoryCB = base::RepeatingCallback<HRESULT(
  31. Microsoft::WRL::ComPtr<IMFContentDecryptionModuleFactory>& factory)>;
  32. void SetCreateCdmFactoryCallbackForTesting(
  33. const std::string& key_system,
  34. CreateCdmFactoryCB create_cdm_factory_cb);
  35. // CdmFactory implementation.
  36. void Create(const CdmConfig& cdm_config,
  37. const SessionMessageCB& session_message_cb,
  38. const SessionClosedCB& session_closed_cb,
  39. const SessionKeysChangeCB& session_keys_change_cb,
  40. const SessionExpirationUpdateCB& session_expiration_update_cb,
  41. CdmCreatedCB cdm_created_cb) override;
  42. private:
  43. // Callback to MediaFoundationCDM to resolve the promise.
  44. using IsTypeSupportedResultCB = base::OnceCallback<void(bool is_supported)>;
  45. void OnCdmOriginIdObtained(
  46. const CdmConfig& cdm_config,
  47. const SessionMessageCB& session_message_cb,
  48. const SessionClosedCB& session_closed_cb,
  49. const SessionKeysChangeCB& session_keys_change_cb,
  50. const SessionExpirationUpdateCB& session_expiration_update_cb,
  51. CdmCreatedCB cdm_created_cb,
  52. const std::unique_ptr<MediaFoundationCdmData> media_foundation_cdm_data);
  53. HRESULT GetCdmFactory(
  54. const std::string& key_system,
  55. Microsoft::WRL::ComPtr<IMFContentDecryptionModuleFactory>& cdm_factory);
  56. void IsTypeSupported(const std::string& key_system,
  57. const std::string& content_type,
  58. IsTypeSupportedResultCB is_type_supported_result_cb);
  59. void StoreClientToken(const std::vector<uint8_t>& client_token);
  60. void OnCdmEvent(CdmEvent event);
  61. // Creates `mf_cdm` based on the input parameters. Same as
  62. // CreateMediaFoundationCdm() but returns the HRESULT in out parameter so we
  63. // can bind it to a repeating callback using weak pointer.
  64. void CreateMfCdm(const CdmConfig& cdm_config,
  65. const base::UnguessableToken& cdm_origin_id,
  66. const absl::optional<std::vector<uint8_t>>& cdm_client_token,
  67. const base::FilePath& cdm_store_path_root,
  68. HRESULT& hresult,
  69. Microsoft::WRL::ComPtr<IMFContentDecryptionModule>& mf_cdm);
  70. std::unique_ptr<CdmAuxiliaryHelper> helper_;
  71. // CDM origin crash key used in crash reporting.
  72. crash_reporter::ScopedCrashKeyString cdm_origin_crash_key_;
  73. // IMFContentDecryptionModule implementations typically require MTA to run.
  74. base::win::ScopedCOMInitializer com_initializer_{
  75. base::win::ScopedCOMInitializer::kMTA};
  76. // Key system to CreateCdmFactoryCB mapping. This is for testing only.
  77. std::map<std::string, CreateCdmFactoryCB> create_cdm_factory_cbs_for_testing_;
  78. // NOTE: Weak pointers must be invalidated before all other member variables.
  79. base::WeakPtrFactory<MediaFoundationCdmFactory> weak_factory_{this};
  80. };
  81. } // namespace media
  82. #endif // MEDIA_CDM_WIN_MEDIA_FOUNDATION_CDM_FACTORY_H_