media_foundation_cdm_session.h 3.8 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_SESSION_H_
  5. #define MEDIA_CDM_WIN_MEDIA_FOUNDATION_CDM_SESSION_H_
  6. #include <mfcontentdecryptionmodule.h>
  7. #include <wrl.h>
  8. #include <string>
  9. #include <vector>
  10. #include "base/callback.h"
  11. #include "base/memory/weak_ptr.h"
  12. #include "base/time/time.h"
  13. #include "media/base/content_decryption_module.h"
  14. #include "media/base/media_export.h"
  15. namespace media {
  16. // A class wrapping IMFContentDecryptionModuleSession.
  17. class MEDIA_EXPORT MediaFoundationCdmSession {
  18. public:
  19. MediaFoundationCdmSession(
  20. const std::string& uma_prefix,
  21. const SessionMessageCB& session_message_cb,
  22. const SessionKeysChangeCB& session_keys_change_cb,
  23. const SessionExpirationUpdateCB& session_expiration_update_cb);
  24. MediaFoundationCdmSession(const MediaFoundationCdmSession&) = delete;
  25. MediaFoundationCdmSession& operator=(const MediaFoundationCdmSession&) =
  26. delete;
  27. ~MediaFoundationCdmSession();
  28. // Initializes the session. All other methods should only be called after
  29. // Initialize() returns S_OK.
  30. HRESULT Initialize(IMFContentDecryptionModule* mf_cdm,
  31. CdmSessionType session_type);
  32. // EME MediaKeySession methods. Returns S_OK on success, otherwise forwards
  33. // the HRESULT from IMFContentDecryptionModuleSession.
  34. // Callback to pass the session ID to the caller. The return value indicates
  35. // whether the session ID is accepted by the caller. If returns false, the
  36. // session ID is rejected by the caller (e.g. empty of duplicate session IDs),
  37. // and |this| could be destructed immediately by the caller.
  38. using SessionIdCB = base::OnceCallback<bool(const std::string&)>;
  39. // Creates session ID and generates requests. Returns an error HRESULT on
  40. // immediate failure, in which case no callbacks will be run. Otherwise,
  41. // returns S_OK, with the following two cases:
  42. // - If |session_id_| is successfully set, |session_id_cb| will be run with
  43. // |session_id_| followed by the session message via |session_message_cb_|.
  44. // - Otherwise, |session_id_cb| will be run with an empty session ID to
  45. // indicate error. No session message in this case.
  46. HRESULT GenerateRequest(EmeInitDataType init_data_type,
  47. const std::vector<uint8_t>& init_data,
  48. SessionIdCB session_id_cb);
  49. HRESULT Load(const std::string& session_id);
  50. HRESULT Update(const std::vector<uint8_t>& response);
  51. HRESULT Close();
  52. HRESULT Remove();
  53. private:
  54. // A wrapper function to report UMA for the HRESULT `hr` of the `api` call.
  55. // Returns the `hr` as is for chained calls.
  56. HRESULT WithUmaReported(HRESULT hr, const std::string& api);
  57. // Callbacks for forwarding session events.
  58. void OnSessionMessage(CdmMessageType message_type,
  59. const std::vector<uint8_t>& message);
  60. void OnSessionKeysChange();
  61. // Sets |session_id_| and returns whether the operation succeeded.
  62. // Note: |this| could already been destructed if false is returned.
  63. bool SetSessionId();
  64. HRESULT UpdateExpirationIfNeeded();
  65. const std::string uma_prefix_;
  66. // Callbacks for firing session events.
  67. SessionMessageCB session_message_cb_;
  68. SessionKeysChangeCB session_keys_change_cb_;
  69. SessionExpirationUpdateCB session_expiration_update_cb_;
  70. Microsoft::WRL::ComPtr<IMFContentDecryptionModuleSession> mf_cdm_session_;
  71. // Callback passed in GenerateRequest() to return the session ID.
  72. SessionIdCB session_id_cb_;
  73. std::string session_id_;
  74. base::Time expiration_;
  75. // NOTE: Weak pointers must be invalidated before all other member variables.
  76. base::WeakPtrFactory<MediaFoundationCdmSession> weak_factory_{this};
  77. };
  78. } // namespace media
  79. #endif // MEDIA_CDM_WIN_MEDIA_FOUNDATION_CDM_SESSION_H_