1 // Copyright 2014 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #ifndef MEDIA_MOJO_SERVICES_MOJO_CDM_SERVICE_H_
6 #define MEDIA_MOJO_SERVICES_MOJO_CDM_SERVICE_H_
12 #include "base/compiler_specific.h"
13 #include "base/functional/callback.h"
14 #include "base/memory/raw_ptr.h"
15 #include "base/memory/scoped_refptr.h"
16 #include "base/memory/weak_ptr.h"
17 #include "media/base/content_decryption_module.h"
18 #include "media/base/eme_constants.h"
19 #include "media/mojo/mojom/content_decryption_module.mojom.h"
20 #include "media/mojo/services/media_mojo_export.h"
21 #include "media/mojo/services/mojo_cdm_promise.h"
22 #include "media/mojo/services/mojo_cdm_service_context.h"
23 #include "media/mojo/services/mojo_decryptor_service.h"
24 #include "mojo/public/cpp/bindings/associated_remote.h"
25 #include "mojo/public/cpp/bindings/pending_associated_remote.h"
26 #include "mojo/public/cpp/bindings/pending_remote.h"
27 #include "mojo/public/cpp/bindings/receiver.h"
28 #include "third_party/abseil-cpp/absl/types/optional.h"
34 // A mojom::ContentDecryptionModule implementation backed by a
35 // media::ContentDecryptionModule.
36 class MEDIA_MOJO_EXPORT MojoCdmService final
37 : public mojom::ContentDecryptionModule {
39 // Callback for Initialize(). Non-null `cdm_context` indicates success. Null
40 // `cdm_context` indicates failure and the `error_message` provides a reason.
42 base::OnceCallback<void(mojom::CdmContextPtr cdm_context,
43 const std::string& error_message)>;
45 explicit MojoCdmService(MojoCdmServiceContext* context);
47 MojoCdmService(const MojoCdmService&) = delete;
48 MojoCdmService& operator=(const MojoCdmService&) = delete;
50 ~MojoCdmService() final;
52 // Initialize the MojoCdmService, including creating the real CDM using the
53 // `cdm_factory`, which must not be null. The MojoCdmService should NOT be
54 // used before the `init_cb` is returned.
55 void Initialize(CdmFactory* cdm_factory,
56 const CdmConfig& cdm_config,
57 InitializeCB init_cb);
59 // mojom::ContentDecryptionModule implementation.
61 mojo::PendingAssociatedRemote<mojom::ContentDecryptionModuleClient>
63 void SetServerCertificate(const std::vector<uint8_t>& certificate_data,
64 SetServerCertificateCallback callback) final;
65 void GetStatusForPolicy(HdcpVersion min_hdcp_version,
66 GetStatusForPolicyCallback callback) final;
67 void CreateSessionAndGenerateRequest(
68 CdmSessionType session_type,
69 EmeInitDataType init_data_type,
70 const std::vector<uint8_t>& init_data,
71 CreateSessionAndGenerateRequestCallback callback) final;
72 void LoadSession(CdmSessionType session_type,
73 const std::string& session_id,
74 LoadSessionCallback callback) final;
75 void UpdateSession(const std::string& session_id,
76 const std::vector<uint8_t>& response,
77 UpdateSessionCallback callback) final;
78 void CloseSession(const std::string& session_id,
79 CloseSessionCallback callback) final;
80 void RemoveSession(const std::string& session_id,
81 RemoveSessionCallback callback) final;
83 // Get CDM to be used by the media pipeline.
84 scoped_refptr<::media::ContentDecryptionModule> GetCdm();
86 // Gets the remote ID of the CDM this is holding.
87 base::UnguessableToken cdm_id() const { return cdm_id_.value(); }
90 // Callback for CdmFactory::Create().
91 void OnCdmCreated(InitializeCB callback,
92 const scoped_refptr<::media::ContentDecryptionModule>& cdm,
93 const std::string& error_message);
95 // Callbacks for firing session events.
96 void OnSessionMessage(const std::string& session_id,
97 ::media::CdmMessageType message_type,
98 const std::vector<uint8_t>& message);
99 void OnSessionKeysChange(const std::string& session_id,
100 bool has_additional_usable_key,
101 CdmKeysInfo keys_info);
102 void OnSessionExpirationUpdate(const std::string& session_id,
103 base::Time new_expiry_time);
104 void OnSessionClosed(const std::string& session_id,
105 CdmSessionClosedReason reason);
107 // Callback for when |decryptor_| loses connectivity.
108 void OnDecryptorConnectionError();
110 const raw_ptr<MojoCdmServiceContext> context_;
111 scoped_refptr<::media::ContentDecryptionModule> cdm_;
113 // MojoDecryptorService is passed the Decryptor from |cdm_|, so
114 // |decryptor_| must not outlive |cdm_|.
115 std::unique_ptr<MojoDecryptorService> decryptor_;
116 mojo::PendingRemote<mojom::Decryptor> decryptor_remote_;
117 std::unique_ptr<mojo::Receiver<mojom::Decryptor>> decryptor_receiver_;
119 // Set to a valid CDM ID if the |cdm_| is successfully created.
120 absl::optional<base::UnguessableToken> cdm_id_;
122 mojo::AssociatedRemote<mojom::ContentDecryptionModuleClient> client_;
124 base::WeakPtrFactory<MojoCdmService> weak_factory_{this};
129 #endif // MEDIA_MOJO_SERVICES_MOJO_CDM_SERVICE_H_