1 // Copyright 2014 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.
5 #include "media/mojo/services/mojo_cdm.h"
8 #include "base/bind_helpers.h"
9 #include "media/base/cdm_context.h"
10 #include "media/base/cdm_key_information.h"
11 #include "media/base/cdm_promise.h"
12 #include "media/mojo/services/media_type_converters.h"
13 #include "mojo/application/public/cpp/connect.h"
14 #include "mojo/application/public/interfaces/service_provider.mojom.h"
19 template <typename PromiseType
>
20 static void RejectPromise(scoped_ptr
<PromiseType
> promise
,
21 interfaces::CdmPromiseResultPtr result
) {
22 promise
->reject(static_cast<MediaKeys::Exception
>(result
->exception
),
23 result
->system_code
, result
->error_message
);
26 int MojoCdm::next_cdm_id_
= CdmContext::kInvalidCdmId
+ 1;
30 const std::string
& key_system
,
31 const GURL
& security_origin
,
32 const media::CdmConfig
& cdm_config
,
33 interfaces::ContentDecryptionModulePtr remote_cdm
,
34 const media::SessionMessageCB
& session_message_cb
,
35 const media::SessionClosedCB
& session_closed_cb
,
36 const media::LegacySessionErrorCB
& legacy_session_error_cb
,
37 const media::SessionKeysChangeCB
& session_keys_change_cb
,
38 const media::SessionExpirationUpdateCB
& session_expiration_update_cb
,
39 const media::CdmCreatedCB
& cdm_created_cb
) {
40 scoped_ptr
<MojoCdm
> mojo_cdm(
41 new MojoCdm(remote_cdm
.Pass(), session_message_cb
, session_closed_cb
,
42 legacy_session_error_cb
, session_keys_change_cb
,
43 session_expiration_update_cb
));
45 // |mojo_cdm|'s ownership will be passed to the promise. Get a raw pointer
46 // here in order to call Initialize().
47 MojoCdm
* mojo_cdm_ptr
= mojo_cdm
.get();
48 scoped_ptr
<CdmInitializedPromise
> promise(
49 new CdmInitializedPromise(cdm_created_cb
, mojo_cdm
.Pass()));
50 mojo_cdm_ptr
->InitializeCdm(key_system
, security_origin
, cdm_config
,
54 MojoCdm::MojoCdm(interfaces::ContentDecryptionModulePtr remote_cdm
,
55 const SessionMessageCB
& session_message_cb
,
56 const SessionClosedCB
& session_closed_cb
,
57 const LegacySessionErrorCB
& legacy_session_error_cb
,
58 const SessionKeysChangeCB
& session_keys_change_cb
,
59 const SessionExpirationUpdateCB
& session_expiration_update_cb
)
60 : remote_cdm_(remote_cdm
.Pass()),
62 // Safe because MojoCdm is always created on one thread.
63 cdm_id_(next_cdm_id_
++),
64 session_message_cb_(session_message_cb
),
65 session_closed_cb_(session_closed_cb
),
66 legacy_session_error_cb_(legacy_session_error_cb
),
67 session_keys_change_cb_(session_keys_change_cb
),
68 session_expiration_update_cb_(session_expiration_update_cb
),
70 DVLOG(1) << __FUNCTION__
;
71 DCHECK_NE(CdmContext::kInvalidCdmId
, cdm_id_
);
72 DCHECK(!session_message_cb_
.is_null());
73 DCHECK(!session_closed_cb_
.is_null());
74 DCHECK(!legacy_session_error_cb_
.is_null());
75 DCHECK(!session_keys_change_cb_
.is_null());
76 DCHECK(!session_expiration_update_cb_
.is_null());
78 interfaces::ContentDecryptionModuleClientPtr client_ptr
;
79 binding_
.Bind(GetProxy(&client_ptr
));
80 remote_cdm_
->SetClient(client_ptr
.Pass());
84 DVLOG(1) << __FUNCTION__
;
87 void MojoCdm::InitializeCdm(const std::string
& key_system
,
88 const GURL
& security_origin
,
89 const media::CdmConfig
& cdm_config
,
90 scoped_ptr
<CdmInitializedPromise
> promise
) {
91 DVLOG(1) << __FUNCTION__
<< ": " << key_system
;
92 remote_cdm_
->Initialize(
93 key_system
, security_origin
.spec(),
94 interfaces::CdmConfig::From(cdm_config
), cdm_id_
,
95 base::Bind(&MojoCdm::OnPromiseResult
<>, weak_factory_
.GetWeakPtr(),
96 base::Passed(&promise
)));
99 void MojoCdm::SetServerCertificate(const std::vector
<uint8_t>& certificate
,
100 scoped_ptr
<SimpleCdmPromise
> promise
) {
101 DVLOG(2) << __FUNCTION__
;
102 remote_cdm_
->SetServerCertificate(
103 mojo::Array
<uint8_t>::From(certificate
),
104 base::Bind(&MojoCdm::OnPromiseResult
<>, weak_factory_
.GetWeakPtr(),
105 base::Passed(&promise
)));
108 void MojoCdm::CreateSessionAndGenerateRequest(
109 SessionType session_type
,
110 EmeInitDataType init_data_type
,
111 const std::vector
<uint8_t>& init_data
,
112 scoped_ptr
<NewSessionCdmPromise
> promise
) {
113 DVLOG(2) << __FUNCTION__
;
114 remote_cdm_
->CreateSessionAndGenerateRequest(
115 static_cast<interfaces::ContentDecryptionModule::SessionType
>(
117 static_cast<interfaces::ContentDecryptionModule::InitDataType
>(
119 mojo::Array
<uint8_t>::From(init_data
),
120 base::Bind(&MojoCdm::OnPromiseResult
<std::string
>,
121 weak_factory_
.GetWeakPtr(), base::Passed(&promise
)));
124 void MojoCdm::LoadSession(SessionType session_type
,
125 const std::string
& session_id
,
126 scoped_ptr
<NewSessionCdmPromise
> promise
) {
127 DVLOG(2) << __FUNCTION__
;
128 remote_cdm_
->LoadSession(
129 static_cast<interfaces::ContentDecryptionModule::SessionType
>(
132 base::Bind(&MojoCdm::OnPromiseResult
<std::string
>,
133 weak_factory_
.GetWeakPtr(), base::Passed(&promise
)));
136 void MojoCdm::UpdateSession(const std::string
& session_id
,
137 const std::vector
<uint8_t>& response
,
138 scoped_ptr
<SimpleCdmPromise
> promise
) {
139 DVLOG(2) << __FUNCTION__
;
140 remote_cdm_
->UpdateSession(
141 session_id
, mojo::Array
<uint8_t>::From(response
),
142 base::Bind(&MojoCdm::OnPromiseResult
<>, weak_factory_
.GetWeakPtr(),
143 base::Passed(&promise
)));
146 void MojoCdm::CloseSession(const std::string
& session_id
,
147 scoped_ptr
<SimpleCdmPromise
> promise
) {
148 DVLOG(2) << __FUNCTION__
;
149 remote_cdm_
->CloseSession(session_id
, base::Bind(&MojoCdm::OnPromiseResult
<>,
150 weak_factory_
.GetWeakPtr(),
151 base::Passed(&promise
)));
154 void MojoCdm::RemoveSession(const std::string
& session_id
,
155 scoped_ptr
<SimpleCdmPromise
> promise
) {
156 DVLOG(2) << __FUNCTION__
;
157 remote_cdm_
->RemoveSession(session_id
, base::Bind(&MojoCdm::OnPromiseResult
<>,
158 weak_factory_
.GetWeakPtr(),
159 base::Passed(&promise
)));
162 CdmContext
* MojoCdm::GetCdmContext() {
163 DVLOG(2) << __FUNCTION__
;
167 media::Decryptor
* MojoCdm::GetDecryptor() {
172 int MojoCdm::GetCdmId() const {
173 DCHECK_NE(CdmContext::kInvalidCdmId
, cdm_id_
);
177 void MojoCdm::OnSessionMessage(const mojo::String
& session_id
,
178 interfaces::CdmMessageType message_type
,
179 mojo::Array
<uint8_t> message
,
180 const mojo::String
& legacy_destination_url
) {
181 DVLOG(2) << __FUNCTION__
;
182 GURL verified_gurl
= GURL(legacy_destination_url
);
183 if (!verified_gurl
.is_valid() && !verified_gurl
.is_empty()) {
184 DLOG(WARNING
) << "SessionMessage destination_url is invalid : "
185 << verified_gurl
.possibly_invalid_spec();
186 verified_gurl
= GURL::EmptyGURL(); // Replace invalid destination_url.
189 session_message_cb_
.Run(session_id
,
190 static_cast<MediaKeys::MessageType
>(message_type
),
191 message
.storage(), verified_gurl
);
194 void MojoCdm::OnSessionClosed(const mojo::String
& session_id
) {
195 DVLOG(2) << __FUNCTION__
;
196 session_closed_cb_
.Run(session_id
);
199 void MojoCdm::OnLegacySessionError(const mojo::String
& session_id
,
200 interfaces::CdmException exception
,
201 uint32_t system_code
,
202 const mojo::String
& error_message
) {
203 DVLOG(2) << __FUNCTION__
;
204 legacy_session_error_cb_
.Run(session_id
,
205 static_cast<MediaKeys::Exception
>(exception
),
206 system_code
, error_message
);
209 void MojoCdm::OnSessionKeysChange(
210 const mojo::String
& session_id
,
211 bool has_additional_usable_key
,
212 mojo::Array
<interfaces::CdmKeyInformationPtr
> keys_info
) {
213 DVLOG(2) << __FUNCTION__
;
214 media::CdmKeysInfo key_data
;
215 key_data
.reserve(keys_info
.size());
216 for (size_t i
= 0; i
< keys_info
.size(); ++i
) {
218 keys_info
[i
].To
<scoped_ptr
<media::CdmKeyInformation
>>().release());
220 session_keys_change_cb_
.Run(session_id
, has_additional_usable_key
,
224 void MojoCdm::OnSessionExpirationUpdate(const mojo::String
& session_id
,
225 double new_expiry_time_sec
) {
226 DVLOG(2) << __FUNCTION__
;
227 session_expiration_update_cb_
.Run(
228 session_id
, base::Time::FromDoubleT(new_expiry_time_sec
));