• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_INNER_ERR_UNKNOWN;
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_INNER_ERR_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_INNER_ERR_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_INNER_ERR_OK;
91 
92     if (serviceProxy_ == nullptr) {
93         DRM_ERR_LOG("GenerateKeySystemRequest serviceProxy_ is null");
94         return DRM_INNER_ERR_SERVICE_FATAL_ERROR;
95     }
96     ret = serviceProxy_->GenerateKeySystemRequest(request, defaultUrl);
97     if (ret != DRM_INNER_ERR_OK) {
98         DRM_ERR_LOG("GenerateKeySystemRequest failed, ret: %{public}d", ret);
99         return DRM_INNER_ERR_BASE;
100     }
101     return DRM_INNER_ERR_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_INNER_ERR_OK;
110 
111     if (serviceProxy_ == nullptr) {
112         DRM_ERR_LOG("ProcessKeySystemResponse serviceProxy_ is null");
113         return DRM_INNER_ERR_SERVICE_FATAL_ERROR;
114     }
115     ret = serviceProxy_->ProcessKeySystemResponse(response);
116     if (ret != DRM_INNER_ERR_OK) {
117         DRM_ERR_LOG("ProcessKeySystemResponse failed, ret: %{public}d", ret);
118         return DRM_INNER_ERR_BASE;
119     }
120     return DRM_INNER_ERR_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_INNER_ERR_OK;
129 
130     if (serviceProxy_ == nullptr) {
131         DRM_ERR_LOG("SetConfiguration serviceProxy_ is null");
132         return DRM_INNER_ERR_SERVICE_FATAL_ERROR;
133     }
134     ret = serviceProxy_->SetConfigurationString(configName, value);
135     if (ret != DRM_INNER_ERR_OK) {
136         DRM_ERR_LOG("SetConfiguration failed, ret: %{public}d", ret);
137         return DRM_INNER_ERR_BASE;
138     }
139     return DRM_INNER_ERR_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_INNER_ERR_OK;
147 
148     if (serviceProxy_ == nullptr) {
149         DRM_ERR_LOG("GetConfiguration serviceProxy_ is null");
150         return DRM_INNER_ERR_SERVICE_FATAL_ERROR;
151     }
152     ret = serviceProxy_->GetConfigurationString(configName, value);
153     if (ret != DRM_INNER_ERR_OK) {
154         DRM_ERR_LOG("GetConfiguration failed, ret: %{public}d", ret);
155         return DRM_INNER_ERR_BASE;
156     }
157     return DRM_INNER_ERR_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_INNER_ERR_OK;
165 
166     if (serviceProxy_ == nullptr) {
167         DRM_ERR_LOG("SetConfiguration serviceProxy_ is null");
168         return DRM_INNER_ERR_SERVICE_FATAL_ERROR;
169     }
170     ret = serviceProxy_->SetConfigurationByteArray(configName, value);
171     if (ret != DRM_INNER_ERR_OK) {
172         DRM_ERR_LOG("SetConfiguration failed, ret: %{public}d", ret);
173         return DRM_INNER_ERR_BASE;
174     }
175     return DRM_INNER_ERR_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_INNER_ERR_OK;
183 
184     if (serviceProxy_ == nullptr) {
185         DRM_ERR_LOG("GetConfiguration serviceProxy_ is null");
186         return DRM_INNER_ERR_SERVICE_FATAL_ERROR;
187     }
188     ret = serviceProxy_->GetConfigurationByteArray(configName, value);
189     if (ret != DRM_INNER_ERR_OK) {
190         DRM_ERR_LOG("GetConfiguration failed, ret: %{public}d", ret);
191         return DRM_INNER_ERR_BASE;
192     }
193 
194     return DRM_INNER_ERR_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_INNER_ERR_OK;
206     if (serviceProxy_ == nullptr) {
207         DRM_ERR_LOG("serviceProxy_ == nullptr");
208         return DRM_INNER_ERR_SERVICE_FATAL_ERROR;
209     }
210 
211     ret = serviceProxy_->CreateMediaKeySession(securityLevel, keySessionProxy);
212     if (ret == DRM_INNER_ERR_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_INNER_ERR_SERVICE_FATAL_ERROR;
218             }
219         } else {
220             DRM_ERR_LOG("Service faltal error");
221             return DRM_INNER_ERR_SERVICE_FATAL_ERROR;
222         }
223     } else {
224         if (ret == DRM_INNER_ERR_MAX_SESSION_NUM_REACHED) {
225             DRM_ERR_LOG("The number of MediaKeySession is greater than 64");
226             return DRM_INNER_ERR_MAX_SESSION_NUM_REACHED;
227         }
228         DRM_ERR_LOG("Failed to get session object from mediakeysystem service!, %{public}d", ret);
229         return ret;
230     }
231     *keySessionImpl = localMediaKeySessionImpl;
232     return DRM_INNER_ERR_OK;
233 }
234 
GetStatistics(std::vector<IMediaKeySystemService::MetircKeyValue> & metrics)235 int32_t MediaKeySystemImpl::GetStatistics(std::vector<IMediaKeySystemService::MetircKeyValue> &metrics)
236 {
237     DRM_INFO_LOG("GetStatistics enter.");
238     std::lock_guard<std::recursive_mutex> lock(mutex_);
239     int32_t ret = DRM_INNER_ERR_OK;
240 
241     if (serviceProxy_ == nullptr) {
242         DRM_ERR_LOG("GetStatistics serviceProxy_ is null");
243         return DRM_INNER_ERR_SERVICE_FATAL_ERROR;
244     }
245     ret = serviceProxy_->GetStatistics(metrics);
246     if (ret != DRM_INNER_ERR_OK) {
247         DRM_ERR_LOG("GetStatistics failed, ret: %{public}d", ret);
248         return DRM_INNER_ERR_BASE;
249     }
250     return DRM_INNER_ERR_OK;
251 }
252 
GetMaxContentProtectionLevel(IMediaKeySessionService::ContentProtectionLevel * securityLevel)253 int32_t MediaKeySystemImpl::GetMaxContentProtectionLevel(IMediaKeySessionService::ContentProtectionLevel *securityLevel)
254 {
255     DRM_INFO_LOG("GetMaxContentProtectionLevel enter.");
256     std::lock_guard<std::recursive_mutex> lock(mutex_);
257     int32_t ret = DRM_INNER_ERR_OK;
258 
259     if (serviceProxy_ == nullptr) {
260         DRM_ERR_LOG("GetMaxContentProtectionLevel serviceProxy_ is null");
261         return DRM_INNER_ERR_SERVICE_FATAL_ERROR;
262     }
263     ret =
264         serviceProxy_->GetMaxContentProtectionLevel((IMediaKeySessionService::ContentProtectionLevel *)securityLevel);
265     if (ret != DRM_INNER_ERR_OK) {
266         DRM_ERR_LOG("GetMaxContentProtectionLevel failed, ret: %{public}d", ret);
267         return DRM_INNER_ERR_BASE;
268     }
269     return DRM_INNER_ERR_OK;
270 }
271 
GetOfflineMediaKeyIds(std::vector<std::vector<uint8_t>> & licenseIds)272 int32_t MediaKeySystemImpl::GetOfflineMediaKeyIds(std::vector<std::vector<uint8_t>> &licenseIds)
273 {
274     DRM_INFO_LOG("GetOfflineMediaKeyIds enter.");
275     std::lock_guard<std::recursive_mutex> lock(mutex_);
276     int32_t ret = DRM_INNER_ERR_OK;
277 
278     if (serviceProxy_ == nullptr) {
279         DRM_ERR_LOG("GetOfflineMediaKeyIds serviceProxy_ is null");
280         return DRM_INNER_ERR_SERVICE_FATAL_ERROR;
281     }
282     ret = serviceProxy_->GetOfflineMediaKeyIds(licenseIds);
283     if (ret != DRM_INNER_ERR_OK) {
284         DRM_ERR_LOG("GetOfflineMediaKeyIds failed, ret: %{public}d", ret);
285         return DRM_INNER_ERR_BASE;
286     }
287     return DRM_INNER_ERR_OK;
288 }
289 
GetOfflineMediaKeyStatus(std::vector<uint8_t> & licenseId,IMediaKeySessionService::OfflineMediaKeyStatus & status)290 int32_t MediaKeySystemImpl::GetOfflineMediaKeyStatus(std::vector<uint8_t> &licenseId,
291     IMediaKeySessionService::OfflineMediaKeyStatus &status)
292 {
293     DRM_INFO_LOG("GetOfflineMediaKeyStatus enter.");
294     std::lock_guard<std::recursive_mutex> lock(mutex_);
295     int32_t ret = DRM_INNER_ERR_OK;
296 
297     if (serviceProxy_ == nullptr) {
298         DRM_ERR_LOG("GetOfflineMediaKeyStatus serviceProxy_ is null");
299         return DRM_INNER_ERR_SERVICE_FATAL_ERROR;
300     }
301     ret = serviceProxy_->GetOfflineMediaKeyStatus(licenseId, status);
302     if (ret != DRM_INNER_ERR_OK) {
303         DRM_ERR_LOG("GetOfflineMediaKeyStatus failed, ret: %{public}d", ret);
304         return DRM_INNER_ERR_BASE;
305     }
306     return DRM_INNER_ERR_OK;
307 }
308 
ClearOfflineMediaKeys(std::vector<uint8_t> & licenseId)309 int32_t MediaKeySystemImpl::ClearOfflineMediaKeys(std::vector<uint8_t> &licenseId)
310 {
311     DRM_INFO_LOG("ClearOfflineMediaKeys enter.");
312     std::lock_guard<std::recursive_mutex> lock(mutex_);
313     int32_t ret = DRM_INNER_ERR_OK;
314     if (serviceProxy_ == nullptr) {
315         DRM_ERR_LOG("ClearOfflineMediaKeys serviceProxy_ is null");
316         return DRM_INNER_ERR_SERVICE_FATAL_ERROR;
317     }
318     ret = serviceProxy_->ClearOfflineMediaKeys(licenseId);
319     if (ret != DRM_INNER_ERR_OK) {
320         DRM_ERR_LOG("ClearOfflineMediaKeys failed, ret: %{public}d", ret);
321         return DRM_INNER_ERR_BASE;
322     }
323     return DRM_INNER_ERR_OK;
324 }
325 
GetCertificateStatus(IMediaKeySystemService::CertificateStatus * certStatus)326 int32_t MediaKeySystemImpl::GetCertificateStatus(IMediaKeySystemService::CertificateStatus *certStatus)
327 {
328     DRM_INFO_LOG("GetCertificateStatus enter.");
329     std::lock_guard<std::recursive_mutex> lock(mutex_);
330     int32_t ret = DRM_INNER_ERR_OK;
331 
332     if (serviceProxy_ == nullptr) {
333         DRM_ERR_LOG("GetCertificateStatus serviceProxy_ is null");
334         return DRM_INNER_ERR_SERVICE_FATAL_ERROR;
335     }
336     ret = serviceProxy_->GetCertificateStatus((IMediaKeySystemService::CertificateStatus *)certStatus);
337     if (ret != DRM_INNER_ERR_OK) {
338         DRM_ERR_LOG("GetCertificateStatus failed, ret: %{public}d", ret);
339         return DRM_INNER_ERR_BASE;
340     }
341     return DRM_INNER_ERR_OK;
342 }
343 
SetCallback(const sptr<MediaKeySystemImplCallback> & callback)344 int32_t MediaKeySystemImpl::SetCallback(const sptr<MediaKeySystemImplCallback> &callback)
345 {
346     DRM_DEBUG_LOG("0x%{public}06" PRIXPTR " SetCallback in", FAKE_POINTER(this));
347     DRM_CHECK_AND_RETURN_RET_LOG(callback != nullptr, DRM_INNER_ERR_INVALID_VAL, "callback is nullptr");
348     std::lock_guard<std::recursive_mutex> lock(mutex_);
349     mediaKeySystemApplicationCallback_ = callback;
350 
351     int32_t ret = DRM_INNER_ERR_BASE;
352     serviceCallback_ = new (std::nothrow) MediaKeySystemCallback(this);
353     if (serviceCallback_ == nullptr) {
354         DRM_ERR_LOG("MediaKeySystemCallback alloc failed.");
355         return ret;
356     }
357 
358     if (serviceProxy_ == nullptr) {
359         DRM_ERR_LOG("SetCallback serviceProxy_ is null");
360         return DRM_INNER_ERR_SERVICE_FATAL_ERROR;
361     }
362     ret = serviceProxy_->SetCallback(serviceCallback_);
363     if (ret != DRM_INNER_ERR_OK) {
364         DRM_ERR_LOG("SetCallback failed, ret: %{public}d", ret);
365         return DRM_INNER_ERR_BASE;
366     }
367     return ret;
368 }
369 
GetApplicationCallback()370 sptr<MediaKeySystemImplCallback> MediaKeySystemImpl::GetApplicationCallback()
371 {
372     DRM_INFO_LOG("GetApplicationCallback");
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_INNER_ERR_OK;
412         }
413     }
414     DRM_DEBUG_LOG("SendEvent failed");
415     return DRM_INNER_ERR_BASE;
416 }
417 } // namespace DrmStandard
418 } // namespace OHOS