1 /*
2 * Copyright (c) 2023 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "media_key_system_impl.h"
17 #include "i_mediakeysystem_service.h"
18 #include "drm_error_code.h"
19 #include "drm_trace.h"
20 #include "napi_param_utils.h"
21
22 namespace OHOS {
23 namespace DrmStandard {
MediaKeySystemImpl(sptr<IMediaKeySystemService> & mediaKeysystem)24 MediaKeySystemImpl::MediaKeySystemImpl(sptr<IMediaKeySystemService> &mediaKeysystem) : serviceProxy_(mediaKeysystem)
25 {
26 DRM_DEBUG_LOG("MediaKeySystemImpl:0x %{public}06" PRIXPTR "MediaKeySystemImpl Instances create",
27 FAKE_POINTER(this));
28
29 sptr<IRemoteObject> object = serviceProxy_->AsObject();
30 pid_t pid = 0;
31 deathRecipient_ = new(std::nothrow) DrmDeathRecipient(pid);
32 DRM_CHECK_AND_RETURN_LOG(deathRecipient_ != nullptr, "failed to new DrmDeathRecipient.");
33
34 deathRecipient_->SetNotifyCb([this] (pid_t pid) {
35 this->MediaKeySystemServerDied(pid);
36 });
37 bool result = object->AddDeathRecipient(deathRecipient_);
38 if (!result) {
39 DRM_ERR_LOG("failed to add deathRecipient");
40 return;
41 }
42 }
43
~MediaKeySystemImpl()44 MediaKeySystemImpl::~MediaKeySystemImpl()
45 {
46 std::lock_guard<std::recursive_mutex> lock(mutex_);
47 serviceProxy_ = nullptr;
48 }
49
MediaKeySystemServerDied(pid_t pid)50 void MediaKeySystemImpl::MediaKeySystemServerDied(pid_t pid)
51 {
52 DRM_ERR_LOG("MediaKeySystem server has died, pid:%{public}d!", pid);
53 std::lock_guard<std::recursive_mutex> lock(mutex_);
54 if (serviceProxy_ != nullptr && serviceProxy_->AsObject() != nullptr) {
55 (void)serviceProxy_->AsObject()->RemoveDeathRecipient(deathRecipient_);
56 serviceProxy_ = nullptr;
57 deathRecipient_ = nullptr;
58 }
59 }
60
Release()61 int32_t MediaKeySystemImpl::Release()
62 {
63 DRM_INFO_LOG("Release enter.");
64 std::lock_guard<std::recursive_mutex> lock(mutex_);
65 int32_t ret = DRM_UNKNOWN_ERROR;
66 if (serviceProxy_ != nullptr) {
67 sptr<IRemoteObject> object = serviceProxy_->AsObject();
68 if (object != nullptr && deathRecipient_ != nullptr) {
69 object->RemoveDeathRecipient(deathRecipient_);
70 deathRecipient_ = nullptr;
71 }
72 ret = serviceProxy_->Release();
73 if (ret != DRM_OK) {
74 DRM_ERR_LOG("Failed to Release keySystem!, errCode:%{public}d", ret);
75 return ret;
76 }
77 serviceProxy_ = nullptr;
78 } else {
79 DRM_ERR_LOG("serviceProxy_ is nullptr");
80 return ret;
81 }
82 return DRM_OK;
83 }
84
GenerateKeySystemRequest(std::vector<uint8_t> & request,std::string & defaultUrl)85 int32_t MediaKeySystemImpl::GenerateKeySystemRequest(std::vector<uint8_t> &request, std::string &defaultUrl)
86 {
87 DrmTrace trace("MediaKeySystemImpl::GenerateKeySystemRequest");
88 DRM_INFO_LOG("GenerateKeySystemRequest enter.");
89 std::lock_guard<std::recursive_mutex> lock(mutex_);
90 int32_t ret = DRM_OK;
91
92 if (serviceProxy_ == nullptr) {
93 DRM_ERR_LOG("GenerateKeySystemRequest serviceProxy_ is null");
94 return DRM_SERVICE_ERROR;
95 }
96 ret = serviceProxy_->GenerateKeySystemRequest(request, defaultUrl);
97 if (ret != DRM_OK) {
98 DRM_ERR_LOG("GenerateKeySystemRequest failed, ret: %{public}d", ret);
99 return DRM_SERVICE_ERROR;
100 }
101 return DRM_OK;
102 }
103
ProcessKeySystemResponse(const std::vector<uint8_t> & response)104 int32_t MediaKeySystemImpl::ProcessKeySystemResponse(const std::vector<uint8_t> &response)
105 {
106 DrmTrace trace("MediaKeySystemImpl::ProcessKeySystemResponse");
107 DRM_INFO_LOG("ProcessKeySystemResponse enter.");
108 std::lock_guard<std::recursive_mutex> lock(mutex_);
109 int32_t ret = DRM_OK;
110
111 if (serviceProxy_ == nullptr) {
112 DRM_ERR_LOG("ProcessKeySystemResponse serviceProxy_ is null");
113 return DRM_SERVICE_ERROR;
114 }
115 ret = serviceProxy_->ProcessKeySystemResponse(response);
116 if (ret != DRM_OK) {
117 DRM_ERR_LOG("ProcessKeySystemResponse failed, ret: %{public}d", ret);
118 return DRM_SERVICE_ERROR;
119 }
120 return DRM_OK;
121 }
122
SetConfigurationString(std::string & configName,std::string & value)123 int32_t MediaKeySystemImpl::SetConfigurationString(std::string &configName, std::string &value)
124 {
125 DRM_INFO_LOG("SetConfiguration enter, configName:%{public}s, value:%{public}s.",
126 configName.c_str(), value.c_str());
127 std::lock_guard<std::recursive_mutex> lock(mutex_);
128 int32_t ret = DRM_OK;
129
130 if (serviceProxy_ == nullptr) {
131 DRM_ERR_LOG("SetConfiguration serviceProxy_ is null");
132 return DRM_SERVICE_ERROR;
133 }
134 ret = serviceProxy_->SetConfigurationString(configName, value);
135 if (ret != DRM_OK) {
136 DRM_ERR_LOG("SetConfiguration failed, ret: %{public}d", ret);
137 return DRM_SERVICE_ERROR;
138 }
139 return DRM_OK;
140 }
141
GetConfigurationString(std::string & configName,std::string & value)142 int32_t MediaKeySystemImpl::GetConfigurationString(std::string &configName, std::string &value)
143 {
144 DRM_INFO_LOG("GetConfiguration enter, configName:%{public}s.", configName.c_str());
145 std::lock_guard<std::recursive_mutex> lock(mutex_);
146 int32_t ret = DRM_OK;
147
148 if (serviceProxy_ == nullptr) {
149 DRM_ERR_LOG("GetConfiguration serviceProxy_ is null");
150 return DRM_SERVICE_ERROR;
151 }
152 ret = serviceProxy_->GetConfigurationString(configName, value);
153 if (ret != DRM_OK) {
154 DRM_ERR_LOG("GetConfiguration failed, ret: %{public}d", ret);
155 return DRM_SERVICE_ERROR;
156 }
157 return DRM_OK;
158 }
159
SetConfigurationByteArray(std::string & configName,std::vector<uint8_t> & value)160 int32_t MediaKeySystemImpl::SetConfigurationByteArray(std::string &configName, std::vector<uint8_t> &value)
161 {
162 DRM_INFO_LOG("SetConfiguration enter, configName:%{public}s.", configName.c_str());
163 std::lock_guard<std::recursive_mutex> lock(mutex_);
164 int32_t ret = DRM_OK;
165
166 if (serviceProxy_ == nullptr) {
167 DRM_ERR_LOG("SetConfiguration serviceProxy_ is null");
168 return DRM_SERVICE_ERROR;
169 }
170 ret = serviceProxy_->SetConfigurationByteArray(configName, value);
171 if (ret != DRM_OK) {
172 DRM_ERR_LOG("SetConfiguration failed, ret: %{public}d", ret);
173 return DRM_SERVICE_ERROR;
174 }
175 return DRM_OK;
176 }
177
GetConfigurationByteArray(std::string & configName,std::vector<uint8_t> & value)178 int32_t MediaKeySystemImpl::GetConfigurationByteArray(std::string &configName, std::vector<uint8_t> &value)
179 {
180 DRM_INFO_LOG("GetConfiguration enter, configName:%{public}s.", configName.c_str());
181 std::lock_guard<std::recursive_mutex> lock(mutex_);
182 int32_t ret = DRM_OK;
183
184 if (serviceProxy_ == nullptr) {
185 DRM_ERR_LOG("GetConfiguration serviceProxy_ is null");
186 return DRM_SERVICE_ERROR;
187 }
188 ret = serviceProxy_->GetConfigurationByteArray(configName, value);
189 if (ret != DRM_OK) {
190 DRM_ERR_LOG("GetConfiguration failed, ret: %{public}d", ret);
191 return DRM_SERVICE_ERROR;
192 }
193
194 return DRM_OK;
195 }
196
CreateMediaKeySession(IMediaKeySessionService::ContentProtectionLevel securityLevel,sptr<MediaKeySessionImpl> * keySessionImpl)197 int32_t MediaKeySystemImpl::CreateMediaKeySession(IMediaKeySessionService::ContentProtectionLevel securityLevel,
198 sptr<MediaKeySessionImpl> *keySessionImpl)
199 {
200 DrmTrace trace("MediaKeySystemImpl::CreateMediaKeySession");
201 DRM_INFO_LOG("CreateMediaKeySession enter.");
202 std::lock_guard<std::recursive_mutex> lock(mutex_);
203 sptr<IMediaKeySessionService> keySessionProxy = nullptr;
204 sptr<MediaKeySessionImpl> localMediaKeySessionImpl = nullptr;
205 int32_t ret = DRM_OK;
206 if (serviceProxy_ == nullptr) {
207 DRM_ERR_LOG("serviceProxy_ == nullptr");
208 return DRM_SERVICE_FATAL_ERROR;
209 }
210
211 ret = serviceProxy_->CreateMediaKeySession(securityLevel, keySessionProxy);
212 if (ret == DRM_OK) {
213 if (keySessionProxy != nullptr) {
214 localMediaKeySessionImpl = new (std::nothrow) MediaKeySessionImpl(keySessionProxy);
215 if (localMediaKeySessionImpl == nullptr) {
216 DRM_ERR_LOG("Failed to new MediaKeySessionImpl");
217 return DRM_SERVICE_FATAL_ERROR;
218 }
219 } else if (ret == DRM_MAX_SESSION_NUM_REACHED) {
220 DRM_ERR_LOG("The number of MediaKeySession is greater than 64");
221 return DRM_MAX_SESSION_NUM_REACHED;
222 } else {
223 DRM_ERR_LOG("Service faltal error");
224 return DRM_SERVICE_FATAL_ERROR;
225 }
226 } else {
227 DRM_ERR_LOG("Failed to get session object from mediakeysystem service!, %{public}d", ret);
228 return ret;
229 }
230 *keySessionImpl = localMediaKeySessionImpl;
231 return DRM_OK;
232 }
233
GetStatistics(std::vector<IMediaKeySystemService::MetircKeyValue> & metrics)234 int32_t MediaKeySystemImpl::GetStatistics(std::vector<IMediaKeySystemService::MetircKeyValue> &metrics)
235 {
236 DRM_INFO_LOG("GetStatistics enter.");
237 std::lock_guard<std::recursive_mutex> lock(mutex_);
238 int32_t ret = DRM_OK;
239
240 if (serviceProxy_ == nullptr) {
241 DRM_ERR_LOG("GetStatistics serviceProxy_ is null");
242 return DRM_SERVICE_ERROR;
243 }
244 ret = serviceProxy_->GetStatistics(metrics);
245 if (ret != DRM_OK) {
246 DRM_ERR_LOG("GetStatistics failed, ret: %{public}d", ret);
247 return DRM_SERVICE_ERROR;
248 }
249 return DRM_OK;
250 }
251
GetMaxContentProtectionLevel(IMediaKeySessionService::ContentProtectionLevel * securityLevel)252 int32_t MediaKeySystemImpl::GetMaxContentProtectionLevel(IMediaKeySessionService::ContentProtectionLevel *securityLevel)
253 {
254 DRM_INFO_LOG("GetMaxContentProtectionLevel enter.");
255 std::lock_guard<std::recursive_mutex> lock(mutex_);
256 int32_t ret = DRM_OK;
257
258 if (serviceProxy_ == nullptr) {
259 DRM_ERR_LOG("GetMaxContentProtectionLevel serviceProxy_ is null");
260 return DRM_SERVICE_ERROR;
261 }
262 ret =
263 serviceProxy_->GetMaxContentProtectionLevel((IMediaKeySessionService::ContentProtectionLevel *)securityLevel);
264 if (ret != DRM_OK) {
265 DRM_ERR_LOG("GetMaxContentProtectionLevel failed, ret: %{public}d", ret);
266 return DRM_SERVICE_ERROR;
267 }
268 return DRM_OK;
269 }
270
GetOfflineMediaKeyIds(std::vector<std::vector<uint8_t>> & licenseIds)271 int32_t MediaKeySystemImpl::GetOfflineMediaKeyIds(std::vector<std::vector<uint8_t>> &licenseIds)
272 {
273 DRM_INFO_LOG("GetOfflineMediaKeyIds enter.");
274 std::lock_guard<std::recursive_mutex> lock(mutex_);
275 int32_t ret = DRM_OK;
276
277 if (serviceProxy_ == nullptr) {
278 DRM_ERR_LOG("GetOfflineMediaKeyIds serviceProxy_ is null");
279 return DRM_SERVICE_ERROR;
280 }
281 ret = serviceProxy_->GetOfflineMediaKeyIds(licenseIds);
282 if (ret != DRM_OK) {
283 DRM_ERR_LOG("GetOfflineMediaKeyIds failed, ret: %{public}d", ret);
284 return DRM_SERVICE_ERROR;
285 }
286 return DRM_OK;
287 }
288
GetOfflineMediaKeyStatus(std::vector<uint8_t> & licenseId,IMediaKeySessionService::OfflineMediaKeyStatus & status)289 int32_t MediaKeySystemImpl::GetOfflineMediaKeyStatus(std::vector<uint8_t> &licenseId,
290 IMediaKeySessionService::OfflineMediaKeyStatus &status)
291 {
292 DRM_INFO_LOG("GetOfflineMediaKeyStatus enter.");
293 std::lock_guard<std::recursive_mutex> lock(mutex_);
294 int32_t ret = DRM_OK;
295
296 if (serviceProxy_ == nullptr) {
297 DRM_ERR_LOG("GetOfflineMediaKeyStatus serviceProxy_ is null");
298 return DRM_SERVICE_ERROR;
299 }
300 ret = serviceProxy_->GetOfflineMediaKeyStatus(licenseId, status);
301 if (ret != DRM_OK) {
302 DRM_ERR_LOG("GetOfflineMediaKeyStatus failed, ret: %{public}d", ret);
303 return DRM_SERVICE_ERROR;
304 }
305 return DRM_OK;
306 }
307
ClearOfflineMediaKeys(std::vector<uint8_t> & licenseId)308 int32_t MediaKeySystemImpl::ClearOfflineMediaKeys(std::vector<uint8_t> &licenseId)
309 {
310 DRM_INFO_LOG("ClearOfflineMediaKeys enter.");
311 std::lock_guard<std::recursive_mutex> lock(mutex_);
312 int32_t ret = DRM_OK;
313 if (serviceProxy_ == nullptr) {
314 DRM_ERR_LOG("ClearOfflineMediaKeys serviceProxy_ is null");
315 return DRM_SERVICE_ERROR;
316 }
317 ret = serviceProxy_->ClearOfflineMediaKeys(licenseId);
318 if (ret != DRM_OK) {
319 DRM_ERR_LOG("ClearOfflineMediaKeys failed, ret: %{public}d", ret);
320 return DRM_SERVICE_ERROR;
321 }
322 return DRM_OK;
323 }
324
GetCertificateStatus(IMediaKeySystemService::CertificateStatus * certStatus)325 int32_t MediaKeySystemImpl::GetCertificateStatus(IMediaKeySystemService::CertificateStatus *certStatus)
326 {
327 DRM_INFO_LOG("GetCertificateStatus enter.");
328 std::lock_guard<std::recursive_mutex> lock(mutex_);
329 int32_t ret = DRM_OK;
330
331 if (serviceProxy_ == nullptr) {
332 DRM_ERR_LOG("GetCertificateStatus serviceProxy_ is null");
333 return DRM_SERVICE_ERROR;
334 }
335 ret = serviceProxy_->GetCertificateStatus((IMediaKeySystemService::CertificateStatus *)certStatus);
336 if (ret != DRM_OK) {
337 DRM_ERR_LOG("GetCertificateStatus failed, ret: %{public}d", ret);
338 return DRM_SERVICE_ERROR;
339 }
340 return DRM_OK;
341 }
342
SetCallback(const sptr<MediaKeySystemImplCallback> & callback)343 int32_t MediaKeySystemImpl::SetCallback(const sptr<MediaKeySystemImplCallback> &callback)
344 {
345 DRM_DEBUG_LOG("0x%{public}06" PRIXPTR " SetCallback in", FAKE_POINTER(this));
346 DRM_CHECK_AND_RETURN_RET_LOG(callback != nullptr, DRM_INVALID_ARG, "callback is nullptr");
347 std::lock_guard<std::recursive_mutex> lock(mutex_);
348 mediaKeySystemApplicationCallback_ = callback;
349
350 int32_t ret = DRM_ERROR;
351 serviceCallback_ = new (std::nothrow) MediaKeySystemCallback(this);
352 if (serviceCallback_ == nullptr) {
353 DRM_ERR_LOG("MediaKeySystemCallback alloc failed.");
354 return ret;
355 }
356
357 if (serviceProxy_ == nullptr) {
358 DRM_ERR_LOG("SetCallback serviceProxy_ is null");
359 return DRM_SERVICE_ERROR;
360 }
361 ret = serviceProxy_->SetCallback(serviceCallback_);
362 if (ret != DRM_OK) {
363 DRM_ERR_LOG("SetCallback failed, ret: %{public}d", ret);
364 return DRM_SERVICE_ERROR;
365 }
366 return ret;
367 }
368
GetApplicationCallback()369 sptr<MediaKeySystemImplCallback> MediaKeySystemImpl::GetApplicationCallback()
370 {
371 DRM_INFO_LOG("GetApplicationCallback");
372 std::lock_guard<std::recursive_mutex> lock(mutex_);
373 return mediaKeySystemApplicationCallback_;
374 }
375
~MediaKeySystemCallback()376 MediaKeySystemCallback::~MediaKeySystemCallback()
377 {
378 DRM_INFO_LOG("~MediaKeySystemCallback");
379 std::lock_guard<std::recursive_mutex> lock(mutex_);
380 systemImpl_ = nullptr;
381 }
382
InitEventMap()383 void MediaKeySystemCallback::InitEventMap()
384 {
385 DRM_INFO_LOG("MediaKeySystemCallback InitEventMap");
386 std::lock_guard<std::recursive_mutex> lock(mutex_);
387 eventMap_[static_cast<int32_t>(DRM_EVENT_PROVISION_REQUIRED)] = MediaKeySystemEvent::EVENT_STR_PROVISION_REQUIRED;
388 }
389
GetEventName(DrmEventType event)390 std::string MediaKeySystemCallback::GetEventName(DrmEventType event)
391 {
392 DRM_INFO_LOG("MediaKeySystemCallback GetEventName");
393 std::string eventName = "";
394 std::lock_guard<std::recursive_mutex> lock(mutex_);
395 int32_t eventType = static_cast<int32_t>(event);
396 if (eventMap_.find(eventType) == eventMap_.end()) {
397 return eventName;
398 }
399 return eventMap_[eventType];
400 }
401
SendEvent(DrmEventType event,int32_t extra,const std::vector<uint8_t> & data)402 int32_t MediaKeySystemCallback::SendEvent(DrmEventType event, int32_t extra, const std::vector<uint8_t> &data)
403 {
404 DRM_INFO_LOG("SendEvent enter");
405 std::string eventName = GetEventName(event);
406 std::lock_guard<std::recursive_mutex> lock(mutex_);
407 if (systemImpl_ != nullptr && eventName.length() != 0) {
408 sptr<MediaKeySystemImplCallback> applicationCallback = systemImpl_->GetApplicationCallback();
409 if (applicationCallback != nullptr) {
410 applicationCallback->SendEvent(eventName, extra, data);
411 return DRM_OK;
412 }
413 }
414 DRM_DEBUG_LOG("SendEvent failed");
415 return DRM_ERROR;
416 }
417 } // namespace DrmStandard
418 } // namespace OHOS