1 /*
2 * Copyright (c) 2025 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 "drm_adapter_impl.h"
17
18 #include <atomic>
19 #include <clocale>
20 #include <cstddef>
21 #include <cstring.h>
22 #include <map>
23 #include <memory>
24 #include <sstream>
25 #include <typeinfo>
26 #include <unistd.h>
27
28 #include "native_mediakeysession.h"
29 #include "native_mediakeysystem.h"
30
31 namespace OHOS::NWeb {
32 std::unordered_map<MediaKeySystem*, std::shared_ptr<DrmCallbackImpl>> DrmAdapterImpl::mediaKeySystemCallbackMap_;
33 std::unordered_map<MediaKeySession*, std::shared_ptr<DrmCallbackImpl>> DrmAdapterImpl::mediaKeySessionCallbackMap_;
34 std::mutex DrmAdapterImpl::mediaKeySystemCallbackMapMutex_;
35 std::mutex DrmAdapterImpl::mediaKeySessionCallbackMutex_;
36
37 static std::unordered_map<std::string, uint32_t> KeyStatusMap {
38 { "USABLE", static_cast<uint32_t>(KeyStatus::KEY_STATUS_USABLE) },
39 { "EXPIRED", static_cast<uint32_t>(KeyStatus::KEY_STATUS_EXPIRED) },
40 { "OUTPUT_NOT_ALLOWED", static_cast<uint32_t>(KeyStatus::KEY_STATUS_OUTPUT_NOT_ALLOWED) },
41 { "PENDING", static_cast<uint32_t>(KeyStatus::KEY_STATUS_PENDING) },
42 { "INTERNAL_ERROR", static_cast<uint32_t>(KeyStatus::KEY_STATUS_INTERNAL_ERROR) },
43 { "USABLE_IN_FUTURE", static_cast<uint32_t>(KeyStatus::KEY_STATUS_USABLE_IN_FUTURE) },
44 };
45
GetContentProtectionLevelFromSecurityLevel(int32_t levelData)46 DRM_ContentProtectionLevel GetContentProtectionLevelFromSecurityLevel(int32_t levelData)
47 {
48 DRM_ContentProtectionLevel contentProtectionLevel = CONTENT_PROTECTION_LEVEL_UNKNOWN;
49 switch (levelData) {
50 case SECURITY_LEVEL_UNKNOWN:
51 contentProtectionLevel = CONTENT_PROTECTION_LEVEL_UNKNOWN;
52 break;
53 case SECURITY_LEVEL_1:
54 contentProtectionLevel = CONTENT_PROTECTION_LEVEL_HW_CRYPTO;
55 break;
56 case SECURITY_LEVEL_3:
57 contentProtectionLevel = CONTENT_PROTECTION_LEVEL_SW_CRYPTO;
58 break;
59 default:
60 break;
61 }
62 return contentProtectionLevel;
63 }
64
GetSecurityLevelFromContentProtectionLevel(int32_t levelData)65 int32_t GetSecurityLevelFromContentProtectionLevel(int32_t levelData)
66 {
67 int32_t securityLevel = SECURITY_LEVEL_3;
68 switch (levelData) {
69 case CONTENT_PROTECTION_LEVEL_UNKNOWN:
70 securityLevel = SECURITY_LEVEL_UNKNOWN;
71 break;
72 case CONTENT_PROTECTION_LEVEL_SW_CRYPTO:
73 securityLevel = SECURITY_LEVEL_3;
74 break;
75 case CONTENT_PROTECTION_LEVEL_HW_CRYPTO:
76 case CONTENT_PROTECTION_LEVEL_ENHANCED_HW_CRYPTO:
77 securityLevel = SECURITY_LEVEL_1;
78 break;
79 case CONTENT_PROTECTION_LEVEL_MAX:
80 default:
81 break;
82 }
83 return securityLevel;
84 }
85
toHexString(const uint8_t * data,size_t length)86 std::string toHexString(const uint8_t* data, size_t length)
87 {
88 static const char hexDigits[] = "0123456789ABCDEF";
89 std::string hexString;
90 hexString.reserve(length + length);
91 for (size_t i = 0; i < length; ++i) {
92 hexString.push_back(hexDigits[data[i] >> HEX_OFFSET]);
93 hexString.push_back(hexDigits[data[i] & 0x0F]);
94 }
95 return hexString;
96 }
97
EndsWithAndRemove(std::string & str,const std::string & suffix)98 bool EndsWithAndRemove(std::string& str, const std::string& suffix)
99 {
100 if (str.size() >= suffix.size() && str.compare(str.size() - suffix.size(), suffix.size(), suffix) == 0) {
101 str.erase(str.size() - suffix.size(), suffix.size());
102 return true;
103 }
104 return false;
105 }
106
IsValidNumber(const std::string & str)107 bool IsValidNumber(const std::string& str)
108 {
109 for (char c : str) {
110 if (!std::isdigit(c)) {
111 return false;
112 }
113 }
114 return true;
115 }
116
DrmCallbackImpl(std::shared_ptr<DrmCallbackAdapter> callbackAdapter)117 DrmCallbackImpl::DrmCallbackImpl(std::shared_ptr<DrmCallbackAdapter> callbackAdapter)
118 : callbackAdapter_(callbackAdapter)
119 {}
120
OnSessionMessage(const std::string & sessionId,int32_t & type,const std::vector<uint8_t> & message)121 void DrmCallbackImpl::OnSessionMessage(const std::string& sessionId, int32_t& type, const std::vector<uint8_t>& message)
122 {
123 if (callbackAdapter_) {
124 callbackAdapter_->OnSessionMessage(sessionId, type, message);
125 }
126 }
127
OnProvisionRequest(const std::string & defaultUrl,const std::string & requestData)128 void DrmCallbackImpl::OnProvisionRequest(const std::string& defaultUrl, const std::string& requestData)
129 {
130 if (callbackAdapter_) {
131 callbackAdapter_->OnProvisionRequest(defaultUrl, requestData);
132 }
133 }
134
OnProvisioningComplete(bool success)135 void DrmCallbackImpl::OnProvisioningComplete(bool success)
136 {
137 if (callbackAdapter_) {
138 callbackAdapter_->OnProvisioningComplete(success);
139 }
140 }
141
OnMediaKeySessionReady(void * session)142 void DrmCallbackImpl::OnMediaKeySessionReady(void* session)
143 {
144 if (callbackAdapter_) {
145 callbackAdapter_->OnMediaKeySessionReady(session);
146 }
147 }
148
OnPromiseRejected(uint32_t promiseId,const std::string & errorMessage)149 void DrmCallbackImpl::OnPromiseRejected(uint32_t promiseId, const std::string& errorMessage)
150 {
151 if (callbackAdapter_) {
152 callbackAdapter_->OnPromiseRejected(promiseId, errorMessage);
153 }
154 }
155
OnPromiseResolved(uint32_t promiseId)156 void DrmCallbackImpl::OnPromiseResolved(uint32_t promiseId)
157 {
158 if (callbackAdapter_) {
159 callbackAdapter_->OnPromiseResolved(promiseId);
160 }
161 }
162
OnPromiseResolvedWithSession(uint32_t promiseId,const std::string & sessionId)163 void DrmCallbackImpl::OnPromiseResolvedWithSession(uint32_t promiseId, const std::string& sessionId)
164 {
165 if (callbackAdapter_) {
166 callbackAdapter_->OnPromiseResolvedWithSession(promiseId, sessionId);
167 }
168 }
169
OnSessionClosed(const std::string & sessionId)170 void DrmCallbackImpl::OnSessionClosed(const std::string& sessionId)
171 {
172 if (callbackAdapter_) {
173 callbackAdapter_->OnSessionClosed(sessionId);
174 }
175 }
176
OnSessionKeysChange(const std::string & sessionId,const std::vector<std::string> & keyIdArray,const std::vector<uint32_t> & statusArray,bool hasAdditionalUsableKey,bool isKeyRelease)177 void DrmCallbackImpl::OnSessionKeysChange(const std::string& sessionId, const std::vector<std::string>& keyIdArray,
178 const std::vector<uint32_t>& statusArray, bool hasAdditionalUsableKey, bool isKeyRelease)
179 {
180 if (callbackAdapter_) {
181 callbackAdapter_->OnSessionKeysChange(sessionId, keyIdArray, statusArray, hasAdditionalUsableKey, isKeyRelease);
182 }
183 }
184
OnSessionExpirationUpdate(const std::string & sessionId,uint64_t expirationTime)185 void DrmCallbackImpl::OnSessionExpirationUpdate(const std::string& sessionId, uint64_t expirationTime)
186 {
187 if (callbackAdapter_) {
188 callbackAdapter_->OnSessionExpirationUpdate(sessionId, expirationTime);
189 }
190 }
191
OnStorageProvisioned()192 void DrmCallbackImpl::OnStorageProvisioned()
193 {
194 if (callbackAdapter_) {
195 callbackAdapter_->OnStorageProvisioned();
196 }
197 }
198
OnStorageSaveInfo(const std::vector<uint8_t> & ketSetId,const std::string & mimeType,const std::string & sessionId,int32_t keyType)199 void DrmCallbackImpl::OnStorageSaveInfo(
200 const std::vector<uint8_t>& ketSetId, const std::string& mimeType, const std::string& sessionId, int32_t keyType)
201 {
202 if (callbackAdapter_) {
203 callbackAdapter_->OnStorageSaveInfo(ketSetId, mimeType, sessionId, keyType);
204 }
205 }
206
OnStorageLoadInfo(const std::string & sessionId)207 void DrmCallbackImpl::OnStorageLoadInfo(const std::string& sessionId)
208 {
209 if (callbackAdapter_) {
210 callbackAdapter_->OnStorageLoadInfo(sessionId);
211 }
212 }
213
OnStorageClearInfoForKeyRelease(const std::string & sessionId)214 void DrmCallbackImpl::OnStorageClearInfoForKeyRelease(const std::string& sessionId)
215 {
216 if (callbackAdapter_) {
217 callbackAdapter_->OnStorageClearInfoForKeyRelease(sessionId);
218 }
219 }
220
OnStorageClearInfoForLoadFail(const std::string & sessionId)221 void DrmCallbackImpl::OnStorageClearInfoForLoadFail(const std::string& sessionId)
222 {
223 if (callbackAdapter_) {
224 callbackAdapter_->OnStorageClearInfoForLoadFail(sessionId);
225 }
226 }
227
OnMediaLicenseReady(bool success)228 void DrmCallbackImpl::OnMediaLicenseReady(bool success)
229 {
230 if (callbackAdapter_) {
231 callbackAdapter_->OnMediaLicenseReady(success);
232 }
233 }
234
UpdateMediaKeySessionInfoMap(MediaKeySession * keySession,std::shared_ptr<SessionInfo> sessionInfo)235 void DrmCallbackImpl::UpdateMediaKeySessionInfoMap(MediaKeySession* keySession,
236 std::shared_ptr<SessionInfo> sessionInfo)
237 {
238 WVLOG_I("[DRM]DrmCallbackImpl::UpdateMediaKeySessionInfoMap enter.");
239 std::lock_guard<std::mutex> lock(mediaKeySessionInfoMutex_);
240 mediaKeySessionInfoMap_[keySession] = sessionInfo;
241 }
242
GetMediaKeySessionInfo(MediaKeySession * keySession)243 std::shared_ptr<SessionInfo> DrmCallbackImpl::GetMediaKeySessionInfo(MediaKeySession* keySession)
244 {
245 WVLOG_I("[DRM]DrmCallbackImpl::GetMediaKeySessionInfo enter.");
246 if (keySession == nullptr) {
247 WVLOG_E("[DRM]DrmCallbackImpl::GetMediaKeySessionInfo error, keySession is nullptr.");
248 return nullptr;
249 }
250 std::lock_guard<std::mutex> lock(mediaKeySessionInfoMutex_);
251 auto iter = mediaKeySessionInfoMap_.find(keySession);
252 if (iter != mediaKeySessionInfoMap_.end()) {
253 return iter->second;
254 }
255 WVLOG_I("[DRM]DrmCallbackImpl::GetMediaKeySessionInfo, keySession not found.");
256 return nullptr;
257 }
258
RemoveMediaKeySessionInfo(MediaKeySession * keySession)259 void DrmCallbackImpl::RemoveMediaKeySessionInfo(MediaKeySession* keySession)
260 {
261 WVLOG_I("[DRM]DrmCallbackImpl::RemoveMediaKeySessionInfo enter.");
262 std::lock_guard<std::mutex> lock(mediaKeySessionInfoMutex_);
263 mediaKeySessionInfoMap_.erase(keySession);
264 }
265
ClearMediaKeySessionInfo()266 void DrmCallbackImpl::ClearMediaKeySessionInfo()
267 {
268 WVLOG_I("[DRM]DrmCallbackImpl::ClearMediaKeySessionInfo enter.");
269 std::lock_guard<std::mutex> lock(mediaKeySessionInfoMutex_);
270 mediaKeySessionInfoMap_.clear();
271 }
272
~DrmAdapterImpl()273 DrmAdapterImpl::~DrmAdapterImpl()
274 {
275 WVLOG_I("[DRM]DrmAdapterImpl::~DrmAdapterImpl enter.");
276 if (callback_) {
277 callback_->ClearMediaKeySessionInfo();
278 }
279 if (drmKeySession_ != nullptr) {
280 ReleaseMediaKeySession();
281 }
282 if (keySystemType_ == KeySystemType::WIDEVINE) {
283 std::lock_guard<std::mutex> lock(mediaKeySessionMutex_);
284 for (auto iter = emeMediaKeySessionMap_.begin(); iter != emeMediaKeySessionMap_.end();) {
285 ReleaseMediaKeySession(iter->second);
286 emeMediaKeySessionMap_.erase(iter++);
287 }
288 }
289 if (drmKeySystem_ != nullptr) {
290 ReleaseMediaKeySystem();
291 }
292 }
293
IsSupported(const std::string & name)294 bool DrmAdapterImpl::IsSupported(const std::string& name)
295 {
296 WVLOG_I("[DRM]DrmAdapterImpl::IsSupported");
297 if (name.empty()) {
298 WVLOG_E("[DRM]name is empty!");
299 return false;
300 }
301
302 bool isSupported = OH_MediaKeySystem_IsSupported(name.c_str());
303 WVLOG_I("[DRM]DrmAdapterImpl::IsSupported: %{public}d", isSupported);
304 return isSupported;
305 }
306
IsSupported2(const std::string & name,const std::string & mimeType)307 bool DrmAdapterImpl::IsSupported2(const std::string& name, const std::string& mimeType)
308 {
309 WVLOG_I("[DRM]DrmAdapterImpl::IsSupported2 enter");
310 if (name.empty()) {
311 WVLOG_E("[DRM]name is empty!");
312 return false;
313 }
314 if (mimeType.empty()) {
315 WVLOG_E("[DRM]mimeType is empty!");
316 return false;
317 }
318
319 bool isSupported = OH_MediaKeySystem_IsSupported2(name.c_str(), mimeType.c_str());
320 WVLOG_I("[DRM]DrmAdapterImpl::IsSupported2: %{public}d", isSupported);
321 return isSupported;
322 }
323
IsSupported3(const std::string & name,const std::string & mimeType,int32_t level)324 bool DrmAdapterImpl::IsSupported3(const std::string& name, const std::string& mimeType, int32_t level)
325 {
326 WVLOG_I("[DRM]DrmAdapterImpl::IsSupported3 enter");
327 if (name.empty()) {
328 WVLOG_E("[DRM]name is empty!");
329 return false;
330 }
331 if (mimeType.empty()) {
332 WVLOG_E("[DRM]mimeType is empty!");
333 return false;
334 }
335 bool isSupported =
336 OH_MediaKeySystem_IsSupported3(name.c_str(), mimeType.c_str(), static_cast<DRM_ContentProtectionLevel>(level));
337 if (isSupported != true) {
338 WVLOG_E("[DRM]The device does not support the content protection level.");
339 }
340 return isSupported;
341 }
342
GetUUID(const std::string & name)343 std::vector<uint8_t> DrmAdapterImpl::GetUUID(const std::string& name)
344 {
345 WVLOG_I("[DRM]DrmAdapterImpl::GetUUID enter, name = %{public}s:", name.c_str());
346 std::vector<uint8_t> uuid;
347 uuid.clear();
348 uint32_t count = 10;
349 DRM_MediaKeySystemDescription infos[10];
350 (void)memset_s(infos, sizeof(infos), 0, sizeof(infos));
351 Drm_ErrCode errNo = OH_MediaKeySystem_GetMediaKeySystems(infos, &count);
352 if (errNo != DRM_ERR_OK) {
353 WVLOG_E("[DRM]DRMAdapterImpl::GetMediaKeySystems failed.");
354 return uuid;
355 }
356 WVLOG_I("[DRM]DrmAdapterImpl::GetUUID, name = %{public}s, count = %{public}d", name.c_str(), count);
357 for (uint32_t i = 0; i < count; i++) {
358 if (name == infos[i].name) {
359 uuid.insert(uuid.begin(), infos[i].uuid, infos[i].uuid + DRM_UUID_LEN);
360 break;
361 }
362 }
363 WVLOG_I("[DRM]DrmAdapterImpl::GetUUID, name = %{public}s", name.c_str());
364 return uuid;
365 }
366
ReleaseMediaKeySystem()367 int32_t DrmAdapterImpl::ReleaseMediaKeySystem()
368 {
369 WVLOG_I("[DRM]DrmAdapterImpl::ReleaseMediaKeySystem enter");
370 if (drmKeySystem_ == nullptr) {
371 WVLOG_E("[DRM]drmKeySystem_ is nullptr!");
372 return static_cast<int32_t>(DrmResult::DRM_RESULT_ERROR);
373 }
374
375 {
376 std::lock_guard<std::mutex> lock(mediaKeySystemCallbackMapMutex_);
377 auto iter = mediaKeySystemCallbackMap_.find(drmKeySystem_);
378 if (iter != mediaKeySystemCallbackMap_.end()) {
379 mediaKeySystemCallbackMap_.erase(iter);
380 }
381 }
382
383 Drm_ErrCode ret = OH_MediaKeySystem_Destroy(drmKeySystem_);
384 drmKeySystem_ = nullptr;
385 if (ret != DRM_ERR_OK) {
386 WVLOG_E("[DRM]Failed to release MediaKeySystem.");
387 }
388 return static_cast<int32_t>(DrmResult::DRM_RESULT_OK);
389 }
390
ReleaseMediaKeySession()391 int32_t DrmAdapterImpl::ReleaseMediaKeySession()
392 {
393 WVLOG_I("[DRM]DrmAdapterImpl::ReleaseMediaKeySession enter");
394 if (drmKeySession_ == nullptr) {
395 WVLOG_E("[DRM]drmKeySession_ is nullptr!");
396 return static_cast<int32_t>(DrmResult::DRM_RESULT_ERROR);
397 }
398
399 {
400 std::lock_guard<std::mutex> lock(mediaKeySessionCallbackMutex_);
401 auto iter = mediaKeySessionCallbackMap_.find(drmKeySession_);
402 if (iter != mediaKeySessionCallbackMap_.end()) {
403 mediaKeySessionCallbackMap_.erase(iter);
404 }
405 }
406
407 Drm_ErrCode ret = OH_MediaKeySession_Destroy(drmKeySession_);
408 drmKeySession_ = nullptr;
409 if (ret != DRM_ERR_OK) {
410 WVLOG_E("[DRM]Failed to release MediaKeySession.");
411 }
412 return static_cast<int32_t>(DrmResult::DRM_RESULT_OK);
413 }
414
SystemCallBackWithObj(MediaKeySystem * mediaKeySystem,DRM_EventType eventType,uint8_t * info,int32_t infoLen,char * extra)415 Drm_ErrCode DrmAdapterImpl::SystemCallBackWithObj(
416 MediaKeySystem* mediaKeySystem, DRM_EventType eventType, uint8_t* info, int32_t infoLen, char* extra)
417 {
418 WVLOG_I("[DRM]DrmAdapterImpl::SystemCallBackWithObj enter. eventType = %{public}d.", eventType);
419 if (mediaKeySystem == nullptr) {
420 WVLOG_E("[DRM]DrmAdapterImpl::SystemCallBackWithObj error, mediaKeySystem is nullptr.");
421 return DRM_ERR_INVALID_VAL;
422 }
423
424 std::shared_ptr<DrmCallbackImpl> callback = nullptr;
425 {
426 std::lock_guard<std::mutex> lock(mediaKeySystemCallbackMapMutex_);
427 auto iter = mediaKeySystemCallbackMap_.find(mediaKeySystem);
428 if (iter == mediaKeySystemCallbackMap_.end()) {
429 WVLOG_E("[DRM]DrmAdapterImpl::SystemCallBackWithObj error, mediaKeySystem not found.");
430 return DRM_ERR_INVALID_VAL;
431 }
432 callback = iter->second;
433 }
434
435 if (callback == nullptr) {
436 WVLOG_E("[DRM]DrmAdapterImpl::SystemCallBackWithObj error, callback is nullptr.");
437 return DRM_ERR_INVALID_VAL;
438 }
439
440 Drm_ErrCode ret = DRM_ERR_OK;
441 if (eventType == EVENT_PROVISION_REQUIRED) {
442 uint8_t request[MAX_REQUEST_LENGTH] = { 0x00 };
443 int32_t requestLen = MAX_REQUEST_LENGTH;
444 char defaultUrl[MAX_URL_LENGTH] = { 0x00 };
445 int32_t defaultUrlLen = MAX_URL_LENGTH;
446 ret =
447 OH_MediaKeySystem_GenerateKeySystemRequest(mediaKeySystem, request, &requestLen, defaultUrl, defaultUrlLen);
448 WVLOG_I("[DRM]DrmAdapterImpl::OH_MediaKeySystem_GenerateKeySystemRequest, ret = %{public}d.", ret);
449 if (ret == DRM_ERR_OK) {
450 if (requestLen > MAX_REQUEST_LENGTH) {
451 WVLOG_E("[DRM]OH_MediaKeySystem_GenerateKeySystemRequest error, invalid requestLen.");
452 return DRM_ERR_INVALID_VAL;
453 }
454 std::vector<uint8_t> requestData;
455 std::string requestString;
456 requestData.insert(requestData.begin(), request, request + requestLen);
457 requestString.assign(requestData.begin(), requestData.end());
458 callback->OnProvisionRequest(std::string(defaultUrl), requestString);
459 }
460 }
461 return ret;
462 }
463
OnSessionExpirationUpdate(MediaKeySession * drmKeySession,uint8_t * info,int32_t infoLen)464 void DrmAdapterImpl::OnSessionExpirationUpdate(MediaKeySession* drmKeySession, uint8_t* info, int32_t infoLen)
465 {
466 WVLOG_I("[DRM]DrmAdapterImpl::OnSessionExpirationUpdate enter. infoLen = %{public}d", infoLen);
467 if (!drmKeySession) {
468 WVLOG_I("[DRM]DrmAdapterImpl::OnSessionExpirationUpdate drmKeySession is nullptr.");
469 return;
470 }
471 std::shared_ptr<DrmCallbackImpl> callback = nullptr;
472 {
473 std::lock_guard<std::mutex> lock(mediaKeySessionCallbackMutex_);
474 auto iter = mediaKeySessionCallbackMap_.find(drmKeySession);
475 if (iter == mediaKeySessionCallbackMap_.end()) {
476 WVLOG_E("[DRM]DrmAdapterImpl::OnSessionExpirationUpdate error, mediaKeySession not found.");
477 return;
478 }
479 callback = iter->second;
480 }
481
482 if (callback == nullptr) {
483 WVLOG_E("[DRM]DrmAdapterImpl::OnSessionExpirationUpdate error, callback is nullptr.");
484 return;
485 }
486
487 uint64_t timeStamp = 0;
488 if (info != nullptr && infoLen > 0 && infoLen <= EXPIRATION_INFO_MAX_LEN) {
489 std::vector<uint8_t> infoData;
490 std::string infoString;
491 infoData.insert(infoData.begin(), info, info + infoLen);
492 infoString.assign(infoData.begin(), infoData.end());
493 WVLOG_I("[DRM]DrmAdapterImpl::OnSessionExpirationUpdate. infoString = %{public}s", infoString.c_str());
494 EndsWithAndRemove(infoString, "ms");
495 if (IsValidNumber(infoString)) {
496 if (infoString.size() > MILLISECOND_DIGITS) {
497 infoString.erase(infoString.size() - MILLISECOND_DIGITS, MILLISECOND_DIGITS);
498 }
499 timeStamp = std::strtoull(infoString.c_str(), nullptr, EXPIRATION_INFO_BASE);
500 }
501 }
502
503 auto sessionInfo = callback->GetMediaKeySessionInfo(drmKeySession);
504 if (sessionInfo) {
505 auto sessionId = sessionInfo->GetSessionId();
506 if (sessionId) {
507 WVLOG_I("[DRM]DrmAdapterImpl::SessionKeyChangeCallBackWithObj emeId = %{public}s.",
508 sessionId->EmeId().c_str());
509 callback->OnSessionExpirationUpdate(sessionId->EmeId(), timeStamp * MILLISECOND_IN_SECOND);
510 }
511 }
512 }
513
GetKeyRequest(MediaKeySession * drmKeySession,uint8_t * info,int32_t infoLen)514 void DrmAdapterImpl::GetKeyRequest(MediaKeySession* drmKeySession, uint8_t* info, int32_t infoLen)
515 {
516 WVLOG_I("[DRM]DrmAdapterImpl::GetKeyRequest enter.");
517 if (drmKeySession == nullptr) {
518 return;
519 }
520 std::shared_ptr<DrmCallbackImpl> callback = nullptr;
521 {
522 std::lock_guard<std::mutex> lock(mediaKeySessionCallbackMutex_);
523 auto iter = mediaKeySessionCallbackMap_.find(drmKeySession);
524 if (iter == mediaKeySessionCallbackMap_.end()) {
525 return;
526 }
527 callback = iter->second;
528 }
529 if (callback == nullptr) {
530 return;
531 }
532 DRM_MediaKeyRequestInfo reqInfo;
533 DRM_MediaKeyRequest mediaKeyRequest;
534 auto sessionInfo = callback->GetMediaKeySessionInfo(drmKeySession);
535 if (!sessionInfo) {
536 return;
537 }
538 reqInfo.type = static_cast<DRM_MediaKeyType>(sessionInfo->KeyType());
539 reqInfo.initDataLen = infoLen;
540 reqInfo.optionsCount = 0;
541 errno_t retCopy =
542 memcpy_s(reqInfo.mimeType, MAX_MIMETYPE_LEN, sessionInfo->MimeType().c_str(), sessionInfo->MimeType().length());
543 if (retCopy != 0) {
544 return;
545 }
546 if (infoLen > 0) {
547 retCopy = memcpy_s(reqInfo.initData, MAX_INIT_DATA_LEN, info, infoLen);
548 if (retCopy != 0) {
549 return;
550 }
551 }
552 Drm_ErrCode ret = OH_MediaKeySession_GenerateMediaKeyRequest(drmKeySession, &reqInfo, &mediaKeyRequest);
553 WVLOG_I("[DRM]DrmAdapterImpl::OH_MediaKeySession_GenerateMediaKeyRequest, ret = %{public}d", ret);
554 if (ret != DRM_ERR_OK) {
555 return;
556 }
557 int32_t requestType = static_cast<int32_t>(mediaKeyRequest.type);
558 std::vector<uint8_t> requestData;
559 requestData.insert(requestData.begin(), mediaKeyRequest.data, mediaKeyRequest.data + mediaKeyRequest.dataLen);
560 auto sessionId = sessionInfo->GetSessionId();
561 if (sessionId) {
562 callback->OnSessionMessage(sessionId->EmeId(), requestType, requestData);
563 }
564 }
565
SessionEventCallBackWithObj(MediaKeySession * mediaKeySession,DRM_EventType eventType,uint8_t * info,int32_t infoLen,char * extra)566 Drm_ErrCode DrmAdapterImpl::SessionEventCallBackWithObj(
567 MediaKeySession* mediaKeySession, DRM_EventType eventType, uint8_t* info, int32_t infoLen, char* extra)
568 {
569 WVLOG_I("[DRM]DrmAdapterImpl::SessionEventCallBackWithObj: %{public}d, infoLen = %{public}d",
570 static_cast<int32_t>(eventType), infoLen);
571 switch (eventType) {
572 case EVENT_KEY_REQUIRED:
573 GetKeyRequest(mediaKeySession, info, infoLen);
574 break;
575 case EVENT_EXPIRATION_UPDATE:
576 OnSessionExpirationUpdate(mediaKeySession, info, infoLen);
577 break;
578 case EVENT_DRM_BASE:
579 case EVENT_PROVISION_REQUIRED:
580 case EVENT_KEY_EXPIRED:
581 case EVENT_VENDOR_DEFINED:
582 default:
583 break;
584 }
585 return DRM_ERR_OK;
586 }
587
SessionKeyChangeCallBackWithObj(MediaKeySession * mediaKeySession,DRM_KeysInfo * keysInfo,bool newKeysAvailable)588 Drm_ErrCode DrmAdapterImpl::SessionKeyChangeCallBackWithObj(
589 MediaKeySession* mediaKeySession, DRM_KeysInfo* keysInfo, bool newKeysAvailable)
590 {
591 WVLOG_I("[DRM]DrmAdapterImpl::SessionKeyChangeCallBackWithObj enter.");
592 if (keysInfo == nullptr) {
593 WVLOG_E("[DRM]DrmAdapterImpl::SessionKeyChangeCallBackWithObj: keysInfo is nullptr.");
594 return DRM_ERR_INVALID_VAL;
595 }
596
597 std::shared_ptr<DrmCallbackImpl> callback = nullptr;
598 {
599 std::lock_guard<std::mutex> lock(mediaKeySessionCallbackMutex_);
600 auto sessionIter = mediaKeySessionCallbackMap_.find(mediaKeySession);
601 if (sessionIter == mediaKeySessionCallbackMap_.end()) {
602 WVLOG_E("[DRM]DrmAdapterImpl::SessionKeyChangeCallBackWithObj: mediaKeySession is invalid.");
603 return DRM_ERR_INVALID_VAL;
604 }
605 callback = sessionIter->second;
606 }
607 std::vector<std::string> keyIdArray;
608 std::vector<uint32_t> statusArray;
609 for (uint32_t i = 0; i < keysInfo->keysInfoCount; i++) {
610 std::string statusStr = std::string(keysInfo->statusValue[i]);
611 uint32_t statusCode = static_cast<uint32_t>(KeyStatus::KEY_STATUS_INTERNAL_ERROR);
612
613 auto iter = KeyStatusMap.find(statusStr);
614 if (iter != KeyStatusMap.end()) {
615 statusCode = iter->second;
616 }
617
618 std::string keyIdStr = toHexString(keysInfo->keyId[i], MAX_KEY_ID_LEN);
619 keyIdArray.push_back(keyIdStr);
620 statusArray.push_back(statusCode);
621 }
622
623 if (callback) {
624 auto sessionInfo = callback->GetMediaKeySessionInfo(mediaKeySession);
625 if (sessionInfo) {
626 auto sessionId = sessionInfo->GetSessionId();
627 if (sessionId) {
628 WVLOG_I("[DRM]DrmAdapterImpl::SessionKeyChangeCallBackWithObj emeId: %{public}s, isRelease: %{public}d",
629 sessionId->EmeId().c_str(), sessionInfo->IsRelease());
630 callback->OnSessionKeysChange(
631 sessionId->EmeId(), keyIdArray, statusArray, newKeysAvailable, sessionInfo->IsRelease());
632 }
633 }
634 } else {
635 WVLOG_E("[DRM]DrmAdapterImpl::SessionKeyChangeCallBackWithObj, callback is nullptr.");
636 }
637 return DRM_ERR_OK;
638 }
639
CreateKeySystem(const std::string & name,const std::string & origin,int32_t securityLevel)640 int32_t DrmAdapterImpl::CreateKeySystem(const std::string& name, const std::string& origin, int32_t securityLevel)
641 {
642 WVLOG_I("[DRM]DrmAdapterImpl::CreateKeySystem enter.");
643 if (name.empty()) {
644 WVLOG_E("[DRM]name is empty!");
645 return static_cast<int32_t>(DrmResult::DRM_RESULT_ERROR);
646 }
647
648 Drm_ErrCode ret = OH_MediaKeySystem_Create(name.c_str(), &drmKeySystem_);
649 WVLOG_I("[DRM]DrmAdapterImpl::OH_MediaKeySystem_Create name: %{public}s, ret: %{public}d.", name.c_str(), ret);
650 if (ret != DRM_ERR_OK) {
651 WVLOG_E("[DRM]DrmAdapterImpl::CreateKeySystem failed.");
652 return static_cast<int32_t>(DrmResult::DRM_RESULT_ERROR);
653 }
654 {
655 std::lock_guard<std::mutex> lock(mediaKeySystemCallbackMapMutex_);
656 mediaKeySystemCallbackMap_[drmKeySystem_] = callback_;
657 }
658
659 if (name == WIDEVINE_NAME) {
660 SetConfigurationString(PRIVACY_MODE, ENABLE);
661 SetConfigurationString(SESSION_SHARING, ENABLE);
662 int32_t configRet = SetConfigurationString(ORIGIN, origin);
663 if (configRet != static_cast<int32_t>(DrmResult::DRM_RESULT_OK)) {
664 WVLOG_E("[DRM]DrmAdapterImpl::CreateKeySystem ORIGIN set failed.");
665 }
666 keySystemType_ = KeySystemType::WIDEVINE;
667 } else if (name == WISEPLAY_NAME) {
668 keySystemType_ = KeySystemType::WISEPLAY;
669 }
670
671 ret = OH_MediaKeySystem_SetCallback(drmKeySystem_, SystemCallBackWithObj);
672 if (ret != DRM_ERR_OK) {
673 WVLOG_E("[DRM]OH_MediaKeySystem_SetCallback failed.");
674 return static_cast<int32_t>(DrmResult::DRM_RESULT_ERROR);
675 }
676 contentProtectionLevel_ = GetContentProtectionLevelFromSecurityLevel(securityLevel);
677 int32_t iRet = CreateMediaKeySession();
678 if (iRet != 0) {
679 WVLOG_E("[DRM]OH_MediaKeySystem_CreateMediaKeySession failed.");
680 return static_cast<int32_t>(DrmResult::DRM_RESULT_ERROR);
681 }
682 WVLOG_I("[DRM]DrmAdapterImpl::CreateKeySystem exit.");
683 return static_cast<int32_t>(DrmResult::DRM_RESULT_OK);
684 }
685
CreateMediaKeySession(std::string emeId)686 int32_t DrmAdapterImpl::CreateMediaKeySession(std::string emeId)
687 {
688 WVLOG_I("[DRM]DrmAdapterImpl::CreateMediaKeySession enter.");
689 if (drmKeySystem_ == nullptr) {
690 WVLOG_E("[DRM]drmKeySystem_ is nullptr!");
691 return static_cast<int32_t>(DrmResult::DRM_RESULT_ERROR);
692 }
693
694 Drm_ErrCode ret = DRM_ERR_OK;
695 MediaKeySession* drmKeySession = nullptr;
696 ret = OH_MediaKeySystem_CreateMediaKeySession(drmKeySystem_, &contentProtectionLevel_, &drmKeySession);
697 WVLOG_I("[DRM]DrmAdapterImpl::OH_MediaKeySystem_CreateMediaKeySession ret: %{public}d.", ret);
698 if (ret != DRM_ERR_OK || (drmKeySession == nullptr)) {
699 WVLOG_E("[DRM]DrmAdapterImpl::CreateMediaKeySession failed.");
700 return static_cast<int32_t>(DrmResult::DRM_RESULT_ERROR);
701 }
702 {
703 std::lock_guard<std::mutex> lock(mediaKeySessionCallbackMutex_);
704 mediaKeySessionCallbackMap_[drmKeySession] = callback_;
705 }
706 OH_MediaKeySession_Callback sessionCallback = { SessionEventCallBackWithObj, SessionKeyChangeCallBackWithObj };
707 ret = OH_MediaKeySession_SetCallback(drmKeySession, &sessionCallback);
708 if (ret != DRM_ERR_OK) {
709 WVLOG_E("[DRM]DrmAdapterImpl::CreateMediaKeySession failed.");
710 return static_cast<int32_t>(DrmResult::DRM_RESULT_ERROR);
711 }
712 if (emeId.empty()) {
713 drmKeySession_ = drmKeySession;
714 // Notify the app that the CDM is ready.
715 if (callback_) {
716 callback_->OnMediaKeySessionReady(reinterpret_cast<OHOSMediaKeySession>(drmKeySession));
717 }
718 } else {
719 std::lock_guard<std::mutex> lock(mediaKeySessionMutex_);
720 emeMediaKeySessionMap_[emeId] = drmKeySession;
721 }
722 return static_cast<int32_t>(DrmResult::DRM_RESULT_OK);
723 }
724
ReleaseMediaKeySession(MediaKeySession * drmKeySession)725 int32_t DrmAdapterImpl::ReleaseMediaKeySession(MediaKeySession* drmKeySession)
726 {
727 WVLOG_I("[DRM]DrmAdapterImpl::ReleaseMediaKeySession enter");
728 if (drmKeySession == nullptr) {
729 WVLOG_E("[DRM]drmKeySession is nullptr!");
730 return static_cast<int32_t>(DrmResult::DRM_RESULT_ERROR);
731 }
732
733 {
734 std::lock_guard<std::mutex> lock(mediaKeySessionCallbackMutex_);
735 auto iter = mediaKeySessionCallbackMap_.find(drmKeySession);
736 if (iter != mediaKeySessionCallbackMap_.end()) {
737 mediaKeySessionCallbackMap_.erase(iter);
738 }
739 }
740
741 Drm_ErrCode ret = OH_MediaKeySession_Destroy(drmKeySession);
742 drmKeySession = nullptr;
743 if (ret != DRM_ERR_OK) {
744 WVLOG_E("[DRM]Failed to release MediaKeySession.");
745 }
746 return static_cast<int32_t>(DrmResult::DRM_RESULT_OK);
747 }
748
GetMediaKeySession(std::string emeId)749 MediaKeySession* DrmAdapterImpl::GetMediaKeySession(std::string emeId)
750 {
751 if (keySystemType_ == KeySystemType::WISEPLAY) {
752 return drmKeySession_;
753 }
754 MediaKeySession* drmKeySession = nullptr;
755 if (emeId.empty()) {
756 return drmKeySession;
757 }
758 std::lock_guard<std::mutex> lock(mediaKeySessionMutex_);
759 auto iter = emeMediaKeySessionMap_.find(emeId);
760 if (iter != emeMediaKeySessionMap_.end()) {
761 drmKeySession = iter->second;
762 } else {
763 WVLOG_E("[DRM]MediaKeySession not found.");
764 }
765 return drmKeySession;
766 }
767
SetConfigurationString(const std::string & configName,const std::string & value)768 int32_t DrmAdapterImpl::SetConfigurationString(const std::string& configName, const std::string& value)
769 {
770 WVLOG_I("[DRM]DrmAdapterImpl::SetConfigurationString configName: %{public}s, vale: %{public}s", configName.c_str(),
771 value.c_str());
772 if (configName.empty()) {
773 WVLOG_E("[DRM]configName is empty!");
774 return static_cast<int32_t>(DrmResult::DRM_RESULT_ERROR);
775 }
776 if (value.empty()) {
777 WVLOG_E("[DRM]value is empty!");
778 return static_cast<int32_t>(DrmResult::DRM_RESULT_ERROR);
779 }
780 if (drmKeySystem_ == nullptr) {
781 WVLOG_E("[DRM]drmKeySystem_ is nullptr!");
782 return static_cast<int32_t>(DrmResult::DRM_RESULT_ERROR);
783 }
784 Drm_ErrCode ret = OH_MediaKeySystem_SetConfigurationString(drmKeySystem_, configName.c_str(), value.c_str());
785 WVLOG_I("[DRM]DrmAdapterImpl::SetConfigurationString configName: %{public}s, vale: %{public}s, ret: %{public}d.",
786 configName.c_str(), value.c_str(), ret);
787 if (ret != DRM_ERR_OK) {
788 WVLOG_E("[DRM]DrmAdapterImpl::SetConfigurationString failed.");
789 return static_cast<int32_t>(DrmResult::DRM_RESULT_ERROR);
790 }
791 return static_cast<int32_t>(DrmResult::DRM_RESULT_OK);
792 }
793
GetConfigurationString(const std::string & configName,char * value,int32_t valueLen)794 int32_t DrmAdapterImpl::GetConfigurationString(const std::string& configName, char* value, int32_t valueLen)
795 {
796 WVLOG_I("[DRM]DrmAdapterImpl::GetConfigurationString");
797
798 if (configName.empty()) {
799 WVLOG_E("[DRM]configName is empty!");
800 return static_cast<int32_t>(DrmResult::DRM_RESULT_ERROR);
801 }
802 if (value == nullptr) {
803 WVLOG_E("[DRM]value is nullptr!");
804 return static_cast<int32_t>(DrmResult::DRM_RESULT_ERROR);
805 }
806 if (drmKeySystem_ == nullptr) {
807 WVLOG_E("[DRM]drmKeySystem_ is nullptr!");
808 return static_cast<int32_t>(DrmResult::DRM_RESULT_ERROR);
809 }
810 Drm_ErrCode ret = OH_MediaKeySystem_GetConfigurationString(drmKeySystem_, configName.c_str(), value, valueLen);
811 if (ret != DRM_ERR_OK) {
812 WVLOG_E("[DRM]DrmAdapterImpl::GetConfigurationString failed.");
813 return static_cast<int32_t>(DrmResult::DRM_RESULT_ERROR);
814 }
815 return static_cast<int32_t>(DrmResult::DRM_RESULT_OK);
816 }
817
SetConfigurationByteArray(const std::string & configName,const uint8_t * value,int32_t valueLen)818 int32_t DrmAdapterImpl::SetConfigurationByteArray(const std::string& configName, const uint8_t* value, int32_t valueLen)
819 {
820 WVLOG_I("[DRM]DrmAdapterImpl::SetConfigurationByteArray");
821
822 if (configName.empty()) {
823 WVLOG_E("[DRM]configName is empty!");
824 return static_cast<int32_t>(DrmResult::DRM_RESULT_ERROR);
825 }
826 if (value == nullptr) {
827 WVLOG_E("[DRM]value is nullptr!");
828 return static_cast<int32_t>(DrmResult::DRM_RESULT_ERROR);
829 }
830 if (drmKeySystem_ == nullptr) {
831 WVLOG_E("[DRM]drmKeySystem_ is nullptr!");
832 return static_cast<int32_t>(DrmResult::DRM_RESULT_ERROR);
833 }
834 uint8_t* nonConstPtr = const_cast<uint8_t*>(value);
835 Drm_ErrCode ret =
836 OH_MediaKeySystem_SetConfigurationByteArray(drmKeySystem_, configName.c_str(), nonConstPtr, valueLen);
837 if (ret != DRM_ERR_OK) {
838 WVLOG_E("[DRM]DrmAdapterImpl::SetConfigurationByteArray failed.");
839 return static_cast<int32_t>(DrmResult::DRM_RESULT_ERROR);
840 }
841 return ret;
842 }
843
GetConfigurationByteArray(const std::string & configName,uint8_t * value,int32_t * valueLen)844 int32_t DrmAdapterImpl::GetConfigurationByteArray(const std::string& configName, uint8_t* value, int32_t* valueLen)
845 {
846 WVLOG_I("[DRM]DrmAdapterImpl::GetConfigurationByteArray");
847 if (configName.empty()) {
848 WVLOG_E("[DRM]configName is empty!");
849 return static_cast<int32_t>(DrmResult::DRM_RESULT_ERROR);
850 }
851 if (value == nullptr) {
852 WVLOG_E("[DRM]value is nullptr!");
853 return static_cast<int32_t>(DrmResult::DRM_RESULT_ERROR);
854 }
855 if (valueLen == nullptr) {
856 WVLOG_E("[DRM]valueLen is nullptr!");
857 return static_cast<int32_t>(DrmResult::DRM_RESULT_ERROR);
858 }
859 if (drmKeySystem_ == nullptr) {
860 WVLOG_E("[DRM]drmKeySystem_ is nullptr!");
861 return static_cast<int32_t>(DrmResult::DRM_RESULT_ERROR);
862 }
863 Drm_ErrCode ret = OH_MediaKeySystem_GetConfigurationByteArray(drmKeySystem_, configName.c_str(), value, valueLen);
864 if (ret != DRM_ERR_OK) {
865 WVLOG_E("[DRM]DrmAdapterImpl::GetConfigurationByteArray failed.");
866 return static_cast<int32_t>(DrmResult::DRM_RESULT_ERROR);
867 }
868 return static_cast<int32_t>(DrmResult::DRM_RESULT_OK);
869 }
870
GetMaxContentProtectionLevel(int32_t & level)871 int32_t DrmAdapterImpl::GetMaxContentProtectionLevel(int32_t& level)
872 {
873 WVLOG_I("[DRM]DrmAdapterImpl::GetMaxContentProtectionLevel enter.");
874 if (drmKeySystem_ == nullptr) {
875 WVLOG_E("[DRM]drmKeySystem_ is nullptr!");
876 return static_cast<int32_t>(DrmResult::DRM_RESULT_ERROR);
877 }
878
879 DRM_ContentProtectionLevel contentProtectionLevel = CONTENT_PROTECTION_LEVEL_UNKNOWN;
880
881 Drm_ErrCode ret = OH_MediaKeySystem_GetMaxContentProtectionLevel(drmKeySystem_, &contentProtectionLevel);
882 level = contentProtectionLevel;
883 if (ret != DRM_ERR_OK) {
884 WVLOG_E("[DRM]DrmAdapterImpl::GetMaxContentProtectionLevel failed.");
885 }
886 return static_cast<int32_t>(DrmResult::DRM_RESULT_OK);
887 }
888
StorageProvisionedResult(bool result)889 void DrmAdapterImpl::StorageProvisionedResult(bool result)
890 {
891 WVLOG_I("[DRM]DrmAdapterImpl::StorageProvisionedResult enter, result = %{public}d: ", result);
892 if (drmKeySession_ == nullptr) {
893 if (result) {
894 CreateMediaKeySession();
895 }
896 }
897 }
898
StorageSaveInfoResult(bool result,int32_t type)899 void DrmAdapterImpl::StorageSaveInfoResult(bool result, int32_t type)
900 {
901 WVLOG_I("[DRM]DrmAdapterImpl::StorageSaveInfoResult enter, result = %{public}d: ", result);
902 if (!result) {
903 if (callback_) {
904 callback_->OnPromiseRejected(removeSessionPromiseId_, "Fail to update persistent storage");
905 }
906 return;
907 }
908 if (type != static_cast<int32_t>(MediaKeyType::MEDIA_KEY_TYPE_RELEASE)) {
909 HandleKeyUpdatedCallback(updateSessionPromiseId_, result);
910 WVLOG_I("[DRM]DrmAdapterImpl::StorageSaveInfoResult result = %{public}d: ", result);
911 return;
912 }
913 if (!drmKeySystem_) {
914 return;
915 }
916 uint8_t releaseRequest[MAX_MEDIA_KEY_REQUEST_DATA_LEN];
917 int32_t releaseRequestLen = MAX_MEDIA_KEY_REQUEST_DATA_LEN;
918 std::shared_ptr<SessionId> sessionId = GetSessionIdByEmeId(releaseEmeId_);
919 if (sessionId == nullptr) {
920 if (callback_) {
921 callback_->OnPromiseRejected(removeSessionPromiseId_, "Session doesn't exist");
922 }
923 return;
924 }
925 MediaKeySession* drmKeySession = GetMediaKeySession(releaseEmeId_);
926 if (!drmKeySession) {
927 return;
928 }
929 Drm_ErrCode ret = OH_MediaKeySession_GenerateOfflineReleaseRequest(
930 drmKeySession, sessionId->KeySetId(), sessionId->KeySetIdLen(), releaseRequest, &releaseRequestLen);
931 WVLOG_I("[DRM]DrmAdapterImpl::OH_MediaKeySession_GenerateOfflineReleaseRequest ret = %{public}d: ", ret);
932 if (ret != DRM_ERR_OK) {
933 if (callback_) {
934 callback_->OnPromiseRejected(removeSessionPromiseId_, "Fail to generate key release request");
935 }
936 return;
937 }
938 if (releaseRequestLen > MAX_MEDIA_KEY_REQUEST_DATA_LEN) {
939 WVLOG_E("[DRM]OH_MediaKeySession_GenerateOfflineReleaseRequest error, invalid releaseRequestLen.");
940 return;
941 }
942 std::vector<uint8_t> requestData;
943 requestData.insert(requestData.begin(), releaseRequest, releaseRequest + releaseRequestLen);
944 int32_t requestType = static_cast<int32_t>(MediaKeyType::MEDIA_KEY_TYPE_RELEASE);
945 if (callback_) {
946 callback_->OnPromiseResolved(removeSessionPromiseId_);
947 callback_->OnSessionMessage(releaseEmeId_, requestType, requestData);
948 }
949 }
950
StorageLoadInfoResult(const std::string & emeId,const std::vector<uint8_t> & keySetId,const std::string & mimeType,uint32_t keyType)951 void DrmAdapterImpl::StorageLoadInfoResult(
952 const std::string& emeId, const std::vector<uint8_t>& keySetId, const std::string& mimeType, uint32_t keyType)
953 {
954 WVLOG_I("[DRM]DrmAdapterImpl::StorageLoadInfoResult enter, emeId = %{public}s: ", emeId.c_str());
955 if (keySetId.size() == 0) {
956 WVLOG_I("[DRM]DrmAdapterImpl::StorageLoadInfoResult emeId = %{public}s: ", emeId.c_str());
957 if (callback_) {
958 callback_->OnPromiseResolvedWithSession(loadSessionPromiseId_, "");
959 }
960 return;
961 }
962
963 // Loading same persistent license into different sessions isn't
964 // supported.
965 if (GetSessionIdByEmeId(emeId) != nullptr) {
966 return;
967 }
968
969 std::shared_ptr<SessionId> sessionId = std::make_shared<SessionId>(emeId, keySetId.data(), keySetId.size());
970 PutSessionInfo(sessionId, mimeType, keyType);
971 LoadSessionWithLoadedStorage(sessionId, loadSessionPromiseId_);
972 }
973
StorageClearInfoResult(bool result,int32_t type)974 void DrmAdapterImpl::StorageClearInfoResult(bool result, int32_t type)
975 {
976 WVLOG_I("[DRM]DrmAdapterImpl::StorageClearInfoResult enter.");
977 if (type == static_cast<int32_t>(ClearInfoType::KEY_RELEASE)) {
978 HandleKeyUpdatedCallback(updateSessionPromiseId_, result);
979 } else if (type == static_cast<int32_t>(ClearInfoType::LOAD_FAIL)) {
980 if (!result) {
981 WVLOG_W("[DRM]Failed to clear persistent storage for non-exist license");
982 }
983 if (callback_) {
984 callback_->OnPromiseResolvedWithSession(loadSessionPromiseId_, "");
985 }
986 }
987 }
988
ProcessKeySystemResponse(const std::string & response,bool isResponseReceived)989 int32_t DrmAdapterImpl::ProcessKeySystemResponse(const std::string& response, bool isResponseReceived)
990 {
991 WVLOG_I("[DRM]DrmAdapterImpl::ProcessKeySystemResponse enter, isResponseReceived : %{public}d", isResponseReceived);
992 if (drmKeySystem_ == nullptr) {
993 WVLOG_E("[DRM]drmKeySystem_ is nullptr!");
994 return static_cast<int32_t>(DrmResult::DRM_RESULT_ERROR);
995 }
996
997 bool success = true;
998 if (isResponseReceived) {
999 int32_t responseLen = static_cast<int32_t>(response.size());
1000 std::vector<uint8_t> vec(responseLen);
1001 errno_t retCopy = memcpy_s(vec.data(), responseLen, response.data(), response.size());
1002 if (retCopy != 0) {
1003 WVLOG_E("[DRM]memcpy_s failed with error.");
1004 return static_cast<int32_t>(DrmResult::DRM_RESULT_ERROR);
1005 }
1006 Drm_ErrCode ret = OH_MediaKeySystem_ProcessKeySystemResponse(drmKeySystem_, vec.data(), responseLen);
1007 WVLOG_I("[DRM]DrmAdapterImpl::OH_MediaKeySystem_ProcessKeySystemResponse ret : %{public}d", ret);
1008 if (ret != DRM_ERR_OK) {
1009 WVLOG_E("[DRM]DrmAdapterImpl::ProcessKeySystemResponse failed.");
1010 success = false;
1011 }
1012 } else {
1013 success = false;
1014 }
1015
1016 if (!success) {
1017 return static_cast<int32_t>(DrmResult::DRM_RESULT_ERROR);
1018 }
1019 if (callback_) {
1020 callback_->OnStorageProvisioned();
1021 }
1022 return static_cast<int32_t>(DrmResult::DRM_RESULT_OK);
1023 }
1024
GetCertificateStatus(int32_t & certStatus)1025 int32_t DrmAdapterImpl::GetCertificateStatus(int32_t& certStatus)
1026 {
1027 WVLOG_I("[DRM]DrmAdapterImpl::GetCertificateStatus");
1028 if (drmKeySystem_ == nullptr) {
1029 WVLOG_E("[DRM]drmKeySystem_ is nullptr!");
1030 return static_cast<int32_t>(DrmResult::DRM_RESULT_ERROR);
1031 }
1032 DRM_CertificateStatus cert = CERT_STATUS_INVALID;
1033 Drm_ErrCode ret = OH_MediaKeySystem_GetCertificateStatus(drmKeySystem_, &cert);
1034 certStatus = cert;
1035 if (ret != DRM_ERR_OK) {
1036 WVLOG_E("[DRM]DrmAdapterImpl::GetCertificateStatus failed.");
1037 }
1038 return static_cast<int32_t>(DrmResult::DRM_RESULT_OK);
1039 }
1040
RegistDrmCallback(std::shared_ptr<DrmCallbackAdapter> callbackAdapter)1041 int32_t DrmAdapterImpl::RegistDrmCallback(std::shared_ptr<DrmCallbackAdapter> callbackAdapter)
1042 {
1043 callback_ = std::make_shared<DrmCallbackImpl>(callbackAdapter);
1044 return DRM_ERR_OK;
1045 }
1046
UpdateSessionResult(bool isKeyRelease,std::shared_ptr<SessionId> sessionId,uint8_t * mediaKeyId,int32_t mediaKeyIdLen)1047 void DrmAdapterImpl::UpdateSessionResult(
1048 bool isKeyRelease, std::shared_ptr<SessionId> sessionId, uint8_t* mediaKeyId, int32_t mediaKeyIdLen)
1049 {
1050 WVLOG_I("[DRM]DrmAdapterImpl::UpdateSessionResult enter.");
1051 if (sessionId == nullptr) {
1052 return;
1053 }
1054
1055 std::shared_ptr<SessionInfo> info = GetSessionInfo(sessionId);
1056 if (info == nullptr) {
1057 WVLOG_E(
1058 "[DRM]DrmAdapterImpl::UpdateSessionResult, info is nullptr, emeId: %{public}s", sessionId->EmeId().c_str());
1059 return;
1060 }
1061
1062 if (isKeyRelease) {
1063 WVLOG_I("[DRM]DrmAdapterImpl::UpdateSessionResult, emeId: %{public}s", sessionId->EmeId().c_str());
1064 ClearPersistentSessionInfoFroKeyRelease(sessionId);
1065 } else if (info->KeyType() == static_cast<int32_t>(MediaKeyType::MEDIA_KEY_TYPE_OFFLINE) && mediaKeyIdLen > 0) {
1066 WVLOG_I("[DRM]DrmAdapterImpl::UpdateSessionResult, emeId: %{public}s", sessionId->EmeId().c_str());
1067 SetKeySetId(sessionId, mediaKeyId, mediaKeyIdLen);
1068 } else {
1069 WVLOG_I("[DRM]DrmAdapterImpl::UpdateSessionResult, emeId: %{public}s", sessionId->EmeId().c_str());
1070 HandleKeyUpdatedCallback(updateSessionPromiseId_, true);
1071 }
1072 }
1073
UpdateSession(uint32_t promiseId,const std::string & emeId,std::vector<uint8_t> response)1074 int32_t DrmAdapterImpl::UpdateSession(uint32_t promiseId, const std::string& emeId, std::vector<uint8_t> response)
1075 {
1076 WVLOG_I("[DRM]DrmAdapterImpl::UpdateSession enter, emeId: %{public}s", emeId.c_str());
1077 MediaKeySession* drmKeySession = GetMediaKeySession(emeId);
1078 if (!drmKeySession) {
1079 WVLOG_E("[DRM]drmKeySession is nullptr!");
1080 return static_cast<int32_t>(DrmResult::DRM_RESULT_ERROR);
1081 }
1082 updateSessionPromiseId_ = promiseId;
1083 std::shared_ptr<SessionId> sessionId = GetSessionIdByEmeId(emeId);
1084 if (sessionId == nullptr) {
1085 if (callback_) {
1086 callback_->OnPromiseRejected(promiseId, "Invalid session in updateSession: " + emeId);
1087 }
1088 return static_cast<int32_t>(DrmResult::DRM_RESULT_ERROR);
1089 }
1090 std::shared_ptr<SessionInfo> info = GetSessionInfo(sessionId);
1091 if (info == nullptr) {
1092 WVLOG_E("[DRM]DrmAdapterImpl::UpdateSession, info is nullptr, emeId: %{public}s", emeId.c_str());
1093 return static_cast<int32_t>(DrmResult::DRM_RESULT_ERROR);
1094 }
1095 bool isKeyRelease = info->KeyType() == static_cast<int32_t>(MediaKeyType::MEDIA_KEY_TYPE_RELEASE);
1096 int32_t mediaKeyIdLen = MAX_KEY_SET_ID_LEN;
1097 uint8_t mediaKeyId[MAX_KEY_SET_ID_LEN] = { 0x00 };
1098 if (isKeyRelease) {
1099 Drm_ErrCode ret = OH_MediaKeySession_ProcessOfflineReleaseResponse(
1100 drmKeySession, sessionId->KeySetId(), sessionId->KeySetIdLen(), response.data(), response.size());
1101 WVLOG_I("[DRM]DrmAdapterImpl::OH_MediaKeySession_ProcessOfflineReleaseResponse, ret: %{public}d", ret);
1102 if (ret != DRM_ERR_OK) {
1103 if (callback_) {
1104 callback_->OnPromiseRejected(promiseId, "Update session failed.");
1105 }
1106 return static_cast<int32_t>(DrmResult::DRM_RESULT_ERROR);
1107 }
1108 } else {
1109 Drm_ErrCode ret = OH_MediaKeySession_ProcessMediaKeyResponse(
1110 drmKeySession, response.data(), response.size(), mediaKeyId, &mediaKeyIdLen);
1111 WVLOG_I("[DRM]DrmAdapterImpl::OH_MediaKeySession_ProcessMediaKeyResponse result. ret: %{public}d", ret);
1112 if (ret != DRM_ERR_OK) {
1113 if (callback_) {
1114 callback_->OnPromiseRejected(promiseId, "Update session failed.");
1115 }
1116 return static_cast<int32_t>(DrmResult::DRM_RESULT_ERROR);
1117 }
1118 if ((keySystemType_ == KeySystemType::WISEPLAY) && callback_) {
1119 callback_->OnMediaLicenseReady(true);
1120 }
1121 }
1122 UpdateSessionResult(isKeyRelease, sessionId, mediaKeyId, mediaKeyIdLen);
1123 return static_cast<int32_t>(DrmResult::DRM_RESULT_OK);
1124 }
1125
CloseSession(uint32_t promiseId,const std::string & emeId)1126 int32_t DrmAdapterImpl::CloseSession(uint32_t promiseId, const std::string& emeId)
1127 {
1128 WVLOG_I("[DRM]DrmAdapterImpl::CloseSession enter.");
1129 if (drmKeySystem_ == nullptr) {
1130 if (callback_) {
1131 callback_->OnPromiseRejected(promiseId, "closeSession() called when MediaDrm is null.");
1132 }
1133 return static_cast<int32_t>(DrmResult::DRM_RESULT_ERROR);
1134 }
1135
1136 std::shared_ptr<SessionId> sessionId = GetSessionIdByEmeId(emeId);
1137 if (sessionId == nullptr) {
1138 if (callback_) {
1139 callback_->OnPromiseRejected(promiseId, "Invalid sessionId in closeSession(): " + emeId);
1140 }
1141 return static_cast<int32_t>(DrmResult::DRM_RESULT_ERROR);
1142 }
1143 if (keySystemType_ == KeySystemType::WIDEVINE) {
1144 MediaKeySession* drmKeySession = GetMediaKeySession(emeId);
1145 if (!drmKeySession) {
1146 return static_cast<int32_t>(DrmResult::DRM_RESULT_ERROR);
1147 }
1148 if (callback_) {
1149 callback_->RemoveMediaKeySessionInfo(drmKeySession);
1150 }
1151 {
1152 std::lock_guard<std::mutex> lock(mediaKeySessionMutex_);
1153 ReleaseMediaKeySession(drmKeySession);
1154 emeMediaKeySessionMap_.erase(emeId);
1155 }
1156 }
1157 RemoveSessionInfo(sessionId);
1158 if (callback_) {
1159 callback_->OnPromiseResolved(promiseId);
1160 callback_->OnSessionClosed(emeId);
1161 }
1162 return static_cast<int32_t>(DrmResult::DRM_RESULT_OK);
1163 }
1164
RemoveSession(uint32_t promiseId,const std::string & emeId)1165 int32_t DrmAdapterImpl::RemoveSession(uint32_t promiseId, const std::string& emeId)
1166 {
1167 WVLOG_I("[DRM]DrmAdapterImpl::RemoveSession enter.");
1168 if (callback_ == nullptr) {
1169 WVLOG_E("[DRM]DrmAdapterImpl::RemoveSession, callback_ is nullptr.");
1170 return static_cast<int32_t>(DrmResult::DRM_RESULT_ERROR);
1171 }
1172 std::shared_ptr<SessionId> sessionId = GetSessionIdByEmeId(emeId);
1173 if (sessionId == nullptr) {
1174 callback_->OnPromiseRejected(promiseId, "Session doesn't exist");
1175 WVLOG_W("[DRM]DrmAdapterImpl::RemoveSession, Session doesn't exist.");
1176 return static_cast<int32_t>(DrmResult::DRM_RESULT_ERROR);
1177 }
1178
1179 std::shared_ptr<SessionInfo> sessionInfo = GetSessionInfo(sessionId);
1180 if (sessionInfo == nullptr) {
1181 callback_->OnPromiseRejected(promiseId, "SessionInfo doesn't exist");
1182 return static_cast<int32_t>(DrmResult::DRM_RESULT_ERROR);
1183 }
1184 if (sessionInfo->KeyType() == static_cast<int32_t>(MediaKeyType::MEDIA_KEY_TYPE_ONLINE)) {
1185 callback_->OnPromiseRejected(promiseId, "Removing temporary session isn't implemented");
1186 return static_cast<int32_t>(DrmResult::DRM_RESULT_ERROR);
1187 }
1188 removeSessionPromiseId_ = promiseId;
1189
1190 releaseEmeId_ = emeId;
1191
1192 SetKeyType(sessionId, static_cast<int32_t>(MediaKeyType::MEDIA_KEY_TYPE_RELEASE));
1193 return static_cast<int32_t>(DrmResult::DRM_RESULT_OK);
1194 }
1195
LoadSession(uint32_t promiseId,const std::string & sessionId)1196 int32_t DrmAdapterImpl::LoadSession(uint32_t promiseId, const std::string& sessionId)
1197 {
1198 WVLOG_I("[DRM]DrmAdapterImpl::LoadSession enter.");
1199 loadSessionPromiseId_ = promiseId;
1200 LoadSessionInfo(sessionId);
1201 return static_cast<int32_t>(DrmResult::DRM_RESULT_OK);
1202 }
1203
ClearMediaKeys()1204 int32_t DrmAdapterImpl::ClearMediaKeys()
1205 {
1206 WVLOG_I("[DRM]DrmAdapterImpl::ClearMediaKeys enter.");
1207 if (drmKeySession_ == nullptr) {
1208 WVLOG_E("[DRM]drmKeySession_ is nullptr!");
1209 return static_cast<int32_t>(DrmResult::DRM_RESULT_ERROR);
1210 }
1211 Drm_ErrCode ret = OH_MediaKeySession_ClearMediaKeys(drmKeySession_);
1212 if (ret != DRM_ERR_OK) {
1213 WVLOG_E("[DRM]DrmAdapterImpl::ClearMediaKeys failed.");
1214 }
1215 return static_cast<int32_t>(DrmResult::DRM_RESULT_OK);
1216 }
1217
GetSecurityLevel()1218 int32_t DrmAdapterImpl::GetSecurityLevel()
1219 {
1220 WVLOG_I("[DRM]DrmAdapterImpl::GetSecurityLevel enter.");
1221 if (drmKeySession_ == nullptr) {
1222 WVLOG_E("[DRM]drmKeySession_ is nullptr!");
1223 return static_cast<int32_t>(DrmResult::DRM_RESULT_ERROR);
1224 }
1225 DRM_ContentProtectionLevel levelData = CONTENT_PROTECTION_LEVEL_SW_CRYPTO;
1226 Drm_ErrCode ret = OH_MediaKeySession_GetContentProtectionLevel(drmKeySession_, &levelData);
1227 if (ret != DRM_ERR_OK) {
1228 WVLOG_E("[DRM]DrmAdapterImpl::GetSecurityLevel failed.");
1229 return static_cast<int32_t>(DrmResult::DRM_RESULT_ERROR);
1230 }
1231 int32_t securityLevel = GetSecurityLevelFromContentProtectionLevel(static_cast<int32_t>(levelData));
1232 return securityLevel;
1233 }
1234
RequireSecureDecoderModule(const std::string & mimeType,bool & status)1235 int32_t DrmAdapterImpl::RequireSecureDecoderModule(const std::string& mimeType, bool& status)
1236 {
1237 WVLOG_I("[DRM]DrmAdapterImpl::RequireSecureDecoderModule enter.");
1238 if (mimeType.empty()) {
1239 WVLOG_E("[DRM]mimeType is empty!");
1240 return static_cast<int32_t>(DrmResult::DRM_RESULT_ERROR);
1241 }
1242 if (drmKeySession_ == nullptr) {
1243 WVLOG_E("[DRM]drmKeySession_ is nullptr!");
1244 return static_cast<int32_t>(DrmResult::DRM_RESULT_ERROR);
1245 }
1246 bool stas = false;
1247 Drm_ErrCode ret = OH_MediaKeySession_RequireSecureDecoderModule(drmKeySession_, mimeType.c_str(), &stas);
1248 status = stas;
1249 if (ret != DRM_ERR_OK) {
1250 WVLOG_E("[DRM]DrmAdapterImpl::RequireSecureDecoderModule failed.");
1251 return static_cast<int32_t>(DrmResult::DRM_RESULT_ERROR);
1252 }
1253 return static_cast<int32_t>(DrmResult::DRM_RESULT_OK);
1254 }
1255
GenerateMediaKeyRequest(const std::string & emeId,int32_t type,int32_t initDataLen,const std::vector<uint8_t> & initData,const std::string & mimeType,uint32_t promiseId)1256 int32_t DrmAdapterImpl::GenerateMediaKeyRequest(const std::string& emeId, int32_t type, int32_t initDataLen,
1257 const std::vector<uint8_t>& initData, const std::string& mimeType, uint32_t promiseId)
1258 {
1259 WVLOG_I("[DRM]DrmAdapterImpl::GenerateMediaKeyRequest enter, emeId = %{public}s", emeId.c_str());
1260 if (!callback_) {
1261 return static_cast<int32_t>(DrmResult::DRM_RESULT_ERROR);
1262 }
1263 if (drmKeySystem_ == nullptr || drmKeySession_ == nullptr) {
1264 callback_->OnPromiseRejected(promiseId, "DrmKeySystem released previously.");
1265 return static_cast<int32_t>(DrmResult::DRM_RESULT_ERROR);
1266 }
1267 std::shared_ptr<SessionId> sessionId = SessionId::CreateSessionId(emeId);
1268 DRM_MediaKeyRequestInfo info;
1269 DRM_MediaKeyRequest mediaKeyRequest;
1270 info.type = static_cast<DRM_MediaKeyType>(type);
1271 info.initDataLen = initDataLen;
1272 info.optionsCount = 0;
1273 if (memcpy_s(info.mimeType, MAX_MIMETYPE_LEN, mimeType.c_str(), mimeType.length()) != 0) {
1274 WVLOG_E("[DRM]memcpy_s failed with error.");
1275 return static_cast<int32_t>(DrmResult::DRM_RESULT_ERROR);
1276 }
1277 if (memcpy_s(info.initData, MAX_INIT_DATA_LEN, initData.data(), initData.size()) != 0) {
1278 WVLOG_E("[DRM]memcpy_s failed with error.");
1279 return static_cast<int32_t>(DrmResult::DRM_RESULT_ERROR);
1280 }
1281 if (keySystemType_ == KeySystemType::WIDEVINE) {
1282 int32_t iRet = CreateMediaKeySession(emeId);
1283 if (iRet != 0) {
1284 return static_cast<int32_t>(DrmResult::DRM_RESULT_ERROR);
1285 }
1286 }
1287 MediaKeySession* drmKeySession = GetMediaKeySession(emeId);
1288 if (!drmKeySession) {
1289 return static_cast<int32_t>(DrmResult::DRM_RESULT_ERROR);
1290 }
1291 Drm_ErrCode ret = OH_MediaKeySession_GenerateMediaKeyRequest(drmKeySession, &info, &mediaKeyRequest);
1292 WVLOG_I("[DRM]DrmAdapterImpl::OH_MediaKeySession_GenerateMediaKeyRequest, ret = %{public}d", ret);
1293 if (ret != DRM_ERR_OK) {
1294 callback_->OnPromiseRejected(promiseId, "Generate request failed.");
1295 return static_cast<int32_t>(DrmResult::DRM_RESULT_ERROR);
1296 }
1297 int32_t requestType = static_cast<int32_t>(mediaKeyRequest.type);
1298 std::vector<uint8_t> requestData;
1299 requestData.insert(requestData.begin(), mediaKeyRequest.data, mediaKeyRequest.data + mediaKeyRequest.dataLen);
1300 PutSessionInfo(sessionId, mimeType, type);
1301 std::shared_ptr<SessionInfo> sessionInfo = GetSessionInfo(sessionId);
1302 callback_->UpdateMediaKeySessionInfoMap(drmKeySession, sessionInfo);
1303 callback_->OnPromiseResolvedWithSession(promiseId, emeId);
1304 callback_->OnSessionMessage(emeId, requestType, requestData);
1305 return static_cast<int32_t>(DrmResult::DRM_RESULT_OK);
1306 }
1307
1308 // private
PutSessionInfo(std::shared_ptr<SessionId> sessionId,const std::string & mimeType,int32_t type)1309 void DrmAdapterImpl::PutSessionInfo(std::shared_ptr<SessionId> sessionId, const std::string& mimeType, int32_t type)
1310 {
1311 if (sessionId == nullptr) {
1312 return;
1313 }
1314 std::shared_ptr<SessionInfo> info = std::make_shared<SessionInfo>(sessionId, mimeType, type);
1315 WVLOG_I("[DRM]DrmAdapterImpl::PutSessionInfo, emeId = %{public}s", sessionId->EmeId().c_str());
1316 emeSessionInfoMap_[sessionId->EmeId()] = info;
1317 }
1318
GetSessionInfo(std::shared_ptr<SessionId> sessionId)1319 std::shared_ptr<SessionInfo> DrmAdapterImpl::GetSessionInfo(std::shared_ptr<SessionId> sessionId)
1320 {
1321 if (sessionId == nullptr) {
1322 WVLOG_I("[DRM]DrmAdapterImpl::GetSessionInfo, sessionId is nullptr.");
1323 return nullptr;
1324 }
1325 WVLOG_I("[DRM]DrmAdapterImpl::GetSessionInfo, emeId = %{public}s", sessionId->EmeId().c_str());
1326 auto iter = emeSessionInfoMap_.find(sessionId->EmeId());
1327 if (iter != emeSessionInfoMap_.end()) {
1328 WVLOG_I("[DRM]DrmAdapterImpl::GetSessionInfo, find.");
1329 return iter->second;
1330 }
1331 WVLOG_I("[DRM]DrmAdapterImpl::GetSessionInfo, ret is nullptr.");
1332 return nullptr;
1333 }
1334
GetSessionIdByEmeId(const std::string & emeId)1335 std::shared_ptr<SessionId> DrmAdapterImpl::GetSessionIdByEmeId(const std::string& emeId)
1336 {
1337 auto iter = emeSessionInfoMap_.find(emeId);
1338 if (iter != emeSessionInfoMap_.end()) {
1339 std::shared_ptr<SessionInfo> info = iter->second;
1340 if (info != nullptr) {
1341 return info->GetSessionId();
1342 }
1343 }
1344 return nullptr;
1345 }
1346
RemoveSessionInfo(std::shared_ptr<SessionId> sessionId)1347 void DrmAdapterImpl::RemoveSessionInfo(std::shared_ptr<SessionId> sessionId)
1348 {
1349 WVLOG_I("[DRM]DrmAdapterImpl::RemoveSessionInfo.");
1350 /**
1351 * Remove session and related infomration from memory, but doesn't touch
1352 * persistent storage.
1353 */
1354 if (sessionId == nullptr) {
1355 return;
1356 }
1357 std::shared_ptr<SessionInfo> info = GetSessionInfo(sessionId);
1358 if (info != nullptr) {
1359 emeSessionInfoMap_.erase(sessionId->EmeId());
1360 }
1361 }
1362
LoadSessionInfo(const std::string & emeId)1363 void DrmAdapterImpl::LoadSessionInfo(const std::string& emeId)
1364 {
1365 WVLOG_I("[DRM]DrmAdapterImpl::LoadSessionInfo.");
1366 if (callback_) {
1367 callback_->OnStorageLoadInfo(emeId);
1368 }
1369 }
1370
LoadSessionWithLoadedStorage(std::shared_ptr<SessionId> sessionId,uint32_t promiseId)1371 void DrmAdapterImpl::LoadSessionWithLoadedStorage(std::shared_ptr<SessionId> sessionId, uint32_t promiseId)
1372 {
1373 WVLOG_I("[DRM]DrmAdapterImpl::LoadSessionWithLoadedStorage.");
1374 if (sessionId == nullptr || callback_ == nullptr) {
1375 return;
1376 }
1377 std::shared_ptr<SessionInfo> info = GetSessionInfo(sessionId);
1378 if (info == nullptr) {
1379 WVLOG_E("[DRM]DrmAdapterImpl::LoadSessionWithLoadedStorage, info is null.");
1380 return;
1381 }
1382 if (info->KeyType() == static_cast<int32_t>(MediaKeyType::MEDIA_KEY_TYPE_RELEASE)) {
1383 callback_->OnPromiseResolvedWithSession(promiseId, sessionId->EmeId());
1384 std::vector<std::string> dummyKeyId;
1385 std::vector<uint32_t> dummyStatus;
1386 dummyKeyId.push_back("");
1387 dummyStatus.push_back(static_cast<uint32_t>(KeyStatus::KEY_STATUS_INTERNAL_ERROR));
1388 callback_->OnSessionKeysChange(sessionId->EmeId(), dummyKeyId, dummyStatus, false, true);
1389 return;
1390 }
1391
1392 if (info->KeyType() != static_cast<int32_t>(MediaKeyType::MEDIA_KEY_TYPE_OFFLINE)) {
1393 return;
1394 }
1395
1396 if (keySystemType_ == KeySystemType::WIDEVINE) {
1397 int32_t iRet = CreateMediaKeySession(sessionId->EmeId());
1398 if (iRet != 0) {
1399 WVLOG_E("[DRM]OH_MediaKeySystem_CreateMediaKeySession failed.");
1400 return;
1401 }
1402 }
1403 MediaKeySession* drmKeySession = GetMediaKeySession(sessionId->EmeId());
1404 callback_->UpdateMediaKeySessionInfoMap(drmKeySession, info);
1405 if (drmKeySession != nullptr) {
1406 Drm_ErrCode ret =
1407 OH_MediaKeySession_RestoreOfflineMediaKeys(drmKeySession, sessionId->KeySetId(), sessionId->KeySetIdLen());
1408 WVLOG_I("[DRM]DrmAdapterImpl::OH_MediaKeySession_RestoreOfflineMediaKeys, ret = %{public}d", ret);
1409 if (ret != DRM_ERR_OK) {
1410 ClearPersistentSessionInfoForLoadFail(sessionId);
1411 return;
1412 }
1413 if (keySystemType_ == KeySystemType::WISEPLAY) {
1414 callback_->OnMediaLicenseReady(true);
1415 }
1416 callback_->OnPromiseResolvedWithSession(promiseId, sessionId->EmeId());
1417 }
1418 WVLOG_I("[DRM]DrmAdapterImpl::LoadSessionWithLoadedStorage.");
1419 }
1420
1421 // remove && release
SetKeyType(std::shared_ptr<SessionId> sessionId,int32_t keyType)1422 void DrmAdapterImpl::SetKeyType(std::shared_ptr<SessionId> sessionId, int32_t keyType)
1423 {
1424 WVLOG_I("[DRM]DrmAdapterImpl::SetKeyType.");
1425 std::shared_ptr<SessionInfo> info = GetSessionInfo(sessionId);
1426 if (info == nullptr) {
1427 return;
1428 }
1429 info->SetKeyType(keyType);
1430
1431 if (info->GetSessionId() != nullptr && info->GetSessionId()->KeySetId() != nullptr) {
1432 std::vector<uint8_t> keySetIdVec;
1433 keySetIdVec.insert(keySetIdVec.begin(), info->GetSessionId()->KeySetId(),
1434 info->GetSessionId()->KeySetId() + info->GetSessionId()->KeySetIdLen());
1435 if (callback_) {
1436 WVLOG_I("[DRM]DrmAdapterImpl::OnStorageSaveInfo.");
1437 callback_->OnStorageSaveInfo(keySetIdVec, info->MimeType(), sessionId->EmeId(), keyType);
1438 }
1439 }
1440 }
1441
1442 // update
SetKeySetId(std::shared_ptr<SessionId> sessionId,uint8_t * mediaKeyId,int32_t mediaKeyIdLen)1443 void DrmAdapterImpl::SetKeySetId(std::shared_ptr<SessionId> sessionId, uint8_t* mediaKeyId, int32_t mediaKeyIdLen)
1444 {
1445 WVLOG_I("[DRM]DrmAdapterImpl::SetKeySetId.");
1446 if (sessionId == nullptr) {
1447 HandleKeyUpdatedCallback(updateSessionPromiseId_, false);
1448 return;
1449 }
1450 sessionId->SetKeySetId(mediaKeyId, mediaKeyIdLen);
1451 if (callback_) {
1452 std::shared_ptr<SessionInfo> info = GetSessionInfo(sessionId);
1453 if (info) {
1454 std::vector<uint8_t> keySetIdVec;
1455 keySetIdVec.insert(keySetIdVec.begin(), info->GetSessionId()->KeySetId(),
1456 info->GetSessionId()->KeySetId() + info->GetSessionId()->KeySetIdLen());
1457 callback_->OnStorageSaveInfo(keySetIdVec, info->MimeType(), sessionId->EmeId(), info->KeyType());
1458 }
1459 } else {
1460 HandleKeyUpdatedCallback(updateSessionPromiseId_, false);
1461 }
1462 }
1463
ClearPersistentSessionInfoFroKeyRelease(std::shared_ptr<SessionId> sessionId)1464 void DrmAdapterImpl::ClearPersistentSessionInfoFroKeyRelease(std::shared_ptr<SessionId> sessionId)
1465 {
1466 WVLOG_I("[DRM]DrmAdapterImpl::ClearPersistentSessionInfoFroKeyRelease.");
1467 if (sessionId != nullptr) {
1468 sessionId->SetKeySetId(nullptr, 0);
1469 if (callback_) {
1470 WVLOG_I("[DRM]OnStorageClearInfoForKeyRelease.");
1471 callback_->OnStorageClearInfoForKeyRelease(sessionId->EmeId());
1472 }
1473 }
1474 }
1475
ClearPersistentSessionInfoForLoadFail(std::shared_ptr<SessionId> sessionId)1476 void DrmAdapterImpl::ClearPersistentSessionInfoForLoadFail(std::shared_ptr<SessionId> sessionId)
1477 {
1478 WVLOG_I("[DRM]DrmAdapterImpl::ClearPersistentSessionInfoFroKeyRelease.");
1479 if (sessionId != nullptr) {
1480 sessionId->SetKeySetId(nullptr, 0);
1481 if (callback_) {
1482 WVLOG_I("[DRM]OnStorageClearInfoForLoadFail.");
1483 callback_->OnStorageClearInfoForLoadFail(sessionId->EmeId());
1484 }
1485 }
1486 }
1487
HandleKeyUpdatedCallback(uint32_t promiseId,bool result)1488 void DrmAdapterImpl::HandleKeyUpdatedCallback(uint32_t promiseId, bool result)
1489 {
1490 WVLOG_I("[DRM]DrmAdapterImpl::HandleKeyUpdatedCallback, result: %{public}d", result);
1491 if (callback_) {
1492 if (!result) {
1493 callback_->OnPromiseRejected(promiseId, "failed to update key after response accepted");
1494 return;
1495 }
1496 callback_->OnPromiseResolved(promiseId);
1497 }
1498 }
1499 } // namespace OHOS::NWeb