diff --git a/BUILD.gn b/BUILD.gn new file mode 100644 index 00000000..a92461a5 --- /dev/null +++ b/BUILD.gn @@ -0,0 +1,9 @@ +#import("//build/config/ohos/rules.gni") +import("//build/ohos.gni") +group("drm_framework") { + deps = [ + "//foundation/multimedia/drm_framework/frameworks/native:drm_framework", + "//foundation/multimedia/drm_framework/interfaces/kits/js/drm_napi:drm_napi", + "//foundation/multimedia/drm_framework/services:drm_service", + ] +} diff --git a/bundle.json b/bundle.json new file mode 100644 index 00000000..1082ff94 --- /dev/null +++ b/bundle.json @@ -0,0 +1,110 @@ +{ + "name": "@ohos/drm_framework", + "description": "DRM standard", + "version": "3.1", + "license": "Apache License 2.0", + "publishAs": "code-segment", + "segment": { + "destPath": "foundation/multimedia/drm_framework" + }, + "dirs": {}, + "scripts": {}, + "component": { + "name": "multimedia_drm_framework", + "subsystem": "multimedia", + "syscap": ["SystemCapability.Multimedia.Drm.Core"], + "features": [], + "adapted_system_type": [ "standard" ], + "rom": "", + "ram": "", + "hisysevent_config": [ ], + "deps": { + "components": [ + "safwk", + "window_manager", + "napi", + "samgr", + "hitrace", + "ipc", + "graphic_2d", + "hisysevent", + "c_utils", + "hilog", + "zlib", + "access_token", + "libhilog", + "ipc_core", + "media_client", + "system_ability_fwk", + "samgr_proxy", + "libaccesstoken_sdk", + "ipc_single", + "common", + "hicollie", + "hdf_core", + "eventhandler", + "bundle_framework", + "ability_base" + ], + "third_party": [ + "glib", + "gstreamer", + "libffi", + "ffmpeg", + "libxml2", + "bounds_checking_function" + ] + }, + "build": { + "group_type": { + "base_group": [ + ], + "fwk_group": [ + "//foundation/multimedia/drm_framework/frameworks/native:drm_framework", + "//foundation/multimedia/drm_framework/interfaces/kits/js/drm_napi:drm_napi", + "//foundation/multimedia/drm_framework/interfaces/kits/js/drm_napi:drm_js" + ], + "service_group": [ + "//foundation/multimedia/drm_framework/sa_profile:drm_service_sa_profile", + "//foundation/multimedia/drm_framework/services/etc:drm_service.rc", + "//foundation/multimedia/drm_framework/services/drm_service:drm_service" + ] + }, + + "inner_kits": [ + { + "type": "none", + "name": "//foundation/multimedia/drm_framework/frameworks/native:drm_framework", + "header": { + "header_base": [ + "foundation/multimedia/drm_framework/interfaces/inner_api/native/drm" + ], + "header_files": [ + "media_key_system_impl.h", + "key_session_impl.h", + "media_decrypt_module_impl.h" + ] + } + }, + { + "type": "none", + "name": "//foundation/multimedia/drm_framework/interfaces/kits/js/drm_napi:drm_napi", + "header": { + "header_base": [ + "//foundation/multimedia/drm_framework/interfaces/kits/js/drm_napi/include" + ], + "header_files": [ + "drm_napi.h", + "key_session_napi.h", + "media_key_system_napi.h", + "media_key_system_callback_napi.h", + "native_module_ohos_drm.h" + ] + } + } + ], + "test": [ + ] + } + } +} \ No newline at end of file diff --git a/frameworks/js/drm_napi/drm_enum_napi.cpp b/frameworks/js/drm_napi/drm_enum_napi.cpp new file mode 100644 index 00000000..a5137dc3 --- /dev/null +++ b/frameworks/js/drm_napi/drm_enum_napi.cpp @@ -0,0 +1,215 @@ +/* + * Copyright (C) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#include +#include "drm_enum_napi.h" + +namespace OHOS { +namespace DrmStandard { + +struct JsEnumInt { + std::string_view enumName; + int32_t enumInt; +}; + +struct JsEnumString { + std::string_view enumName; + std::string_view enumString; +}; + +static const std::vector g_listenerType = { + { "LISTENER_DRM_EVENT", IKeySessionService::ListenerType::LISTENER_DRM_EVENT }, + { "LISTENER_PROVISION_REQUIRED", IKeySessionService::ListenerType::LISTENER_PROVISION_REQUIRED }, + { "LISTENER_KEY_NEEDED", IKeySessionService::ListenerType::LISTENER_KEY_NEEDED }, + { "LISTENER_KEY_EXPIRED", IKeySessionService::ListenerType::LISTENER_KEY_EXPIRED }, + { "LISTENER_VENDOR_DEFINED", IKeySessionService::ListenerType::LISTENER_VENDOR_DEFINED }, + { "LISTENER_KEYSESSION_RECLAIMED", IKeySessionService::ListenerType::LISTENER_KEYSESSION_RECLAIMED }, + { "LISTENER_EXPIRATION_UPDATE", IKeySessionService::ListenerType::LISTENER_EXPIRATION_UPDATE }, + { "LISTENER_KEY_CHANGE", IKeySessionService::ListenerType::LISTENER_KEY_CHANGE }, + { "LISTENER_KEYSESSION_LOSE", IKeySessionService::ListenerType::LISTENER_KEYSESSION_LOSE }, +}; + +static const std::vector g_hdcpLevel = { + { "HDCP_UNKNOWN", IMediaKeySystemService::HdcpLevel::HDCP_UNKNOWN }, + { "HDCP_NONE", IMediaKeySystemService::HdcpLevel::HDCP_NONE }, + { "HDCP_V1", IMediaKeySystemService::HdcpLevel::HDCP_V1 }, + { "HDCP_V2", IMediaKeySystemService::HdcpLevel::HDCP_V2 }, + { "HDCP_V2_1", IMediaKeySystemService::HdcpLevel::HDCP_V2_1 }, + { "HDCP_V2_2", IMediaKeySystemService::HdcpLevel::HDCP_V2_2 }, + { "HDCP_V2_3", IMediaKeySystemService::HdcpLevel::HDCP_V2_3 }, + { "HDCP_NO_OUTPUT", IMediaKeySystemService::HdcpLevel::HDCP_NO_OUTPUT }, +}; + +static const std::vector g_configType = { + { "CONFIGTYPE_DEVICEPROPERTY", IMediaKeySystemService::ConfigType::CONFIGTYPE_DEVICEPROPERTY }, + { "CONFIGTYPE_KEYSESSION", IMediaKeySystemService::ConfigType::CONFIGTYPE_KEYSESSION }, + { "CONFIGTYPE_OUTPUTPROTECTTYPE", IMediaKeySystemService::ConfigType::CONFIGTYPE_OUTPUTPROTECTTYPE }, +}; + +static const std::vector g_keyType = { + { "KEYTYPE_OFFLINE", IKeySessionService::KeyType::KEYTYPE_OFFLINE }, + { "KEYTYPE_ONLINE", IKeySessionService::KeyType::KEYTYPE_ONLINE }, +}; + +static const std::vector g_offlineKeyState = { + { "OFFLINEKEYSTATE_UNKNOWN", IKeySessionService::OfflineKeyState::OFFLINEKEYSTATE_UNKNOWN }, + { "OFFLINEKEYSTATE_USABLE", IKeySessionService::OfflineKeyState::OFFLINEKEYSTATE_USABLE }, + { "OFFLINEKEYSTATE_INACTIVE", IKeySessionService::OfflineKeyState::OFFLINEKEYSTATE_INACTIVE }, +}; + +static const std::vector g_requestType = { + { "REQUEST_TYPE_UNKNOWN", IKeySessionService::RequestType::REQUEST_TYPE_UNKNOWN }, + { "REQUEST_TYPE_INITIAL", IKeySessionService::RequestType::REQUEST_TYPE_INITIAL }, + { "REQUEST_TYPE_RENEWAL", IKeySessionService::RequestType::REQUEST_TYPE_RENEWAL }, + { "REQUEST_TYPE_RELEASE", IKeySessionService::RequestType::REQUEST_TYPE_RELEASE }, + { "REQUEST_TYPE_NONE", IKeySessionService::RequestType::REQUEST_TYPE_NONE }, + { "REQUEST_TYPE_UPDATE", IKeySessionService::RequestType::REQUEST_TYPE_UPDATE }, + { "REQUEST_TYPE_DOWNLOADCERT", IKeySessionService::RequestType::REQUEST_TYPE_DOWNLOADCERT }, +}; + +static const std::vector g_securityLevel = { + { "SECURITY_LEVEL_UNKNOWN", IKeySessionService::SecurityLevel::SECURITY_LEVEL_UNKNOWN }, + { "SECURITY_LEVEL_SW_CRYPTO", IKeySessionService::SecurityLevel::SECURITY_LEVEL_SW_CRYPTO }, + { "SECURITY_LEVEL_SW_DECODE", IKeySessionService::SecurityLevel::SECURITY_LEVEL_SW_DECODE }, + { "SECURITY_LEVEL_HW_CRYPTO", IKeySessionService::SecurityLevel::SECURITY_LEVEL_HW_CRYPTO }, + { "SECURITY_LEVEL_HW_DECODE", IKeySessionService::SecurityLevel::SECURITY_LEVEL_HW_DECODE }, + { "SECURITY_LEVEL_HW_ALL", IKeySessionService::SecurityLevel::SECURITY_LEVEL_HW_ALL }, + { "SECURITY_LEVEL_MAX", IKeySessionService::SecurityLevel::SECURITY_LEVEL_MAX }, +}; + +static const std::vector g_cryptAlgorithmType = { + { "ALGTYPE_UNENCRYPTED", IMediaDecryptModuleService::CryptAlgorithmType::ALGTYPE_UNENCRYPTED }, + { "ALGTYPE_AES_CTR", IMediaDecryptModuleService::CryptAlgorithmType::ALGTYPE_AES_CTR }, + { "ALGTYPE_AES_WV", IMediaDecryptModuleService::CryptAlgorithmType::ALGTYPE_AES_WV }, + { "ALGTYPE_AES_CBC", IMediaDecryptModuleService::CryptAlgorithmType::ALGTYPE_AES_CBC }, + { "ALGTYPE_SM4_CBC", IMediaDecryptModuleService::CryptAlgorithmType::ALGTYPE_SM4_CBC }, +}; + +static const std::vector g_propertyName = { + { "PROPERTY_DEVICE_VENDOR", "vendor" }, + { "PROPERTY_DEVICE_VERSION", "version" }, + { "PROPERTY_DEVICE_DESCRIPTION", "description" }, + { "PROPERTY_DEVICE_ALGORITHMS", "algorithms" }, + { "PROPERTY_DEVICE_UNIQUE_ID", "deviceUniqueId" }, + { "PROPERTY_SESSION_MAX", "maxSessionNum" }, + { "PROPERTY_SESSION_CURRENT", "currentSessionNum" }, + { "PROPERTY_OUTPUT_HDCP_MAX", "maxHDCPLevel" }, + { "PROPERTY_OUTPUT_HDCP_CURRENT", "currentHDCPLevel" }, +}; + +static const std::vector g_metricsName = { + { "METRICS_SESSION_CURRENT", "currentSessionNum" }, + { "METRICS_DEVICE_VERSION", "version" }, + { "METRICS_DECRYPT_NUMBER", "decryptNumber" }, + { "METRICS_ERRO_DECRYPT_NUMBER", "errorDecryptNumber" }, +}; + +static const std::map&> g_intEnumClassMap = { + { "ListenerType", g_listenerType }, + { "HdcpLevel", g_hdcpLevel }, + { "ConfigType", g_configType }, + { "KeyType", g_keyType }, + { "OfflineKeyState", g_offlineKeyState }, + { "RequestType", g_requestType }, + { "SecurityLevel", g_securityLevel }, + { "CryptAlgorithmType", g_cryptAlgorithmType }, +}; + +static const std::map&> g_stringEnumClassMap = { + { "PropertyName", g_propertyName }, + { "MetricsName", g_metricsName }, +}; + +napi_value DrmEnumNapi::JsEnumIntInit(napi_env env, napi_value exports) +{ + for (auto it = g_intEnumClassMap.begin(); it != g_intEnumClassMap.end(); it++) { + auto &enumClassName = it->first; + auto &enumItemVec = it->second; + int32_t vecSize = enumItemVec.size(); + std::vector value; + value.resize(vecSize); + for (int32_t index = 0; index < vecSize; ++index) { + napi_create_int32(env, enumItemVec[index].enumInt, &value[index]); + } + + std::vector property; + property.resize(vecSize); + for (int32_t index = 0; index < vecSize; ++index) { + property[index] = napi_property_descriptor DECLARE_NAPI_STATIC_PROPERTY( + enumItemVec[index].enumName.data(), value[index]); + } + + auto constructor = [](napi_env env, napi_callback_info info) { + napi_value jsThis = nullptr; + napi_get_cb_info(env, info, nullptr, nullptr, &jsThis, nullptr); + return jsThis; + }; + + napi_value result = nullptr; + napi_status status = napi_define_class(env, enumClassName.data(), NAPI_AUTO_LENGTH, constructor, + nullptr, property.size(), property.data(), &result); + CHECK_AND_RETURN_RET_LOG(status == napi_ok, nullptr, "Failed to define enum"); + + status = napi_set_named_property(env, exports, enumClassName.data(), result); + CHECK_AND_RETURN_RET_LOG(status == napi_ok, nullptr, "Failed to set result"); + } + return exports; +} + +napi_value DrmEnumNapi::JsEnumStringInit(napi_env env, napi_value exports) +{ + for (auto it = g_stringEnumClassMap.begin(); it != g_stringEnumClassMap.end(); it++) { + auto &enumClassName = it->first; + auto &enumItemVec = it->second; + int32_t vecSize = enumItemVec.size(); + std::vector value; + value.resize(vecSize); + for (int32_t index = 0; index < vecSize; ++index) { + napi_create_string_utf8(env, enumItemVec[index].enumString.data(), NAPI_AUTO_LENGTH, &value[index]); + } + + std::vector property; + property.resize(vecSize); + for (int32_t index = 0; index < vecSize; ++index) { + property[index] = napi_property_descriptor DECLARE_NAPI_STATIC_PROPERTY( + enumItemVec[index].enumName.data(), value[index]); + } + + auto constructor = [](napi_env env, napi_callback_info info) { + napi_value jsThis = nullptr; + napi_get_cb_info(env, info, nullptr, nullptr, &jsThis, nullptr); + return jsThis; + }; + + napi_value result = nullptr; + napi_status status = napi_define_class(env, enumClassName.data(), NAPI_AUTO_LENGTH, constructor, + nullptr, property.size(), property.data(), &result); + CHECK_AND_RETURN_RET_LOG(status == napi_ok, nullptr, "Failed to define enum"); + + status = napi_set_named_property(env, exports, enumClassName.data(), result); + CHECK_AND_RETURN_RET_LOG(status == napi_ok, nullptr, "Failed to set result"); + } + return exports; +} + +napi_value DrmEnumNapi::Init(napi_env env, napi_value exports) +{ + JsEnumIntInit(env, exports); + JsEnumStringInit(env, exports); + return exports; +} +} // namespace drm +} // namespace OHOS \ No newline at end of file diff --git a/frameworks/js/drm_napi/drm_napi.cpp b/frameworks/js/drm_napi/drm_napi.cpp new file mode 100644 index 00000000..4f4bb09d --- /dev/null +++ b/frameworks/js/drm_napi/drm_napi.cpp @@ -0,0 +1,126 @@ +/* + * Copyright (c) 2023-2024 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "drm_napi.h" +#include "ipc_skeleton.h" +namespace OHOS { +namespace DrmStandard { + +thread_local napi_ref DrmNapi::sConstructor_ = nullptr; + +DrmNapi::DrmNapi() : env_(nullptr), wrapper_(nullptr) +{ + DRM_DEBUG_LOG("0x%{public}06" PRIXPTR " Instances create", FAKE_POINTER(this)); +} + +DrmNapi::~DrmNapi() +{ + DRM_DEBUG_LOG("0x%{public}06" PRIXPTR " Instances destroy", FAKE_POINTER(this)); + if (wrapper_ != nullptr) { + napi_delete_reference(env_, wrapper_); + } +} + +napi_value DrmNapi::Init(napi_env env, napi_value exports) +{ + DRM_INFO_LOG("DrmNapi Init enter."); + napi_property_descriptor drmproperty[] = { + DECLARE_NAPI_FUNCTION("getMediaKeySystemTest", CreateMediaKeySystemInstance), + }; + DRM_DEBUG_LOG("DrmNapi enter properties"); + napi_property_descriptor drm_static_properties[] = { + DECLARE_NAPI_STATIC_FUNCTION("getMediaKeySystem", CreateMediaKeySystemInstance), + DECLARE_NAPI_STATIC_FUNCTION("isMediaKeySystemSupported", IsMediaKeySystemSupported), + }; + DRM_DEBUG_LOG("DrmNapi enter napi_define_class"); + napi_value constructor = nullptr; + napi_status status = napi_define_class(env, DRM_NAPI_CLASS_NAME, NAPI_AUTO_LENGTH, DrmNapiConstructor, nullptr, + sizeof(drmproperty) / sizeof(drmproperty[0]), drmproperty, &constructor); + + CHECK_AND_RETURN_RET_LOG(status == napi_ok, nullptr, "Failed to define DrmNapi class"); + + status = napi_create_reference(env, constructor, 1, &sConstructor_); + CHECK_AND_RETURN_RET_LOG(status == napi_ok, nullptr, "Failed to create reference of DrmNapi constructor"); + + status = napi_set_named_property(env, exports, DRM_NAPI_CLASS_NAME, constructor); + CHECK_AND_RETURN_RET_LOG(status == napi_ok, nullptr, "Failed to set DrmNapi constructor"); + + status = napi_define_properties(env, exports, sizeof(drm_static_properties) / sizeof(drm_static_properties[0]), drm_static_properties); + CHECK_AND_RETURN_RET_LOG(status == napi_ok, nullptr, "Failed to define static DrmNapi function"); + + DRM_INFO_LOG("DrmNapi Init success"); + return exports; +} + +napi_value DrmNapi::DrmNapiConstructor(napi_env env, napi_callback_info info) +{ + DRM_INFO_LOG("DrmNapi DrmNapiConstructor enter."); + napi_status status; + napi_value result = nullptr; + napi_value thisVar = nullptr; + + napi_get_undefined(env, &result); + DRM_NAPI_GET_JS_OBJ_WITH_ZERO_ARGS(env, info, status, thisVar); + + if (status == napi_ok && thisVar != nullptr) { + std::unique_ptr obj = std::make_unique(); + if (obj != nullptr) { + obj->env_ = env; + status = napi_wrap(env, thisVar, reinterpret_cast(obj.get()), + DrmNapi::DrmNapiDestructor, nullptr, nullptr); + if (status == napi_ok) { + obj.release(); + return thisVar; + } else { + DRM_ERR_LOG("DrmNapiDestructor Failure wrapping js to native napi"); + } + } + } + DRM_ERR_LOG("DrmNapiDestructor call Failed!"); + DRM_INFO_LOG("DrmNapi DrmNapiConstructor exit."); + return result; +} + +void DrmNapi::DrmNapiDestructor(napi_env env, void *nativeObject, void *finalize) +{ + DRM_INFO_LOG("DrmNapi DrmNapiDestructor enter."); + DrmNapi* drmNapi = reinterpret_cast(nativeObject); + if (drmNapi != nullptr) { + drmNapi->~DrmNapi(); + } + DRM_INFO_LOG("DrmNapi DrmNapiDestructor exit."); +} + +napi_value DrmNapi::IsMediaKeySystemSupported(napi_env env, napi_callback_info info) +{ + DRM_INFO_LOG("DrmNapi IsMediaKeySystemSupported enter."); + napi_value result = nullptr; + result = MediaKeySystemNapi::IsMediaKeySystemSupported(env, info); + DRM_ERR_LOG("DrmNapi IsMediaKeySystemSupported exit."); + return result; +} + +napi_value DrmNapi::CreateMediaKeySystemInstance(napi_env env, napi_callback_info info) +{ + DRM_INFO_LOG("DrmNapi CreateMediaKeySystemInstance enter."); + napi_value result = nullptr; + result = MediaKeySystemNapi::CreateMediaKeySystemInstance(env,info); + DRM_INFO_LOG("DrmNapi CreateMediaKeySystemInstance exit."); + + return result; +} + +} //namespace OHOS +} //namespace DrmStandard diff --git a/frameworks/js/drm_napi/key_session_callback_napi.cpp b/frameworks/js/drm_napi/key_session_callback_napi.cpp new file mode 100644 index 00000000..055f468f --- /dev/null +++ b/frameworks/js/drm_napi/key_session_callback_napi.cpp @@ -0,0 +1,106 @@ +/* + * Copyright (c) 2023-2024 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#include "key_session_callback_napi.h" +#include "drm_log.h" +#include "drm_error_code.h" + +namespace OHOS { +namespace DrmStandard { + +KeySessionCallbackNapi::KeySessionCallbackNapi() {} + +KeySessionCallbackNapi::~KeySessionCallbackNapi() {} + +void KeySessionCallbackNapi::SetCallbackReference(const std::string eventType, sptr callbackPair) +{ + DRM_INFO_LOG("KeySessionCallbackNapi SetCallbackReference"); + std::lock_guard lock(mutex_); + callbackMap_[eventType] = callbackPair; +} + +void KeySessionCallbackNapi::ClearCallbackReference(const std::string eventType) +{ + DRM_INFO_LOG("KeySessionCallbackNapi ClearCallbackReference"); + std::lock_guard lock(mutex_); + if (callbackMap_.find(eventType) != callbackMap_.end()) { + callbackMap_.erase(eventType); + DRM_INFO_LOG("KeySessionCallbackNapi ClearCallbackReference %{public}s", eventType.c_str()); + } else { + DRM_WARNING_LOG("ClearCallbackReference failed, no this event: %{public}s", eventType.c_str()); + } +} + +void KeySessionCallbackNapi::OnKeySessionKeyExpired(const std::string eventType, const KeyStatus status) +{ + DRM_INFO_LOG("KeySessionCallbackNapi OnKeySessionKeyExpired"); + napi_value result; + napi_value jsCallback = nullptr; + napi_value retVal; + napi_status state; + + CHECK_AND_RETURN_LOG(callbackMap_.find(eventType) != callbackMap_.end(), + "Napi OnKeySessionKeyExpired, not register this callback"); + sptr item = callbackMap_[eventType]; + + CHECK_AND_RETURN_LOG(item != nullptr, "Napi OnKeySessionKeyExpired, sptr callbackPair is nullptr"); + napi_env env = item->GetEnv(); + napi_ref callbackRef = item->GetCallback(); + + napi_get_undefined(env, &result); + napi_create_object(env, &result); + + int32_t keyStatus = static_cast(status); + napi_value args[1] = {nullptr}; + (void)napi_create_int32(env, keyStatus, &args[0]); + DRM_INFO_LOG("Napi OnKeySessionKeyExpired, Status: %{public}d", keyStatus); + + napi_get_reference_value(env, callbackRef, &jsCallback); + state = napi_call_function(env, nullptr, jsCallback, ARGS_ONE, args, &retVal); + CHECK_AND_RETURN_LOG(state == napi_ok, + "%{public}s failed to napi_call_function", eventType.c_str()); +} + +void KeySessionCallbackNapi::OnKeySessionReclaimed(const std::string eventType, const SessionStatus status) +{ + DRM_INFO_LOG("KeySessionCallbackNapi OnKeySessionReclaimed"); + napi_value result; + napi_value jsCallback = nullptr; + napi_value retVal; + napi_status state; + + CHECK_AND_RETURN_LOG(callbackMap_.find(eventType) != callbackMap_.end(), + "Napi OnKeySessionReclaimed, not register this callback"); + sptr item = callbackMap_[eventType]; + + CHECK_AND_RETURN_LOG(item != nullptr, "Napi OnKeySessionReclaimed, sptr callbackPair is nullptr"); + napi_env env = item->GetEnv(); + napi_ref callbackRef = item->GetCallback(); + + napi_get_undefined(env, &result); + napi_create_object(env, &result); + + int32_t sessionStatus = static_cast(status); + napi_value args[1] = {nullptr}; + (void)napi_create_int32(env, sessionStatus, &args[0]); + DRM_INFO_LOG("Napi OnKeySessionReclaimed, Status: %{public}d", sessionStatus); + + napi_get_reference_value(env, callbackRef, &jsCallback); + state = napi_call_function(env, nullptr, jsCallback, ARGS_ONE, args, &retVal); + CHECK_AND_RETURN_LOG(state == napi_ok, + "%{public}s failed to napi_call_function", eventType.c_str()); +} + +} +} \ No newline at end of file diff --git a/frameworks/js/drm_napi/key_session_napi.cpp b/frameworks/js/drm_napi/key_session_napi.cpp new file mode 100644 index 00000000..69f5a74a --- /dev/null +++ b/frameworks/js/drm_napi/key_session_napi.cpp @@ -0,0 +1,889 @@ +/* + * Copyright (c) 2023-2024 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#include "key_session_napi.h" +#include "media_decrypt_module_napi.h" +#include "key_session_impl.h" +#include "media_key_system_impl.h" +#include "ipc_skeleton.h" + +namespace OHOS { +namespace DrmStandard { + +thread_local napi_ref KeySessionNapi::sConstructor_ = nullptr; +thread_local sptr KeySessionNapi::sKeySessionImpl_ = nullptr; + +KeySessionNapi::KeySessionNapi() : env_(nullptr), wrapper_(nullptr) +{ + DRM_INFO_LOG("0x%{public}06" PRIXPTR " Instances create", FAKE_POINTER(this)); +} + +KeySessionNapi::~KeySessionNapi() +{ + DRM_INFO_LOG("KeySessionNapi::~KeySessionNapi Init enter."); + DRM_DEBUG_LOG("0x%{public}06" PRIXPTR " Instances destroy", FAKE_POINTER(this)); + if (wrapper_ != nullptr) { + napi_delete_reference(env_, wrapper_); + } + if (keySessionImpl_) { + keySessionImpl_ = nullptr; + } + DRM_INFO_LOG("KeySessionNapi::~KeySessionNapi Init exit."); +} + +napi_value KeySessionNapi::Init(napi_env env, napi_value exports) +{ + DRM_INFO_LOG("KeySessionNapi::KeySessionNapi Init enter."); + napi_status status; + napi_value ctorObj; + int32_t refCount = 1; + + napi_property_descriptor session_props[] = { + DECLARE_NAPI_FUNCTION("release", Release), + DECLARE_NAPI_FUNCTION("generateLicenseRequest", GenerateLicenseRequest), + DECLARE_NAPI_FUNCTION("processLicenseResponse", ProcessLicenseResponse), + DECLARE_NAPI_FUNCTION("generateOfflineReleaseRequest", GenerateOfflineReleaseRequest), + DECLARE_NAPI_FUNCTION("processOfflineReleaseResponse", ProcessOfflineReleaseResponse), + DECLARE_NAPI_FUNCTION("checkLicenseStatus", CheckLicenseStatus), + DECLARE_NAPI_FUNCTION("restoreOfflineKeys", RestoreOfflineKeys), + DECLARE_NAPI_FUNCTION("removeOfflineKeys", RemoveOfflineKeys), + DECLARE_NAPI_FUNCTION("getOfflineKeyIds", GetOfflineKeyIds), + DECLARE_NAPI_FUNCTION("removeLicenses", RemoveLicenses), + DECLARE_NAPI_FUNCTION("getDecryptModule", GetDecryptModule), + DECLARE_NAPI_FUNCTION("addEventListener", AddEventListener), + DECLARE_NAPI_FUNCTION("deleteEventListener", DeleteEventListener), + DECLARE_NAPI_FUNCTION("getOfflineKeyState", GetOfflineKeyState), + DECLARE_NAPI_FUNCTION("on", SetEventCallback), + DECLARE_NAPI_FUNCTION("off", UnsetEventCallback), + }; + status = napi_define_class(env, KEY_SESSION_NAPI_CLASS_NAME, NAPI_AUTO_LENGTH, + KeySessionNapiConstructor, nullptr, + sizeof(session_props) / sizeof(session_props[PARAM0]), + session_props, &ctorObj); + if (status == napi_ok) { + status = napi_create_reference(env, ctorObj, refCount, &sConstructor_); + if (status == napi_ok) { + status = napi_set_named_property(env, exports, KEY_SESSION_NAPI_CLASS_NAME, ctorObj); + if (status == napi_ok) { + DRM_DEBUG_LOG("KeySessionNapi return exports"); + return exports; + } + } + } + DRM_INFO_LOG("KeySessionNapi::KeySessionNapi Init call Failed!"); + return nullptr; +} + +napi_value KeySessionNapi::KeySessionNapiConstructor(napi_env env, napi_callback_info info) +{ + DRM_INFO_LOG("KeySessionNapi::KeySessionNapiConstructor enter."); + + napi_status status; + napi_value result = nullptr; + napi_value thisVar = nullptr; + + napi_get_undefined(env, &result); + DRM_NAPI_GET_JS_OBJ_WITH_ZERO_ARGS(env, info, status, thisVar); + + if (status == napi_ok && thisVar != nullptr) { + std::unique_ptr obj = std::make_unique(); + if (obj != nullptr) { + obj->env_ = env; + if (sKeySessionImpl_ == nullptr) { + DRM_ERR_LOG("sKeySessionImpl_ is null"); + return result; + } + obj->keySessionImpl_ = sKeySessionImpl_; + obj->keySessionCallbackNapi_ = new KeySessionCallbackNapi(); + obj->keySessionImpl_->SetKeySessionCallback(obj->keySessionCallbackNapi_); + status = napi_wrap(env, thisVar, reinterpret_cast(obj.get()), + KeySessionNapi::KeySessionNapiDestructor, nullptr, nullptr); + if (status == napi_ok) { + obj.release(); + const std::string propertyName = "KeySessionNative"; + SetKeySessionNativeProperty(env, thisVar, propertyName, sKeySessionImpl_); + return thisVar; + } else { + DRM_ERR_LOG("KeySessionNapi Failure wrapping js to native napi"); + } + } + } + DRM_INFO_LOG("KeySessionNapi::KeySessionNapiConstructor faild, exit."); + return result; +} + +void KeySessionNapi::KeySessionNapiDestructor(napi_env env, void *nativeObject, void *finalize) +{ + DRM_INFO_LOG("KeySessionNapi::KeySessionNapiDestructor enter."); + KeySessionNapi* keySessionNapiObj = reinterpret_cast(nativeObject); + if (keySessionNapiObj != nullptr) { + keySessionNapiObj->~KeySessionNapi(); + } + DRM_INFO_LOG("KeySessionNapi::KeySessionNapiDestructor exit."); +} + +bool KeySessionNapi::SetKeySessionNativeProperty(napi_env env, napi_value obj, const std::string &name, sptr keySessionImpl) +{ + DRM_INFO_LOG("KeySessionNapi::SetKeySessionNativeProperty enter."); + CHECK_AND_RETURN_RET_LOG(obj != nullptr, false, "obj is nullptr"); + + napi_value keySessionImplNative = nullptr; + int64_t nativePointer = reinterpret_cast(keySessionImpl.GetRefPtr()); + DRM_DEBUG_LOG("SetKeySessionNativeProperty get nativePointer:%{public}lld", nativePointer); + napi_status status = napi_create_int64(env, nativePointer, &keySessionImplNative); + CHECK_AND_RETURN_RET_LOG(status == napi_ok, false, "create int failed"); + + napi_value nameStr = nullptr; + status = napi_create_string_utf8(env, name.c_str(), NAPI_AUTO_LENGTH, &nameStr); + CHECK_AND_RETURN_RET_LOG(status == napi_ok, false, "create string failed"); + + status = napi_set_property(env, obj, nameStr, keySessionImplNative); + CHECK_AND_RETURN_RET_LOG(status == napi_ok, false, "set property failed"); + + DRM_INFO_LOG("KeySessionNapi::SetKeySessionNativeProperty exit."); + return true; +} + +napi_value KeySessionNapi::CreateKeySession(napi_env env, sptr keySessionImpl) +{ + DRM_INFO_LOG("KeySessionNapi::CreateKeySession enter."); + napi_status status; + napi_value result = nullptr; + napi_value constructor; + + status = napi_get_reference_value(env, sConstructor_, &constructor); + + if (status == napi_ok) { + sKeySessionImpl_ = keySessionImpl; + if (sKeySessionImpl_ == nullptr) { + DRM_ERR_LOG("Failed to CreateKeySession instance"); + napi_get_undefined(env, &result); + return result; + } + status = napi_new_instance(env, constructor, 0, nullptr, &result); + sKeySessionImpl_ = nullptr; + if (status == napi_ok && result != nullptr) { + DRM_INFO_LOG("success to CreateKeySession napi instance"); + return result; + } else { + DRM_ERR_LOG("Failed to CreateKeySession napi instance"); + } + } + DRM_DEBUG_LOG("Failed to create CreateKeySession napi instance last"); + napi_get_undefined(env, &result); + DRM_INFO_LOG("KeySessionNapi::CreateKeySession exit."); + return result; +} + +napi_value KeySessionNapi::Release(napi_env env, napi_callback_info info) +{ + DRM_INFO_LOG("KeySessionNapi::Release enter."); + int32_t currentPid = IPCSkeleton::GetCallingPid(); + DRM_DEBUG_LOG("KeySessionNapi GetCallingPID: %{public}d", currentPid); + + napi_status status; + napi_value result = nullptr; + size_t argc = ARGS_ZERO; + napi_value argv[ARGS_ZERO]; + napi_value thisVar = nullptr; + KeySessionNapi* keySessionNapi = nullptr; + + DRM_NAPI_GET_JS_ARGS(env, info, argc, argv, thisVar); + + napi_get_undefined(env, &result); + status = napi_unwrap(env, thisVar, reinterpret_cast(&keySessionNapi)); + if (status == napi_ok && keySessionNapi != nullptr) { + keySessionNapi->keySessionImpl_->Release(); + } else { + DRM_ERR_LOG("KeySessionNapi Release call Failed!"); + } + DRM_INFO_LOG("KeySessionNapi::Release exit."); + return result; +} + +napi_value KeySessionNapi::GenerateLicenseRequest(napi_env env, napi_callback_info info) +{ + DRM_INFO_LOG("KeySessionNapi::GenerateLicenseRequest enter"); + napi_value result = nullptr; + size_t argc = ARGS_THREE; + napi_value argv[ARGS_THREE] = {0}; + napi_value thisVar = nullptr; + napi_status status; + char buffer[PATH_MAX]; + size_t length = 0; + bool isTypeArray; + void *initData = nullptr; + size_t initDataLen; + size_t offset; + napi_value arraybuffer = nullptr; + napi_typedarray_type type; + napi_value requestType; + napi_value mData; + napi_value mDefaultURL; + const char *requestTypeEnum; + IKeySessionService::DrmInfo drmInfo; + IKeySessionService::LicenseInfo licenseInfo; + KeySessionNapi* keySessionNapi = nullptr; + + napi_create_object(env, &result); + DRM_NAPI_GET_JS_ARGS(env, info, argc, argv, thisVar); + NAPI_ASSERT(env, argc <= ARGS_THREE, "requires 3 parameters maximum"); + + if (napi_get_value_string_utf8(env, argv[PARAM0], buffer, PATH_MAX, &length) != napi_ok) { + DRM_ERR_LOG("Could not able to read mimeType argument!"); + return nullptr; + } + napi_is_typedarray(env, argv[PARAM1], &isTypeArray); + if (!isTypeArray) { + DRM_ERR_LOG("argv[PARAM1] is not array!"); + return nullptr; + } + napi_get_typedarray_info(env, argv[PARAM1], &type, &initDataLen, &initData, &arraybuffer, &offset); + if (initData == nullptr) { + DRM_ERR_LOG("napi_get_typedarray_info faild!"); + return nullptr; + } + uint8_t* initDataPtr = reinterpret_cast(initData); + std::vector initDataStr(initDataPtr, initDataPtr + initDataLen); + int32_t keyType = 0; + status = napi_get_value_int32(env, argv[PARAM2], &keyType); + if (status != napi_ok) { + DRM_ERR_LOG("-Could not able to read keyType argument!"); + return nullptr; + } + drmInfo.keyType = (IKeySessionService::KeyType)keyType; + drmInfo.mimeType = std::string(buffer); + drmInfo.indexInfo = initDataStr; + status = napi_unwrap(env, thisVar, reinterpret_cast(&keySessionNapi)); + if (status == napi_ok && keySessionNapi != nullptr) { + keySessionNapi->keySessionImpl_->GenerateLicenseRequest(drmInfo, licenseInfo); + } else { + DRM_ERR_LOG("KeySessionNapi GenerateLicenseRequest call Failed!"); + } + + if (licenseInfo.requestType == 0) { + requestTypeEnum = "ALGTYPE_UNENCRYPTED"; + } else if (licenseInfo.requestType == 1) { + requestTypeEnum = "REQUEST_TYPE_INITIAL"; + } else if (licenseInfo.requestType == 2) { + requestTypeEnum = "REQUEST_TYPE_RENEWAL"; + } else if (licenseInfo.requestType == 3) { + requestTypeEnum = "REQUEST_TYPE_RELEASE"; + } else if (licenseInfo.requestType == 4) { + requestTypeEnum = "REQUEST_TYPE_NONE"; + } else if (licenseInfo.requestType == 5) { + requestTypeEnum = "REQUEST_TYPE_UPDATE"; + } else if (licenseInfo.requestType == 6) { + requestTypeEnum = "REQUEST_TYPE_DOWNLOADCERT"; + } + napi_create_string_utf8(env, requestTypeEnum, NAPI_AUTO_LENGTH, &requestType); + napi_set_named_property(env, result, "requestType", requestType); + + size_t mDataLen = licenseInfo.mData.size(); + NAPI_CALL(env, napi_create_array(env, &mData)); + for (size_t i = 0; i < mDataLen; i++) { + napi_value item; + napi_create_int32(env, licenseInfo.mData[i], &item); + napi_set_element(env, mData, i, item); + } + napi_set_named_property(env, result, "mData", mData); + + napi_create_string_utf8(env, licenseInfo.mDefaultURL.c_str(), NAPI_AUTO_LENGTH, &mDefaultURL); + napi_set_named_property(env, result, "mDefaultURL", mDefaultURL); + + DRM_INFO_LOG("KeySessionNapi::GenerateLicenseRequest exit"); + return result; +} + +napi_value KeySessionNapi::ProcessLicenseResponse(napi_env env, napi_callback_info info) +{ + DRM_INFO_LOG("KeySessionNapi::ProcessLicenseResponse enter."); + napi_value result = nullptr; + size_t argc = ARGS_ONE; + napi_value argv[ARGS_ONE] = {0}; + napi_value thisVar = nullptr; + napi_status status; + bool isTypeArray; + void *reponseData = nullptr; + size_t reponseDataLen; + size_t offset; + napi_value arraybuffer = nullptr; + napi_typedarray_type type; + std::vector keyId; + KeySessionNapi* keySessionNapi = nullptr; + + DRM_NAPI_GET_JS_ARGS(env, info, argc, argv, thisVar); + NAPI_ASSERT(env, argc <= ARGS_ONE, "requires 1 parameters maximum"); + + napi_is_typedarray(env, argv[PARAM0], &isTypeArray); + if (!isTypeArray) { + DRM_ERR_LOG("argv[PARAM0] is not array!"); + return nullptr; + } + + napi_get_typedarray_info(env, argv[PARAM0], &type, &reponseDataLen, &reponseData, &arraybuffer, &offset); + if (reponseData == nullptr) { + DRM_ERR_LOG("napi_get_typedarray_info faild!"); + return nullptr; + } + uint8_t* reponseDataPtr = reinterpret_cast(reponseData); + std::vector licenseResponse(reponseDataPtr, reponseDataPtr + reponseDataLen); + + status = napi_unwrap(env, thisVar, reinterpret_cast(&keySessionNapi)); + if (status == napi_ok && keySessionNapi != nullptr) { + int ret = keySessionNapi->keySessionImpl_->ProcessLicenseResponse(keyId, licenseResponse); + if (ret != napi_ok) { + DRM_ERR_LOG("napi ProcessLicenseResponse faild!"); + return nullptr; + } + size_t keyIdLen = keyId.size(); + NAPI_CALL(env, napi_create_array(env, &result)); + for (size_t i = 0; i < keyIdLen; i++) { + napi_value item; + napi_create_int32(env, keyId[i], &item); + napi_set_element(env, result, i, item); + } + } else { + DRM_ERR_LOG("KeySessionNapi ProcessLicenseResponse call Failed!"); + } + + DRM_INFO_LOG("KeySessionNapi::ProcessLicenseResponse exit."); + return result; +} + +napi_value KeySessionNapi::GenerateOfflineReleaseRequest(napi_env env, napi_callback_info info) +{ + DRM_INFO_LOG("KeySessionNapi::GenerateOfflineReleaseRequest enter"); + napi_value result = nullptr; + size_t argc = ARGS_ONE; + napi_value argv[ARGS_ONE] = {0}; + napi_value thisVar = nullptr; + napi_status status; + bool isTypeArray; + void *keyId = nullptr; + size_t keyIdLen; + size_t offset; + napi_value arraybuffer = nullptr; + napi_typedarray_type type; + std::vector releaseRequest; + KeySessionNapi* keySessionNapi = nullptr; + + DRM_NAPI_GET_JS_ARGS(env, info, argc, argv, thisVar); + NAPI_ASSERT(env, argc <= ARGS_ONE, "requires 1 parameters maximum"); + + napi_is_typedarray(env, argv[PARAM0], &isTypeArray); + if (!isTypeArray) { + DRM_ERR_LOG("argv[PARAM0] is not array!"); + return nullptr; + } + napi_get_typedarray_info(env, argv[PARAM0], &type, &keyIdLen, &keyId, &arraybuffer, &offset); + if (keyId == nullptr) { + DRM_ERR_LOG("napi_get_typedarray_info faild!"); + return nullptr; + } + uint8_t* keyIdPtr = reinterpret_cast(keyId); + std::vector keyIdVec(keyIdPtr, keyIdPtr + keyIdLen); + + status = napi_unwrap(env, thisVar, reinterpret_cast(&keySessionNapi)); + if (status == napi_ok && keySessionNapi != nullptr) { + int ret = keySessionNapi->keySessionImpl_->GenerateOfflineReleaseRequest(keyIdVec, releaseRequest); + if (ret != napi_ok) { + DRM_ERR_LOG("napi GenerateOfflineReleaseRequest faild!"); + return nullptr; + } + } else { + DRM_ERR_LOG("KeySessionNapi ProcessLicenseResponse call Failed!"); + } + + size_t releaseRequestLen = releaseRequest.size(); + NAPI_CALL(env, napi_create_array(env, &result)); + for (size_t i = 0; i < releaseRequestLen; i++) { + napi_value item; + napi_create_int32(env, releaseRequest[i], &item); + napi_set_element(env, result, i, item); + } + + DRM_INFO_LOG("KeySessionNapi::GenerateOfflineReleaseRequest exit"); + return result; +} + +napi_value KeySessionNapi::ProcessOfflineReleaseResponse(napi_env env, napi_callback_info info) +{ + DRM_INFO_LOG("KeySessionNapi::ProcessOfflineReleaseResponse enter."); + napi_value result = nullptr; + size_t argc = ARGS_TWO; + napi_value argv[ARGS_TWO] = {0}; + napi_value thisVar = nullptr; + napi_status status; + bool isTypeArray; + void *keyId = nullptr; + size_t keyIdLen; + size_t offset; + napi_value arraybuffer = nullptr; + napi_typedarray_type type; + void *response = nullptr; + size_t responseLen; + + DRM_NAPI_GET_JS_ARGS(env, info, argc, argv, thisVar); + NAPI_ASSERT(env, argc <= ARGS_TWO, "requires 2 parameters maximum"); + + napi_is_typedarray(env, argv[PARAM0], &isTypeArray); + if (!isTypeArray) { + DRM_ERR_LOG("argv[PARAM0] is not array!"); + return nullptr; + } + + napi_get_typedarray_info(env, argv[PARAM0], &type, &keyIdLen, &keyId, &arraybuffer, &offset); + if (keyId == nullptr) { + DRM_ERR_LOG("napi_get_typedarray_info faild!"); + return nullptr; + } + uint8_t* keyIdPtr = reinterpret_cast(keyId); + std::vector keyIdVec(keyIdPtr, keyIdPtr + keyIdLen); + napi_is_typedarray(env, argv[PARAM1], &isTypeArray); + if (!isTypeArray) { + DRM_ERR_LOG("argv[PARAM1] is not array!"); + return nullptr; + } + napi_get_typedarray_info(env, argv[PARAM1], &type, &responseLen, &response, &arraybuffer, &offset); + if (response == nullptr) { + DRM_ERR_LOG("napi_get_typedarray_info faild!"); + return nullptr; + } + uint8_t* responsePtr = reinterpret_cast(response); + std::vector responseVec(responsePtr, responsePtr + responseLen); + + KeySessionNapi* keySessionNapi = nullptr; + status = napi_unwrap(env, thisVar, reinterpret_cast(&keySessionNapi)); + if (status == napi_ok && keySessionNapi != nullptr) { + int ret = keySessionNapi->keySessionImpl_->ProcessOfflineReleaseResponse(keyIdVec, responseVec); + if (ret != napi_ok) { + DRM_ERR_LOG("napi ProcessOfflineReleaseResponse faild!"); + return nullptr; + } + } else { + DRM_ERR_LOG("KeySessionNapi ProcessOfflineReleaseResponse call Failed!"); + } + + DRM_INFO_LOG("KeySessionNapi::ProcessOfflineReleaseResponse exit."); + return result; +} + +static napi_value vectorToJsArray(napi_env env, std::vector &infoMap) +{ + DRM_INFO_LOG("vectorToJsArray enter."); + napi_value jsArray; + napi_value jsName; + napi_value jsValue; + napi_create_array_with_length(env, infoMap.size(), &jsArray); + for (size_t i = 0; i < infoMap.size(); i++) { + napi_value jsObject; + napi_create_object(env, &jsObject); + napi_create_string_utf8(env, infoMap[i].name.c_str(), NAPI_AUTO_LENGTH, &jsName); + napi_set_named_property(env, jsObject, "name", jsName); + napi_create_string_utf8(env, infoMap[i].value.c_str(), NAPI_AUTO_LENGTH, &jsValue); + napi_set_named_property(env, jsObject, "value", jsValue); + napi_set_element(env, jsArray, i, jsObject); + } + DRM_INFO_LOG("vectorToJsArray exit."); + return jsArray; +} + +napi_value KeySessionNapi::CheckLicenseStatus(napi_env env, napi_callback_info info) +{ + DRM_INFO_LOG("KeySessionNapi::CheckLicenseStatus enter"); + napi_value result = nullptr; + size_t argc = ARGS_ZERO; + napi_value argv[ARGS_ZERO]; + napi_value thisVar = nullptr; + napi_status status; + KeySessionNapi* keySessionNapi = nullptr; + std::vector infoMap; + + DRM_NAPI_GET_JS_ARGS(env, info, argc, argv, thisVar); + + status = napi_unwrap(env, thisVar, reinterpret_cast(&keySessionNapi)); + if (status == napi_ok && keySessionNapi != nullptr) { + int ret = keySessionNapi->keySessionImpl_->CheckLicenseStatus(infoMap); + if (ret != napi_ok) { + DRM_ERR_LOG("napi CheckLicenseStatus faild!"); + return nullptr; + } + } else { + DRM_ERR_LOG("KeySessionNapi CheckLicenseStatus call Failed!"); + } + + result = vectorToJsArray(env, infoMap); + DRM_INFO_LOG("KeySessionNapi::CheckLicenseStatus exit."); + return result; +} + +napi_value KeySessionNapi::RestoreOfflineKeys(napi_env env, napi_callback_info info) +{ + DRM_INFO_LOG("KeySessionNapi::RestoreOfflineKeys enter."); + napi_value result = nullptr; + size_t argc = ARGS_ONE; + napi_value argv[ARGS_ONE] = {0}; + napi_value thisVar = nullptr; + napi_status status; + bool isTypeArray; + void *keyId = nullptr; + size_t keyIdLen; + size_t offset; + napi_value arraybuffer = nullptr; + napi_typedarray_type type; + KeySessionNapi* keySessionNapi = nullptr; + + DRM_NAPI_GET_JS_ARGS(env, info, argc, argv, thisVar); + NAPI_ASSERT(env, argc <= ARGS_ONE, "requires 1 parameters maximum"); + + napi_is_typedarray(env, argv[PARAM0], &isTypeArray); + if (!isTypeArray) { + DRM_ERR_LOG("argv[PARAM0] is not array!"); + return nullptr; + } + napi_get_typedarray_info(env, argv[PARAM0], &type, &keyIdLen, &keyId, &arraybuffer, &offset); + if (keyId == nullptr) { + DRM_ERR_LOG("napi_get_typedarray_info faild!"); + return nullptr; + } + uint8_t* keyIdPtr = reinterpret_cast(keyId); + std::vector keyIdVec(keyIdPtr, keyIdPtr + keyIdLen); + status = napi_unwrap(env, thisVar, reinterpret_cast(&keySessionNapi)); + if (status == napi_ok && keySessionNapi != nullptr) { + int ret = keySessionNapi->keySessionImpl_->RestoreOfflineKeys(keyIdVec); + if (ret != napi_ok) { + DRM_ERR_LOG("napi RestoreOfflineKeys faild!"); + return nullptr; + } + } else { + DRM_ERR_LOG("KeySessionNapi RestoreOfflineKeys call Failed!"); + } + + DRM_INFO_LOG("KeySessionNapi::RestoreOfflineKeys exit."); + return result; +} + +napi_value KeySessionNapi::RemoveOfflineKeys(napi_env env, napi_callback_info info) +{ + DRM_INFO_LOG("KeySessionNapi::RemoveOfflineKeys enter."); + napi_value result = nullptr; + size_t argc = ARGS_ONE; + napi_value argv[ARGS_ONE] = {0}; + napi_value thisVar = nullptr; + napi_status status; + bool isTypeArray; + void *keyId = nullptr; + size_t keyIdLen; + size_t offset; + napi_value arraybuffer = nullptr; + napi_typedarray_type type; + + DRM_NAPI_GET_JS_ARGS(env, info, argc, argv, thisVar); + NAPI_ASSERT(env, argc <= ARGS_ONE, "requires 2 parameters maximum"); + napi_is_typedarray(env, argv[PARAM0], &isTypeArray); + if (!isTypeArray) { + DRM_ERR_LOG("argv[PARAM0] is not array!"); + return nullptr; + } + napi_get_typedarray_info(env, argv[PARAM0], &type, &keyIdLen, &keyId, &arraybuffer, &offset); + if (keyId == nullptr) { + DRM_ERR_LOG("napi_get_typedarray_info faild!"); + return nullptr; + } + uint8_t* keyIdPtr = reinterpret_cast(keyId); + std::vector keyIdVec(keyIdPtr, keyIdPtr + keyIdLen); + KeySessionNapi* keySessionNapi = nullptr; + status = napi_unwrap(env, thisVar, reinterpret_cast(&keySessionNapi)); + if (status == napi_ok && keySessionNapi != nullptr) { + int ret = keySessionNapi->keySessionImpl_->RemoveOfflineKeys(keyIdVec); + if (ret != napi_ok) { + DRM_ERR_LOG("napi RemoveOfflineKeys faild!"); + return nullptr; + } + } else { + DRM_ERR_LOG("KeySessionNapi RemoveOfflineKeys call Failed!"); + } + + DRM_INFO_LOG("KeySessionNapi::RemoveOfflineKeys exit."); + return result; +} + +static napi_value vectorToJs2DArray(napi_env env, std::vector> vec) +{ + DRM_INFO_LOG("vectorToJs2DArray enter."); + napi_value outArray; + napi_value inArray; + napi_status status = napi_create_array(env, &outArray); + for (size_t i = 0; i < vec.size(); i++) { + status = napi_create_array(env, &inArray); + for (size_t j = 0; j < vec[i].size(); j++) { + napi_value elem; + status = napi_create_uint32(env, vec[i][j], &elem); + status = napi_set_element(env, inArray, j, elem); + } + status = napi_set_element(env, outArray, i, inArray); + } + DRM_INFO_LOG("vectorToJs2DArray exit."); + return outArray; +} + +napi_value KeySessionNapi::GetOfflineKeyIds(napi_env env, napi_callback_info info) +{ + DRM_INFO_LOG("KeySessionNapi::GetOfflineKeyIds enter."); + napi_value result = nullptr; + size_t argc = ARGS_ZERO; + napi_value argv[ARGS_ZERO]; + napi_value thisVar = nullptr; + napi_status status; + + DRM_NAPI_GET_JS_ARGS(env, info, argc, argv, thisVar); + NAPI_ASSERT(env, argc >= ARGS_ZERO, "requires 0 parameters maximum"); + std::vector> keyIds; + KeySessionNapi* keySessionNapi = nullptr; + status = napi_unwrap(env, thisVar, reinterpret_cast(&keySessionNapi)); + if (status == napi_ok && keySessionNapi != nullptr) { + int ret = keySessionNapi->keySessionImpl_->GetOfflineKeyIds(keyIds); + if (ret != napi_ok) { + DRM_ERR_LOG("napi GetOfflineKeyIds faild!"); + return nullptr; + } + } else { + DRM_ERR_LOG("KeySessionNapi GetOfflineKeyIds call Failed!"); + } + + result = vectorToJs2DArray(env, keyIds); + DRM_INFO_LOG("KeySessionNapi::GetOfflineKeyIds exit."); + return result; +} + +napi_value KeySessionNapi::RemoveLicenses(napi_env env, napi_callback_info info) +{ + DRM_INFO_LOG("KeySessionNapi::RemoveLicenses enter."); + napi_value result = nullptr; + size_t argc = ARGS_ZERO; + napi_value argv[ARGS_ZERO]; + napi_value thisVar = nullptr; + napi_status status; + + int32_t currentPid = IPCSkeleton::GetCallingPid(); + DRM_DEBUG_LOG("KeySessionNapi GetCallingPID: %{public}d", currentPid); + + DRM_NAPI_GET_JS_ARGS(env, info, argc, argv, thisVar); + KeySessionNapi* keySessionNapi = nullptr; + status = napi_unwrap(env, thisVar, reinterpret_cast(&keySessionNapi)); + if (status == napi_ok && keySessionNapi != nullptr) { + int ret = keySessionNapi->keySessionImpl_->RemoveLicenses(); + if (ret != napi_ok) { + DRM_ERR_LOG("napi RemoveLicenses faild!"); + return nullptr; + } + } else { + DRM_ERR_LOG("KeySessionNapi RemoveLicenses call Failed!"); + } + + DRM_INFO_LOG("KeySessionNapi::RemoveLicenses exit."); + return result; +} + +napi_value KeySessionNapi::GetDecryptModule(napi_env env, napi_callback_info info) +{ + DRM_INFO_LOG("KeySessionNapi::GetDecryptModule enter."); + napi_status status; + napi_value result = nullptr; + size_t argc = ARGS_ZERO; + napi_value argv[ARGS_ZERO]; + napi_value thisVar = nullptr; + + DRM_NAPI_GET_JS_ARGS(env, info, argc, argv, thisVar); + + napi_get_undefined(env, &result); + KeySessionNapi* keySessionNapi = nullptr; + status = napi_unwrap(env, thisVar, reinterpret_cast(&keySessionNapi)); + sptr mediaDecryptModuleImpl = nullptr; + if (status == napi_ok && keySessionNapi != nullptr) { + mediaDecryptModuleImpl = keySessionNapi->keySessionImpl_->GetDecryptModule(); + if (mediaDecryptModuleImpl == nullptr) { + DRM_ERR_LOG("KeySessionNapi mediaDecryptModuleImpl get failed!!!"); + return nullptr; + } + } else { + DRM_ERR_LOG("KeySessionNapi RemoveLicenses call Failed!"); + } + + result = MediaDecryptModuleNapi::GetDecryptModule(env, mediaDecryptModuleImpl); + DRM_INFO_LOG("KeySessionNapi::GetDecryptModule exit."); + return result; +} + +napi_value KeySessionNapi::GetOfflineKeyState(napi_env env, napi_callback_info info) +{ + DRM_INFO_LOG("KeySessionNapi::GetOfflineKeyState enter"); + napi_value result = nullptr; + size_t argc = ARGS_ONE; + napi_value argv[ARGS_ONE] = {0}; + napi_value thisVar = nullptr; + napi_status status; + void *keyId = nullptr; + size_t keyIdLen; + size_t offset; + napi_value arraybuffer = nullptr; + napi_typedarray_type type; + KeySessionNapi* keySessionNapi = nullptr; + bool isTypeArray; + + DRM_NAPI_GET_JS_ARGS(env, info, argc, argv, thisVar); + NAPI_ASSERT(env, argc <= ARGS_ONE, "requires 1 parameters maximum"); + napi_is_typedarray(env, argv[PARAM0], &isTypeArray); + if (!isTypeArray) { + DRM_ERR_LOG("argv[PARAM0] is not array!"); + return nullptr; + } + napi_get_typedarray_info(env, argv[PARAM0], &type, &keyIdLen, &keyId, &arraybuffer, &offset); + if (keyId == nullptr) { + DRM_ERR_LOG("napi_get_typedarray_info faild!"); + return nullptr; + } + uint8_t* keyIdPtr = reinterpret_cast(keyId); + std::vector keyIdVec(keyIdPtr, keyIdPtr + keyIdLen); + IKeySessionService::OfflineKeyState offlineKeyState = IKeySessionService::OFFLINEKEYSTATE_UNKNOWN; + status = napi_unwrap(env, thisVar, reinterpret_cast(&keySessionNapi)); + if (status == napi_ok && keySessionNapi != nullptr) { + int ret = keySessionNapi->keySessionImpl_->GetOfflineKeyState(keyIdVec, offlineKeyState); + if (ret != napi_ok) { + DRM_ERR_LOG("napi GetOfflineKeyState faild!"); + return nullptr; + } + } else { + DRM_ERR_LOG("KeySessionNapi GetOfflineKeyState call Failed!"); + return nullptr; + } + + NAPI_CALL(env, napi_create_int32(env, (int)offlineKeyState, &result)); + DRM_INFO_LOG("KeySessionNapi::GetOfflineKeyState exit"); + return result; +} + + +napi_value KeySessionNapi::AddEventListener(napi_env env, napi_callback_info info) +{ + napi_value result = nullptr; + return result; +} + +napi_value KeySessionNapi::DeleteEventListener(napi_env env, napi_callback_info info) +{ + napi_value result = nullptr; + return result; +} + +void KeySessionNapi::SetEventCallbackReference(const std::string eventType, sptr callbackPair) +{ + DRM_INFO_LOG("KeySessionNapi::SetCallbackReference"); + std::lock_guard lock(mutex_); + if (keySessionCallbackNapi_ != nullptr) { + keySessionCallbackNapi_->SetCallbackReference(eventType, callbackPair); + } else { + DRM_ERR_LOG("KeySessionNapi::SetCallbackReference failed"); + } +} + +void KeySessionNapi::ClearEventCallbackReference(const std::string eventType) +{ + DRM_INFO_LOG("KeySessionNapi::ClearEventCallbackReference"); + if (keySessionCallbackNapi_ != nullptr) { + keySessionCallbackNapi_->ClearCallbackReference(eventType); + } else { + DRM_ERR_LOG("KeySessionNapi::ClearEventCallbackReference failed"); + } +} + +napi_value KeySessionNapi::SetEventCallback(napi_env env, napi_callback_info info) +{ + DRM_INFO_LOG("KeySessionNapi::SetEventCallback"); + napi_value result = nullptr; + napi_get_undefined(env, &result); + char buffer[PATH_MAX]; + size_t length = 0; + size_t argc = ARGS_TWO; + napi_value thisVar = nullptr; + napi_value argv[ARGS_TWO] = { nullptr }; + DRM_NAPI_GET_JS_ARGS(env, info, argc, argv, thisVar); + NAPI_ASSERT(env, argc == ARGS_TWO, "only requires 2 parameters"); + if (thisVar == nullptr || argv[PARAM0] == nullptr || argv[PARAM1] == nullptr) { + DRM_ERR_LOG("Failed to retrieve arguments in SetEventCallback!"); + return result; + } + napi_valuetype valueType = napi_undefined; + if (napi_typeof(env, argv[PARAM0], &valueType) != napi_ok || valueType != napi_string || + napi_typeof(env, argv[PARAM1], &valueType) != napi_ok || valueType != napi_function) { + return result; + } + + KeySessionNapi* keySessionNapi = nullptr; + napi_status status = napi_unwrap(env, thisVar, reinterpret_cast(&keySessionNapi)); + if (status == napi_ok && keySessionNapi != nullptr) { + napi_get_value_string_utf8(env, argv[PARAM0], buffer, PATH_MAX, &length); + std::string eventType = std::string(buffer); + napi_ref callbackRef; + napi_create_reference(env, argv[PARAM1], 1, &callbackRef); + DRM_INFO_LOG("SetEventCallback event is %{public}s", eventType.c_str()); + + sptr callbackPair = new CallBackPair(env, callbackRef); + keySessionNapi->SetEventCallbackReference(eventType, callbackPair); + DRM_INFO_LOG("KeySessionNapi::SetEventCallback out"); + } else { + DRM_ERR_LOG("KeySessionNapi SetEventCallback failed!"); + } + return result; +} + +napi_value KeySessionNapi::UnsetEventCallback(napi_env env, napi_callback_info info) +{ + DRM_INFO_LOG("KeySessionNapi::UnsetEventCallback"); + napi_value result = nullptr; + napi_get_undefined(env, &result); + napi_value thisVar = nullptr; + napi_value argv[1] = { nullptr }; + size_t argc = 1; + DRM_NAPI_GET_JS_ARGS(env, info, argc, argv, thisVar); + NAPI_ASSERT(env, argc == ARGS_ONE, "only requires 1 parameters"); + if (thisVar == nullptr || argv[PARAM0] == nullptr) { + DRM_ERR_LOG("Failed to retrieve arguments in UnsetEventCallback!"); + return result; + } + napi_valuetype valueType = napi_undefined; + if (napi_typeof(env, argv[PARAM0], &valueType) != napi_ok || valueType != napi_string) { + DRM_ERR_LOG("Failed to retrieve reasonable arguments in UnsetEventCallback!"); + return result; + } + + KeySessionNapi* keySessionNapi = nullptr; + char buffer[PATH_MAX]; + size_t length = 0; + napi_status status = napi_unwrap(env, thisVar, reinterpret_cast(&keySessionNapi)); + if (status == napi_ok && keySessionNapi != nullptr) { + napi_get_value_string_utf8(env, argv[PARAM0], buffer, PATH_MAX, &length); + std::string eventType = std::string(buffer); + keySessionNapi->ClearEventCallbackReference(eventType); + DRM_INFO_LOG("KeySessionNapi::UnsetEventCallback out"); + } else { + DRM_ERR_LOG("KeySessionNapi UnsetEventCallback failed!"); + } + return result; + +} + +} // DrmStandardr +} // OHOS \ No newline at end of file diff --git a/frameworks/js/drm_napi/media_decrypt_module_napi.cpp b/frameworks/js/drm_napi/media_decrypt_module_napi.cpp new file mode 100644 index 00000000..12a1c933 --- /dev/null +++ b/frameworks/js/drm_napi/media_decrypt_module_napi.cpp @@ -0,0 +1,352 @@ +/* + * Copyright (c) 2023-2024 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#include "media_decrypt_module_napi.h" +#include "media_decrypt_module_impl.h" +#include "key_session_impl.h" +#include "ipc_skeleton.h" + +namespace OHOS { +namespace DrmStandard { + +thread_local napi_ref MediaDecryptModuleNapi::sConstructor_ = nullptr; +thread_local sptr MediaDecryptModuleNapi::sMediaDecryptModuleImpl_ = nullptr; + +MediaDecryptModuleNapi::MediaDecryptModuleNapi() : env_(nullptr), wrapper_(nullptr) +{ + DRM_DEBUG_LOG("0x%{public}06" PRIXPTR " Instances create", FAKE_POINTER(this)); +} + +MediaDecryptModuleNapi::~MediaDecryptModuleNapi() +{ + DRM_DEBUG_LOG("0x%{public}06" PRIXPTR " Instances destroy", FAKE_POINTER(this)); + if (wrapper_ != nullptr) { + napi_delete_reference(env_, wrapper_); + } + if (sMediaDecryptModuleImpl_) { + sMediaDecryptModuleImpl_ = nullptr; + } +} + +napi_value MediaDecryptModuleNapi::Init(napi_env env, napi_value exports) +{ + DRM_INFO_LOG("MediaDecryptModuleNapi Init ENTER"); + + napi_status status; + napi_value ctorObj; + int32_t refCount = 1; + + napi_property_descriptor decryptModule_props[] = { + DECLARE_NAPI_FUNCTION("release", Release), + DECLARE_NAPI_FUNCTION("requireSecureDecoderModule", RequireSecureDecoderModule), + DECLARE_NAPI_FUNCTION("decryptData", DecryptData), + }; + + DRM_DEBUG_LOG("MediaDecryptModuleNapi napi_define_class"); + status = napi_define_class(env, DECYPT_MODULE_NAPI_CLASS_NAME, NAPI_AUTO_LENGTH, + MediaDecryptModuleNapiConstructor, nullptr, + sizeof(decryptModule_props) / sizeof(decryptModule_props[PARAM0]), + decryptModule_props, &ctorObj); + if (status == napi_ok) { + status = napi_create_reference(env, ctorObj, refCount, &sConstructor_); + if (status == napi_ok) { + status = napi_set_named_property(env, exports, DECYPT_MODULE_NAPI_CLASS_NAME, ctorObj); + if (status == napi_ok) { + DRM_INFO_LOG("MediaDecryptModuleNapi return"); + return exports; + } + } + } + DRM_INFO_LOG("MediaDecryptModuleNapi Init call Failed!"); + return nullptr; +} + +napi_value MediaDecryptModuleNapi::MediaDecryptModuleNapiConstructor(napi_env env, napi_callback_info info) +{ + DRM_INFO_LOG("MediaDecryptModuleNapi::MediaDecryptModuleNapiConstructor enter."); + napi_status status; + napi_value result = nullptr; + napi_value thisVar = nullptr; + + napi_get_undefined(env, &result); + DRM_NAPI_GET_JS_OBJ_WITH_ZERO_ARGS(env, info, status, thisVar); + + if (status == napi_ok && thisVar != nullptr) { + std::unique_ptr obj = std::make_unique(); + if (obj != nullptr) { + obj->env_ = env; + if (sMediaDecryptModuleImpl_ == nullptr) { + DRM_ERR_LOG("sMediaDecryptModuleImpl_ is null"); + return result; + } + obj->mediaDecryptModuleImpl_ = sMediaDecryptModuleImpl_; + status = napi_wrap(env, thisVar, reinterpret_cast(obj.get()), + MediaDecryptModuleNapi::MediaDecryptModuleNapiDestructor, nullptr, nullptr); + if (status == napi_ok) { + obj.release(); + return thisVar; + } else { + DRM_ERR_LOG("MediaDecryptModuleNapi Failure wrapping js to native napi"); + } + } + } + DRM_INFO_LOG("MediaDecryptModuleNapi::MediaDecryptModuleNapiConstructor exit."); + return result; +} + +void MediaDecryptModuleNapi::MediaDecryptModuleNapiDestructor(napi_env env, void *nativeObject, void *finalize) +{ + DRM_INFO_LOG("MediaDecryptModuleNapi::MediaDecryptModuleNapiDestructor enter."); + MediaDecryptModuleNapi* decryptModuleNapiObj = reinterpret_cast(nativeObject); + if (decryptModuleNapiObj != nullptr) { + decryptModuleNapiObj->~MediaDecryptModuleNapi(); + } + DRM_INFO_LOG("MediaDecryptModuleNapi::MediaDecryptModuleNapiDestructor exit."); +} + +napi_value MediaDecryptModuleNapi::GetDecryptModule(napi_env env, sptr decryptModuleImpl) +{ + DRM_INFO_LOG("MediaDecryptModuleNapi::GetDecryptModule enter."); + napi_status status; + napi_value result = nullptr; + napi_value constructor; + + status = napi_get_reference_value(env, sConstructor_, &constructor); + if (status == napi_ok) { + sMediaDecryptModuleImpl_ = decryptModuleImpl; + if (sMediaDecryptModuleImpl_ == nullptr) { + DRM_ERR_LOG("Failed to GetDecryptModule instance"); + napi_get_undefined(env, &result); + return result; + } + status = napi_new_instance(env, constructor, 0, nullptr, &result); + sMediaDecryptModuleImpl_ = nullptr; + if (status == napi_ok && result != nullptr) { + DRM_INFO_LOG("success to GetDecryptModule napi instance"); + return result; + } else { + DRM_ERR_LOG("Failed to GetDecryptModule napi instance"); + } + } + DRM_ERR_LOG("Failed to create GetDecryptModule napi instance last"); + napi_get_undefined(env, &result); + DRM_INFO_LOG("MediaDecryptModuleNapi::GetDecryptModule exit."); + return result; +} + +napi_value MediaDecryptModuleNapi::Release(napi_env env, napi_callback_info info) +{ + DRM_INFO_LOG("MediaDecryptModuleNapi::Release enter."); + int32_t currentPid = IPCSkeleton::GetCallingPid(); + DRM_DEBUG_LOG("MediaDecryptModuleNapi GetCallingPID: %{public}d", currentPid); + + napi_status status; + napi_value result = nullptr; + size_t argc = ARGS_ZERO; + napi_value argv[ARGS_ZERO]; + napi_value thisVar = nullptr; + + DRM_NAPI_GET_JS_ARGS(env, info, argc, argv, thisVar); + + napi_get_undefined(env, &result); + MediaDecryptModuleNapi* decryptModuleNapi = nullptr; + status = napi_unwrap(env, thisVar, reinterpret_cast(&decryptModuleNapi)); + if (status == napi_ok && decryptModuleNapi != nullptr) { + decryptModuleNapi->mediaDecryptModuleImpl_->Release(); + } else { + DRM_ERR_LOG("MediaDecryptModuleNapi Release call Failed!"); + } + DRM_INFO_LOG("MediaDecryptModuleNapi::Release exit."); + return result; +} + +napi_value MediaDecryptModuleNapi::RequireSecureDecoderModule(napi_env env, napi_callback_info info) +{ + DRM_INFO_LOG("MediaDecryptModuleNapi::RequireSecureDecoderModule enter."); + napi_value result = nullptr; + size_t argc = ARGS_ONE; + napi_value argv[ARGS_ONE] = {0}; + napi_value thisVar = nullptr; + napi_status status; + + DRM_NAPI_GET_JS_ARGS(env, info, argc, argv, thisVar); + napi_get_undefined(env, &result); + NAPI_ASSERT(env, argc <= ARGS_ONE, "requires 1 parameters maximum"); + char mimeTypeBuf[PATH_MAX]; + size_t length = 0; + if (napi_get_value_string_utf8(env, argv[PARAM0], mimeTypeBuf, PATH_MAX, &length) == napi_ok) { + std::string mimeType = std::string(mimeTypeBuf); + bool statusValue; + MediaDecryptModuleNapi* decryptModuleNapi = nullptr; + status = napi_unwrap(env, thisVar, reinterpret_cast(&decryptModuleNapi)); + if (status == napi_ok && decryptModuleNapi != nullptr) { + if (decryptModuleNapi->mediaDecryptModuleImpl_ != nullptr) { + decryptModuleNapi->mediaDecryptModuleImpl_->RequireSecureDecoderModule(mimeType, &statusValue); + status = napi_get_boolean(env, statusValue, &result); + DRM_INFO_LOG("napi_get_boolean call success!,statusValue:%{public}d.", statusValue); + if (status != napi_ok) { + DRM_ERR_LOG("napi_get_boolean call faild!"); + return nullptr; + } + } else { + DRM_ERR_LOG("mediaDecryptModuleImpl_ == nullptr."); + return nullptr; + } + } else { + DRM_ERR_LOG("MediaDecryptModuleNapi DecryptData call Failed!"); + return nullptr; + } + } else { + DRM_ERR_LOG("-Could not able to read response argument!"); + return nullptr; + } + DRM_INFO_LOG("MediaDecryptModuleNapi::RequireSecureDecoderModule exit."); + return result; +} + +napi_value MediaDecryptModuleNapi::DecryptData(napi_env env, napi_callback_info info) +{ + DRM_INFO_LOG("MediaDecryptModuleNapi::DecryptData enter."); + napi_value result = nullptr; + size_t argc = 4; // 4 is argc number + napi_value argv[4] = {0}; // 4 is argc number + napi_value thisVar = nullptr; + napi_status status; + IMediaDecryptModuleService::CryptInfo cryptInfo; + napi_value type_property; + napi_value keyId_property; + napi_value iv_property; + napi_value pattern_property; + napi_value subSample_property; + napi_value subSampleNumber_property; + bool secureDecodrtState = false; + int32_t type = 0; + bool isTypeArray; + void *keyId = nullptr; + size_t keyIdLen; + size_t offset; + napi_value arraybuffer = nullptr; + napi_typedarray_type array_type; + void *iv = nullptr; + size_t ivLen; + napi_value pattern_property_encryptBlocks; + napi_value pattern_property_skipBlocks; + int32_t subSampleLen = 0; + napi_value jsClearHeaderLen; + napi_value jsPayLoadLen; + int32_t srcBuffer = 0; + int32_t dstBuffer = 0; + + DRM_NAPI_GET_JS_ARGS(env, info, argc, argv, thisVar); + napi_get_undefined(env, &result); + NAPI_ASSERT(env, argc <= 4, "requires 4 parameters maximum"); + status = napi_get_value_bool(env, argv[PARAM0], &secureDecodrtState); + if (status != napi_ok) { + DRM_ERR_LOG("Could not able to read secureDecodrtState argument!"); + return nullptr; + } + napi_get_named_property(env, argv[PARAM1], "type", &type_property); + napi_get_value_int32(env, type_property, &type); + if (type == 0) { + cryptInfo.type = IMediaDecryptModuleService::ALGTYPE_UNENCRYPTED; + DRM_DEBUG_LOG("cryptInfo.type:%{public}d.",(int)cryptInfo.type); + } else if (type == 1) { + cryptInfo.type = IMediaDecryptModuleService::ALGTYPE_AES_CTR; + DRM_DEBUG_LOG("cryptInfo.type:%{public}d.",(int)cryptInfo.type); + } else if (type == 2) { + cryptInfo.type = IMediaDecryptModuleService::ALGTYPE_AES_WV; + DRM_DEBUG_LOG("cryptInfo.type:%{public}d.",(int)cryptInfo.type); + } else if (type == 3) { + cryptInfo.type = IMediaDecryptModuleService::ALGTYPE_AES_CBC; + DRM_DEBUG_LOG("cryptInfo.type:%{public}d.",(int)cryptInfo.type); + } else if (type == 4) { + cryptInfo.type = IMediaDecryptModuleService::ALGTYPE_SM4_CBC; + DRM_DEBUG_LOG("cryptInfo.type:%{public}d.",(int)cryptInfo.type); + } + + napi_get_named_property(env, argv[PARAM1], "keyId", &keyId_property); + napi_is_typedarray(env, keyId_property, &isTypeArray); + if (!isTypeArray) { + DRM_ERR_LOG("argv[PARAM1] keyId is not array!"); + return nullptr; + } + napi_get_typedarray_info(env, keyId_property, &array_type, &keyIdLen, &keyId, &arraybuffer, &offset); + if (keyId == nullptr) { + DRM_ERR_LOG("napi_get_typedarray_info faild!"); + return nullptr; + } + uint8_t* keyIdPtr = reinterpret_cast(keyId); + std::vector keyIdVec(keyIdPtr, keyIdPtr + keyIdLen); + cryptInfo.keyId.assign(keyIdVec.begin(), keyIdVec.end()); + + napi_get_named_property(env, argv[PARAM1], "iv", &iv_property); + napi_is_typedarray(env, iv_property, &isTypeArray); + if (!isTypeArray) { + DRM_ERR_LOG("argv[PARAM1] iv is not array!"); + return nullptr; + } + napi_get_typedarray_info(env, iv_property, &array_type, &ivLen, &iv, &arraybuffer, &offset); + if (iv == nullptr) { + DRM_ERR_LOG("napi_get_typedarray_info faild!"); + return nullptr; + } + uint8_t* ivPtr = reinterpret_cast(iv); + std::vector ivVec(ivPtr, ivPtr + ivLen); + cryptInfo.iv.assign(ivVec.begin(), ivVec.end()); + + napi_get_named_property(env, argv[PARAM1], "pattern", &pattern_property); + napi_get_named_property(env, pattern_property, "encryptBlocks", &pattern_property_encryptBlocks); + napi_get_named_property(env, pattern_property, "skipBlocks", &pattern_property_skipBlocks); + napi_get_value_int32(env, pattern_property_encryptBlocks, &cryptInfo.pattern.encryptBlocks); + napi_get_value_int32(env, pattern_property_skipBlocks, &cryptInfo.pattern.skipBlocks); + napi_get_named_property(env, argv[PARAM1], "subSampleNumber", &subSampleNumber_property); + napi_get_value_int32(env, subSampleNumber_property, &subSampleLen); + napi_get_named_property(env, argv[PARAM1], "subSample", &subSample_property); + for (uint32_t i = 0; i < subSampleLen; i++) { + napi_value jsSubSample; + IMediaDecryptModuleService::SubSample subSampleTmp = {0}; + napi_get_element(env, subSample_property, i, &jsSubSample); + napi_get_named_property(env, jsSubSample, "clearHeaderLen", &jsClearHeaderLen); + napi_get_named_property(env, jsSubSample, "payLoadLen", &jsPayLoadLen); + napi_get_value_int32(env, jsClearHeaderLen, &subSampleTmp.clearHeaderLen); + napi_get_value_int32(env, jsPayLoadLen, &subSampleTmp.payLoadLen); + cryptInfo.subSample.push_back(subSampleTmp); + } + status = napi_get_value_int32(env, argv[PARAM2], &srcBuffer); + if (status != napi_ok) { + DRM_ERR_LOG("Could not able to read srcBuffer argument!"); + return nullptr; + } + status = napi_get_value_int32(env, argv[3], &dstBuffer); + if (status != napi_ok) { + DRM_ERR_LOG("Could not able to read dstBuffer argument!"); + return nullptr; + } + MediaDecryptModuleNapi* decryptModuleNapi = nullptr; + status = napi_unwrap(env, thisVar, reinterpret_cast(&decryptModuleNapi)); + if (status == napi_ok && decryptModuleNapi != nullptr) { + if (decryptModuleNapi->mediaDecryptModuleImpl_ != nullptr) { + decryptModuleNapi->mediaDecryptModuleImpl_->DecryptData(secureDecodrtState, cryptInfo, srcBuffer, dstBuffer); + } else { + DRM_ERR_LOG("mediaDecryptModuleImpl_ == nullptr."); + return nullptr; + } + } else { + DRM_ERR_LOG("MediaDecryptModuleNapi DecryptData call Failed!"); + } + DRM_INFO_LOG("MediaDecryptModuleNapi::DecryptData exit."); + return result; +} +} // DrmStandardr +} // OHOS \ No newline at end of file diff --git a/frameworks/js/drm_napi/media_key_system_callback_napi.cpp b/frameworks/js/drm_napi/media_key_system_callback_napi.cpp new file mode 100644 index 00000000..08bdaca2 --- /dev/null +++ b/frameworks/js/drm_napi/media_key_system_callback_napi.cpp @@ -0,0 +1,14 @@ +/* + * Copyright (c) 2023-2024 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ diff --git a/frameworks/js/drm_napi/media_key_system_napi.cpp b/frameworks/js/drm_napi/media_key_system_napi.cpp new file mode 100644 index 00000000..2a3a6b58 --- /dev/null +++ b/frameworks/js/drm_napi/media_key_system_napi.cpp @@ -0,0 +1,591 @@ +/* + * Copyright (c) 2023-2024 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#include "media_key_system_napi.h" +#include "key_session_napi.h" + +namespace OHOS { +namespace DrmStandard { + +thread_local napi_ref MediaKeySystemNapi::sConstructor_ = nullptr; +thread_local sptr MediaKeySystemNapi::sMediaKeySystemImpl_ = nullptr; + +MediaKeySystemNapi::MediaKeySystemNapi() : env_(nullptr), wrapper_(nullptr) +{ + DRM_DEBUG_LOG("0x%{public}06" PRIXPTR " Instances create", FAKE_POINTER(this)); +} + +MediaKeySystemNapi::~MediaKeySystemNapi() +{ + DRM_INFO_LOG("MediaKeySystemNapi::~MediaKeySystemNapi enter."); + DRM_DEBUG_LOG("0x%{public}06" PRIXPTR " Instances destroy", FAKE_POINTER(this)); + if (wrapper_ != nullptr) { + napi_delete_reference(env_, wrapper_); + } + if (mediaKeySystemImpl_) { + mediaKeySystemImpl_ = nullptr; + } + DRM_INFO_LOG("MediaKeySystemNapi::~MediaKeySystemNapi exit."); +} + +napi_value MediaKeySystemNapi::Init(napi_env env, napi_value exports) +{ + DRM_INFO_LOG("MediaKeySystemNapi Init enter."); + napi_status status; + napi_value ctorObj; + int32_t refCount = 1; + + napi_property_descriptor properties[] = { + DECLARE_NAPI_FUNCTION("setConfiguration", SetConfiguration), + DECLARE_NAPI_FUNCTION("getConfiguration", GetConfiguration), + DECLARE_NAPI_FUNCTION("generateKeySystemRequest", GenerateKeySystemRequest), + DECLARE_NAPI_FUNCTION("processKeySystemResponse", ProcessKeySystemResponse), + DECLARE_NAPI_FUNCTION("createKeySession", CreateKeySession), + DECLARE_NAPI_FUNCTION("getMetric", GetMetric), + DECLARE_NAPI_FUNCTION("getSecurityLevel", GetSecurityLevel), + }; + + status = napi_define_class(env, MEDIA_KEY_SYSTEM_NAPI_CLASS_NAME, NAPI_AUTO_LENGTH, + MediaKeySystemNapiConstructor, nullptr, + sizeof(properties) / sizeof(properties[PARAM0]), + properties, &ctorObj); + if (status == napi_ok) { + if (napi_create_reference(env, ctorObj, refCount, &sConstructor_) == napi_ok) { + status = napi_set_named_property(env, exports, MEDIA_KEY_SYSTEM_NAPI_CLASS_NAME, ctorObj); + if (status == napi_ok) { + return exports; + } + } + } + DRM_INFO_LOG("MediaKeySystemNapi Init exit."); + return nullptr; +} + +napi_value MediaKeySystemNapi::MediaKeySystemNapiConstructor(napi_env env, napi_callback_info info) +{ + DRM_INFO_LOG("MediaKeySystemNapi::MediaKeySystemNapiConstructor enter."); + napi_value result = nullptr; + size_t argCount = 0; + napi_value jsThis = nullptr; + + napi_get_undefined(env, &result); + + napi_status status = napi_get_cb_info(env, info, &argCount, nullptr, &jsThis, nullptr); + CHECK_AND_RETURN_RET_LOG(status == napi_ok, result, "failed to napi_get_cb_info"); + + if (status == napi_ok && jsThis != nullptr) { + std::unique_ptr obj = std::make_unique(); + obj->env_ = env; + if (MediaKeySystemNapi::sMediaKeySystemImpl_ == nullptr) { + DRM_ERR_LOG("sMediaKeySystemImpl_ is null"); + return result; + } + obj->mediaKeySystemImpl_ = MediaKeySystemNapi::sMediaKeySystemImpl_; + + status = napi_wrap(env, jsThis, reinterpret_cast(obj.get()), + MediaKeySystemNapi::MediaKeySystemNapiDestructor, nullptr, nullptr); + if (status == napi_ok) { + obj.release(); + return jsThis; + } else { + DRM_ERR_LOG("Failure wrapping js to native napi"); + } + DRM_ERR_LOG("will call IsMediaKeySystemSupported"); + } + + DRM_INFO_LOG("MediaKeySystemNapi::MediaKeySystemNapiConstructor exit."); + return result; +} + +napi_value MediaKeySystemNapi::CreateMediaKeySystemInstance(napi_env env, napi_callback_info info) +{ + DRM_INFO_LOG("MediaKeySystemNapi::CreateMediaKeySystemInstance enter."); + napi_status status; + napi_value result = nullptr; + napi_value ctor; + + status = napi_get_reference_value(env, sConstructor_, &ctor); + if (status == napi_ok) { + size_t argc = ARGS_ONE; + napi_value argv[ARGS_ONE] = {0}; + napi_value thisVar = nullptr; + + DRM_NAPI_GET_JS_ARGS(env, info, argc, argv, thisVar); + + napi_get_undefined(env, &result); + + std::string uuid; + char uuidBuffer[PATH_MAX]; + size_t uuidBufferLen = 0; + status = napi_get_value_string_utf8(env, argv[PARAM0], uuidBuffer, PATH_MAX, &uuidBufferLen); + if (status != napi_ok) { + DRM_ERR_LOG("Could not able to read uuid argument!"); + return nullptr; + } + uuid = std::string(uuidBuffer); + int retCode = MediaKeySystemFactoryImpl::GetInstance()->CreateMediaKeySystem(uuid, &MediaKeySystemNapi::sMediaKeySystemImpl_); + if (retCode != DRM_OK || MediaKeySystemNapi::sMediaKeySystemImpl_ == nullptr) { + DRM_ERR_LOG("MediaKeySystemNapi sMediaKeySystemImpl_ get failed!!!"); + return nullptr; + } + status = napi_new_instance(env, ctor, 0, nullptr, &result); + MediaKeySystemNapi::sMediaKeySystemImpl_ = nullptr; + if (status == napi_ok) { + DRM_ERR_LOG("CreateMediaKeySystemInstance 164"); + return result; + } else { + DRM_ERR_LOG("New instance could not be obtained"); + } + } + napi_get_undefined(env, &result); + DRM_INFO_LOG("CreateMediaKeySystemInstance exit."); + return result; +} + +void MediaKeySystemNapi::MediaKeySystemNapiDestructor(napi_env env, void *nativeObject, void *finalize) +{ + DRM_INFO_LOG("MediaKeySystemNapi::MediaKeySystemNapiDestructor enter."); + MediaKeySystemNapi* mediaKeySystemNapi = reinterpret_cast(nativeObject); + if (mediaKeySystemNapi != nullptr) { + mediaKeySystemNapi->~MediaKeySystemNapi(); + } + DRM_INFO_LOG("MediaKeySystemNapi::MediaKeySystemNapiDestructor exit."); +} + +napi_value MediaKeySystemNapi::Release(napi_env env, napi_callback_info info) +{ + DRM_INFO_LOG("MediaKeySystemNapi::Release enter."); + int32_t currentPid = IPCSkeleton::GetCallingPid(); + DRM_DEBUG_LOG("MediaKeySystemNapi GetCallingPID: %{public}d", currentPid); + + napi_status status; + napi_value result = nullptr; + size_t argc = ARGS_ZERO; + napi_value argv[ARGS_ZERO]; + napi_value thisVar = nullptr; + MediaKeySystemNapi* mediaKeySystemNapi = nullptr; + + DRM_NAPI_GET_JS_ARGS(env, info, argc, argv, thisVar); + napi_get_undefined(env, &result); + status = napi_unwrap(env, thisVar, reinterpret_cast(&mediaKeySystemNapi)); + if (status == napi_ok && mediaKeySystemNapi != nullptr) { + mediaKeySystemNapi->mediaKeySystemImpl_->Release(); + } else { + DRM_ERR_LOG("mediaKeySystemNapi Release call Failed!"); + } + DRM_INFO_LOG("MediaKeySystemNapi::Release exit."); + return result; +} + +napi_value MediaKeySystemNapi::IsMediaKeySystemSupported(napi_env env, napi_callback_info info) +{ + DRM_INFO_LOG("MediaKeySystemNapi::IsMediaKeySystemSupported enter."); + napi_value result = nullptr; + size_t argc = ARGS_THREE; + napi_value argv[ARGS_THREE] = {0, 0, 0}; + napi_value thisVar = nullptr; + + DRM_NAPI_GET_JS_ARGS(env, info, argc, argv, thisVar); + char buffer[PATH_MAX]; + size_t length = 0; + // get uuid + if (napi_get_value_string_utf8(env, argv[PARAM0], buffer, PATH_MAX, &length) != napi_ok) { + DRM_ERR_LOG("Could not able to read uuid argument!"); + return nullptr; + } + std::string uuid = std::string(buffer); + memset(buffer, '\0', PATH_MAX); + // search by uuid + if (argc == ARGS_ONE) { + bool isSurpportted = MediaKeySystemFactoryImpl::GetInstance()->IsMediaKeySystemSupported(uuid); + napi_get_boolean(env, isSurpportted, &result); + return result; + } + // get mimeType + if (napi_get_value_string_utf8(env, argv[PARAM1], buffer, PATH_MAX, &length) != napi_ok) { + DRM_ERR_LOG("Could not able to read mimeType argument!"); + return nullptr; + } + std::string mimeType = std::string(buffer); + memset(buffer, '\0', PATH_MAX); + // search by uuid and mineType + if (argc == ARGS_TWO) { + bool isSurpportted = MediaKeySystemFactoryImpl::GetInstance()->IsMediaKeySystemSupported(uuid, mimeType); + napi_get_boolean(env, isSurpportted, &result); + return result; + } + + // get securityLevel + int32_t jsSecurityLevel = -1; + if (napi_get_value_int32(env, argv[PARAM2], &jsSecurityLevel) != napi_ok) { + DRM_ERR_LOG("Could not able to read securityLevel argument!"); + return nullptr; + } + IKeySessionService::SecurityLevel securityLevel = IKeySessionService::SECURITY_LEVEL_UNKNOWN; + switch (jsSecurityLevel) { + case 0: + securityLevel = IKeySessionService::SECURITY_LEVEL_UNKNOWN; + break; + case 1: + securityLevel = IKeySessionService::SECURITY_LEVEL_SW_CRYPTO; + break; + case 2: + securityLevel = IKeySessionService::SECURITY_LEVEL_SW_DECODE; + break; + case 3: + securityLevel = IKeySessionService::SECURITY_LEVEL_HW_CRYPTO; + break; + case 4: + securityLevel = IKeySessionService::SECURITY_LEVEL_HW_DECODE; + break; + case 5: + securityLevel = IKeySessionService::SECURITY_LEVEL_HW_ALL; + break; + case 6: + securityLevel = IKeySessionService::SECURITY_LEVEL_MAX; + break; + default: + DRM_ERR_LOG("jsSecurityLevel is invalid"); + return nullptr; + } + + // search by uuid, mineType and securityLevel + if (argc == ARGS_THREE) { + bool isSurpportted = MediaKeySystemFactoryImpl::GetInstance()->IsMediaKeySystemSupported(uuid, mimeType, securityLevel); + napi_get_boolean(env, isSurpportted, &result); + return result; + } + DRM_INFO_LOG("MediaKeySystemNapi::IsMediaKeySystemSupported exit."); + return nullptr; +} + +napi_value MediaKeySystemNapi::CreateKeySession(napi_env env, napi_callback_info info) +{ + DRM_INFO_LOG("MediaKeySystemNapi::CreateKeySession enter."); + napi_status status; + napi_value result = nullptr; + size_t argc = ARGS_ONE; + napi_value argv[ARGS_ONE] = {0}; + napi_value thisVar = nullptr; + int32_t jsSecurityLevel = 0; + sptr keySessionImpl = nullptr; + MediaKeySystemNapi* mediaKeySystemNapi = nullptr; + + DRM_NAPI_GET_JS_ARGS(env, info, argc, argv, thisVar); + napi_get_undefined(env, &result); + + status = napi_get_value_int32(env, argv[PARAM0], &jsSecurityLevel); + if (status != napi_ok) { + DRM_ERR_LOG("MediaKeySystemNapi napi get jsSecurityLevel failure!"); + return nullptr; + } + // not check the securityLevel is valid. + IKeySessionService::SecurityLevel securityLevel = static_cast(jsSecurityLevel); + status = napi_unwrap(env, thisVar, reinterpret_cast(&mediaKeySystemNapi)); + + if (status == napi_ok && mediaKeySystemNapi != nullptr) { + int ret = mediaKeySystemNapi->mediaKeySystemImpl_->CreateKeySession((IKeySessionService::SecurityLevel)securityLevel, &keySessionImpl); + if (ret != DRM_OK || keySessionImpl == nullptr) { + DRM_ERR_LOG("MediaKeySystemNapi CreateKeySession get failed!!!"); + return nullptr; + } + } else { + DRM_ERR_LOG("mediaKeySystemNapi CreateKeySession call Failed!"); + return nullptr; + } + + result = KeySessionNapi::CreateKeySession(env, keySessionImpl); + DRM_INFO_LOG("MediaKeySystemNapi::CreateKeySession exit."); + return result; +} + +napi_value MediaKeySystemNapi::SetConfiguration(napi_env env, napi_callback_info info) +{ + DRM_INFO_LOG("MediaKeySystemNapi::SetConfiguration enter."); + napi_value result = nullptr; + size_t argc = ARGS_THREE; + napi_value argv[ARGS_THREE] = {0}; + napi_value thisVar = nullptr; + napi_status status; + int32_t configType = 0; + std::string name; + std::string value; + char nameBuffer[PATH_MAX]; + size_t nameBufferLen = 0; + char valueBuffer[PATH_MAX]; + size_t valueBufferLen = 0; + MediaKeySystemNapi* mediaKeySystemNapi = nullptr; + + DRM_NAPI_GET_JS_ARGS(env, info, argc, argv, thisVar); + NAPI_ASSERT(env, argc <= ARGS_THREE, "requires 3 parameters maximum"); + + status = napi_get_value_int32(env, argv[PARAM0], &configType); + if (status != napi_ok) { + DRM_ERR_LOG("Could not able to read configType argument!"); + return nullptr; + } + status = napi_get_value_string_utf8(env, argv[PARAM1], nameBuffer, PATH_MAX, &nameBufferLen); + if (status != napi_ok) { + DRM_ERR_LOG("Could not able to read name argument!"); + return nullptr; + } + name = std::string(nameBuffer); + status = napi_get_value_string_utf8(env, argv[PARAM2], valueBuffer, PATH_MAX, &valueBufferLen); + if (status != napi_ok) { + DRM_ERR_LOG("Could not able to read value argument!"); + return nullptr; + } + value = std::string(valueBuffer); + status = napi_unwrap(env, thisVar, reinterpret_cast(&mediaKeySystemNapi)); + if (status == napi_ok && mediaKeySystemNapi != nullptr) { + int ret = mediaKeySystemNapi->mediaKeySystemImpl_->SetConfiguration(IMediaKeySystemService::ConfigType(configType), name, value); + if (ret != napi_ok) { + DRM_ERR_LOG("napi SetConfiguration faild!"); + return nullptr; + } + } else { + DRM_ERR_LOG("mediaKeySystemNapi SetConfiguration call Failed!"); + return nullptr; + } + + DRM_INFO_LOG("MediaKeySystemNapi::SetConfiguration exit."); + return result; +} + +napi_value MediaKeySystemNapi::GetConfiguration(napi_env env, napi_callback_info info) +{ + DRM_INFO_LOG("MediaKeySystemNapi::GetConfiguration enter"); + napi_value result = nullptr; + size_t argc = ARGS_THREE; + napi_value argv[ARGS_THREE] = {0}; + napi_value thisVar = nullptr; + napi_status status; + int32_t configType = 0; + char nameStr[PATH_MAX]; + size_t nameStrLength = 0; + std::string value; + MediaKeySystemNapi* mediaKeySystemNapi = nullptr; + + DRM_NAPI_GET_JS_ARGS(env, info, argc, argv, thisVar); + NAPI_ASSERT(env, argc <= ARGS_THREE, "requires 3 parameters maximum"); + + status = napi_get_value_int32(env, argv[PARAM0], &configType); + if (status != napi_ok) { + DRM_ERR_LOG("Could not able to read requestType argument!"); + return nullptr; + } + + napi_get_value_string_utf8(env, argv[PARAM1], nameStr, PATH_MAX, &nameStrLength); + std::string name = std::string(nameStr); + status = napi_unwrap(env, thisVar, reinterpret_cast(&mediaKeySystemNapi)); + if (status == napi_ok && mediaKeySystemNapi != nullptr) { + int ret = mediaKeySystemNapi->mediaKeySystemImpl_->GetConfiguration(IMediaKeySystemService::ConfigType(configType), name, value); + if (ret != napi_ok) { + DRM_ERR_LOG("napi GetConfiguration faild!"); + return nullptr; + } + } else { + DRM_ERR_LOG("mediaKeySystemNapi Release call Failed!"); + return nullptr; + } + + napi_create_string_utf8(env, value.c_str(), NAPI_AUTO_LENGTH, &result); + DRM_INFO_LOG("MediaKeySystemNapi::GetConfiguration exit"); + return result; +} + +napi_value MediaKeySystemNapi::GetSecurityLevel(napi_env env, napi_callback_info info) +{ + DRM_INFO_LOG("MediaKeySystemNapi::GetSecurityLevel enter."); + napi_value result = nullptr; + size_t argc = ARGS_ONE; + napi_value argv[ARGS_ONE] = {0}; + napi_value thisVar = nullptr; + napi_status status; + MediaKeySystemNapi* mediaKeySystemNapi = nullptr; + + DRM_NAPI_GET_JS_ARGS(env, info, argc, argv, thisVar); + + NAPI_ASSERT(env, argc <= ARGS_ONE, "requires 1 parameters maximum"); + IKeySessionService::SecurityLevel level = (IKeySessionService::SecurityLevel)0; + status = napi_unwrap(env, thisVar, reinterpret_cast(&mediaKeySystemNapi)); + if (status == napi_ok && mediaKeySystemNapi != nullptr) { + mediaKeySystemNapi->mediaKeySystemImpl_->GetSecurityLevel(&level); + } else { + DRM_ERR_LOG("MediaKeySystemNapi GetSecurityLevel call Failed!"); + return nullptr; + } + + NAPI_CALL(env, napi_create_int32(env, (int)level, &result)); + DRM_INFO_LOG("MediaKeySystemNapi::GetSecurityLevel exit"); + return result; +} + +napi_value MediaKeySystemNapi::GenerateKeySystemRequest(napi_env env, napi_callback_info info) +{ + DRM_INFO_LOG("MediaKeySystemNapi::GenerateKeySystemRequest enter"); + napi_value result = nullptr; + size_t argc = ARGS_THREE; + napi_value argv[ARGS_THREE] = {0}; + napi_value thisVar = nullptr; + napi_status status; + int32_t requestType = 0; + std::vector request; + std::string defaultUrl; + napi_value keyRequestType; + napi_value mDefaultURL; + napi_value mData; + + DRM_NAPI_GET_JS_ARGS(env, info, argc, argv, thisVar); + NAPI_ASSERT(env, argc <= ARGS_THREE, "requires 3 parameters maximum"); + status = napi_get_value_int32(env, argv[PARAM0], &requestType); + if (status != napi_ok) { + DRM_ERR_LOG("Could not able to read requestType argument!"); + return nullptr; + } + MediaKeySystemNapi* mediaKeySystemNapi = nullptr; + status = napi_unwrap(env, thisVar, reinterpret_cast(&mediaKeySystemNapi)); + if (status == napi_ok && mediaKeySystemNapi != nullptr) { + int ret = mediaKeySystemNapi->mediaKeySystemImpl_->GenerateKeySystemRequest(IMediaKeySystemService::RequestType(requestType), request, defaultUrl); + if (ret != napi_ok) { + DRM_ERR_LOG("napi GenerateKeySystemRequest faild!"); + return nullptr; + } + } else { + DRM_ERR_LOG("mediaKeySystemNapi GenerateKeySystemRequest call Failed!"); + return nullptr; + } + + NAPI_CALL(env, napi_create_object(env, &result)); + NAPI_CALL(env, napi_create_int32(env, static_cast(requestType), &keyRequestType)); + size_t requestLen = request.size(); + NAPI_CALL(env, napi_create_array(env, &mData)); + for (size_t i = 0; i < requestLen; i++) { + napi_value item; + napi_create_int32(env, request[i], &item); + napi_set_element(env, mData, i, item); + } + NAPI_CALL(env, napi_create_string_utf8(env, defaultUrl.c_str(), NAPI_AUTO_LENGTH, &mDefaultURL)); + + NAPI_CALL(env, napi_set_named_property(env, result, "keyRequestType", keyRequestType)); + NAPI_CALL(env, napi_set_named_property(env, result, "mDefaultURL", mDefaultURL)); + NAPI_CALL(env, napi_set_named_property(env, result, "mData", mData)); + + DRM_INFO_LOG("MediaKeySystemNapi::GenerateKeySystemRequest exit"); + return result; +} + +napi_value MediaKeySystemNapi::ProcessKeySystemResponse(napi_env env, napi_callback_info info) +{ + DRM_INFO_LOG("MediaKeySystemNapi::ProcessKeySystemResponse enter."); + napi_value result = nullptr; + size_t argc = ARGS_TWO; + napi_value argv[ARGS_TWO] = {0}; + napi_value thisVar = nullptr; + napi_status status; + int32_t requestType = 0; + bool isTypeArray; + void *reponseData = nullptr; + size_t reponseDataLen; + size_t offset; + napi_value arraybuffer = nullptr; + napi_typedarray_type type; + MediaKeySystemNapi* mediaKeySystemNapi = nullptr; + + DRM_NAPI_GET_JS_ARGS(env, info, argc, argv, thisVar); + NAPI_ASSERT(env, argc <= ARGS_TWO, "requires 2 parameters maximum"); + status = napi_get_value_int32(env, argv[PARAM0], &requestType); + if (status != napi_ok) { + DRM_ERR_LOG("Could not able to read requestType argument!"); + return nullptr; + } + napi_is_typedarray(env, argv[PARAM1], &isTypeArray); + if (!isTypeArray) { + DRM_ERR_LOG("argv[PARAM1] reponse is not array!"); + return nullptr; + } + napi_get_typedarray_info(env, argv[PARAM1], &type, &reponseDataLen, &reponseData, &arraybuffer, &offset); + if (reponseData == nullptr) { + DRM_ERR_LOG("napi_get_typedarray_info faild!"); + return nullptr; + } + uint8_t* reponseDataPtr = reinterpret_cast(reponseData); + std::vector keySystemResponse(reponseDataPtr, reponseDataPtr + reponseDataLen); + status = napi_unwrap(env, thisVar, reinterpret_cast(&mediaKeySystemNapi)); + if (status == napi_ok && mediaKeySystemNapi != nullptr) { + int ret = mediaKeySystemNapi->mediaKeySystemImpl_->ProcessKeySystemResponse(IMediaKeySystemService::RequestType(requestType), keySystemResponse); + if (ret != napi_ok) { + DRM_ERR_LOG("napi ProcessKeySystemResponse faild!"); + return nullptr; + } + } else { + DRM_ERR_LOG("mediaKeySystemNapi ProcessKeySystemResponse call Failed!"); + return nullptr; + } + + DRM_INFO_LOG("MediaKeySystemNapi::ProcessKeySystemResponse exit."); + return result; +} + +static napi_value vectorToJsArray(napi_env env, std::vector &infoMap) +{ + DRM_INFO_LOG("vectorToJsArray enter."); + napi_value jsArray; + napi_create_array_with_length(env, infoMap.size(), &jsArray); + for (size_t i = 0; i < infoMap.size(); i++) { + napi_value jsObject; + napi_value jsName; + napi_value jsValue; + napi_create_object(env, &jsObject); + napi_create_string_utf8(env, infoMap[i].name.c_str(), NAPI_AUTO_LENGTH, &jsName); + napi_set_named_property(env, jsObject, "name", jsName); + napi_create_string_utf8(env, infoMap[i].value.c_str(), NAPI_AUTO_LENGTH, &jsValue); + napi_set_named_property(env, jsObject, "value", jsValue); + + napi_set_element(env, jsArray, i, jsObject); + } + DRM_INFO_LOG("vectorToJsArray exit."); + return jsArray; +} + +napi_value MediaKeySystemNapi::GetMetric(napi_env env, napi_callback_info info) +{ + DRM_INFO_LOG("MediaKeySystemNapi::GetMetric enter."); + napi_value result = nullptr; + size_t argc = ARGS_ZERO; + napi_value argv[ARGS_ZERO]; + napi_value thisVar = nullptr; + napi_status status; + std::vector infoMap; + MediaKeySystemNapi* mediaKeySystemNapi = nullptr; + + DRM_NAPI_GET_JS_ARGS(env, info, argc, argv, thisVar); + status = napi_unwrap(env, thisVar, reinterpret_cast(&mediaKeySystemNapi)); + if (status == napi_ok && mediaKeySystemNapi != nullptr) { + int ret = mediaKeySystemNapi->mediaKeySystemImpl_->GetMetric(infoMap); + if (ret != napi_ok) { + DRM_ERR_LOG("mediaKeySystemImpl_->GetMetric faild!"); + return nullptr; + } + } else { + DRM_ERR_LOG("mediaKeySystemNapi Release call Failed!"); + return nullptr; + } + result = vectorToJsArray(env, infoMap); + DRM_INFO_LOG("MediaKeySystemNapi::GetMetric exit."); + return result; +} + +} // namespace DrmStandard +} // namespace OHOS diff --git a/frameworks/js/drm_napi/native_module_ohos_drm.cpp b/frameworks/js/drm_napi/native_module_ohos_drm.cpp new file mode 100644 index 00000000..1f7e258e --- /dev/null +++ b/frameworks/js/drm_napi/native_module_ohos_drm.cpp @@ -0,0 +1,62 @@ +/* + * Copyright (c) 2023-2024 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#include "native_module_ohos_drm.h" + +namespace OHOS { +namespace DrmStandard { +/* + * Function registering all props and functions of ohos.media module + * which involves drm + */ +static napi_value Export(napi_env env, napi_value exports) +{ + DRM_ERR_LOG("Drm Export() is called"); + DRM_ERR_LOG("Drm DecryptModuleNapi::Init"); + DrmEnumNapi::Init(env, exports); + DRM_ERR_LOG("Drm DecryptModuleNapi::Init"); + MediaDecryptModuleNapi::Init(env, exports); + DRM_ERR_LOG("Drm KeySessionNapi::Init"); + KeySessionNapi::Init(env, exports); + DRM_ERR_LOG("Drm MediaKeySystemNapi Init"); + MediaKeySystemNapi::Init(env, exports); + DRM_ERR_LOG("DrmNapi::Init"); + DrmNapi::Init(env, exports); + return exports; +} + +/* + * module define + */ +static napi_module g_module = { + .nm_version = 1, + .nm_flags = 0, + .nm_filename = nullptr, + .nm_register_func = Export, + .nm_modname = "multimedia.drm", + .nm_priv = (reinterpret_cast(0)), + .reserved = {0} +}; + +/* + * module register + */ +extern "C" __attribute__((constructor)) void RegisterModule(void) +{ + DRM_ERR_LOG("RegisterModule() is called"); + napi_module_register(&g_module); +} + +} // namespace DrmStandard +} // namespace OHOS \ No newline at end of file diff --git a/frameworks/native/BUILD.gn b/frameworks/native/BUILD.gn new file mode 100644 index 00000000..42ba9523 --- /dev/null +++ b/frameworks/native/BUILD.gn @@ -0,0 +1,88 @@ +# for ohos_shared_library("drm_framework") + +import("//build/ohos.gni") + +config("drm_framework_public_config") { + include_dirs = [ + "//foundation/multimedia/drm_framework/interfaces/inner_api/native/drm", + "//foundation/multimedia/drm_framework/services/include", + ] +} + +config("drm_framework_local_config") { + include_dirs = [ + "//foundation/multimedia/drm_framework/services/drm_service", + "//foundation/multimedia/drm_framework/services/include", + "//foundation/multimedia/drm_framework/services/utils/include", + "//foundation/multimedia/drm_framework/services/drm_service/client/include", + "//foundation/multimedia/drm_framework/services/drm_service/ipc", + "//foundation/multimedia/drm_framework/services/drm_service/server/include", + "//foundation/multimedia/drm_framework/interfaces/inner_api/native/drm", + ] +} + +config("drm_framework_drivers_config") { + include_dirs = [ + "//foundation/multimedia/drm_framework/services/drm_service/ipc/", + ] +} + +ohos_shared_library("drm_framework") { + install_enable = true + sources = [ + "//foundation/multimedia/drm_framework/frameworks/native/drm/media_key_system_factory_impl.cpp", + "//foundation/multimedia/drm_framework/services/drm_service/client/src/mediakeysystemfactory_service_proxy.cpp", + "//foundation/multimedia/drm_framework/frameworks/native/drm/media_key_system_impl.cpp", + "//foundation/multimedia/drm_framework/services/drm_service/client/src/mediakeysystem_service_proxy.cpp", + "//foundation/multimedia/drm_framework/frameworks/native/drm/key_session_impl.cpp", + "//foundation/multimedia/drm_framework/services/drm_service/client/src/key_session_service_proxy.cpp", + "//foundation/multimedia/drm_framework/frameworks/native/drm/media_decrypt_module_impl.cpp", + "//foundation/multimedia/drm_framework/services/drm_service/client/src/media_decrypt_module_service_proxy.cpp", + "//foundation/multimedia/drm_framework/services/drm_service/server/src/key_session_service_callback_stub.cpp", + ] + + cflags = [ + "-fPIC", + "-Wall", + ] + if (target_cpu == "arm") { + cflags += [ "-DBINDER_IPC_32BIT" ] + } + sanitize = { + cfi = true + cfi_cross_dso = true + debug = true + } + + public_configs = [ + ":drm_framework_public_config", + ":drm_framework_local_config", + ":drm_framework_drivers_config", + ] + + include_dirs = [ + "//foundation/multimedia/drm_framework/frameworks/native/drm", + "//foundation/graphic/graphic_2d/frameworks/surface/include", + "//utils/system/safwk/native/include", + "//foundation//arkui/napi/interfaces/kits", + ] + + deps = [ + ] + + external_deps = [ + "c_utils:utils", + "hisysevent:libhisysevent", + "hitrace:hitrace_meter", + "hilog:libhilog", + "ipc:ipc_core", + "safwk:system_ability_fwk", + "samgr:samgr_proxy", + "napi:ace_napi", + ] + + cflags_cc = cflags + + part_name = "multimedia_drm_framework" + subsystem_name = "multimedia" +} \ No newline at end of file diff --git a/frameworks/native/drm/common_napi.h b/frameworks/native/drm/common_napi.h new file mode 100644 index 00000000..5e9fede5 --- /dev/null +++ b/frameworks/native/drm/common_napi.h @@ -0,0 +1,56 @@ +/* + * Copyright (c) 2023-2024 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#include +#include +#include "napi/native_api.h" +#include "napi/native_node_api.h" + +namespace OHOS { +namespace DrmStandard { + +class CallBackPair : public RefBase { +public: + CallBackPair(napi_env env, napi_ref callback) : env_(env), callback_(callback) { } + + ~CallBackPair() + { + if (env_ != nullptr && callback_ != nullptr) { + (void)napi_delete_reference(env_, callback_); + } + } + + napi_env GetEnv() { + return env_; + } + + napi_ref GetCallback() { + return callback_; + } + +private: + napi_env env_; + napi_ref callback_; +}; + +namespace DrmEvent { +const std::string DRM_EVENT_KEY_EXPIRED = "keyExpired"; +const std::string DRM_EVENT_KEY_SESSION_RECLAIMED = "keySessionReclaimed"; +const std::string DRM_EVENT_SYSTEM_PROVISION_REQUIRED = "keySystemProvisionRequired"; +} + +} // namespace DrmStandard +} // namespace OHOS \ No newline at end of file diff --git a/frameworks/native/drm/key_session_impl.cpp b/frameworks/native/drm/key_session_impl.cpp new file mode 100644 index 00000000..b25d2da6 --- /dev/null +++ b/frameworks/native/drm/key_session_impl.cpp @@ -0,0 +1,367 @@ +/* + * Copyright (c) 2023-2024 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#include "key_session_impl.h" +#include "common_napi.h" +#include "drm_log.h" +#include "drm_error_code.h" + +namespace OHOS { +namespace DrmStandard { + +KeySessionImpl::KeySessionImpl(sptr &keySession) +{ + DRM_DEBUG_LOG("KeySessionImpl::KeySessionImpl enter."); + keySessionServiceProxy_ = keySession; + mediaDecryptModuleImpl_ = nullptr; + keySessionServiceCallback_ = nullptr; +} + +KeySessionImpl::~KeySessionImpl() +{ + DRM_INFO_LOG("KeySessionImpl::~KeySessionImpl enter."); + keySessionServiceProxy_ = nullptr; + mediaDecryptModuleImpl_ = nullptr; + keySessionServiceCallback_ = nullptr; +} + +int32_t KeySessionImpl::Release() +{ + DRM_INFO_LOG("KeySessionImpl Release enter."); + int errCode = DRM_UNKNOWN_ERROR; + if (keySessionServiceProxy_) { + errCode = keySessionServiceProxy_->Release(); + if (errCode != DRM_OK) { + DRM_ERR_LOG("Failed to Release key session!, %{public}d", errCode); + } + } else { + DRM_ERR_LOG("KeySessionServiceProxy_ == nullptr"); + } + keySessionServiceProxy_ = nullptr; + mediaDecryptModuleImpl_ = nullptr; + keySessionServiceCallback_ = nullptr; + DRM_INFO_LOG("KeySessionImpl Release exit."); + return errCode; +} + +int KeySessionImpl::GenerateLicenseRequest(IKeySessionService::DrmInfo &drmInfo, IKeySessionService::LicenseInfo &licenseInfo) +{ + DRM_INFO_LOG("KeySessionImpl::GenerateLicenseRequest enter."); + std::lock_guard lock(mutex_); + int32_t retCode = DRM_OK; + if (keySessionServiceProxy_ == nullptr) { + DRM_ERR_LOG("KeySessionImpl::GenerateLicenseRequest keySessionServiceProxy_ is null"); + return DRM_SERVICE_ERROR; + } + retCode = keySessionServiceProxy_->GenerateLicenseRequest(drmInfo, licenseInfo); + if (retCode != DRM_OK) { + DRM_ERR_LOG("KeySessionImpl::GenerateLicenseRequest failed, retCode: %{public}d", retCode); + return DRM_SERVICE_ERROR; + } + DRM_INFO_LOG("KeySessionImpl::GenerateLicenseRequest exit"); + return DRM_OK; +} + +int KeySessionImpl::ProcessLicenseResponse(std::vector &keyId, std::vector &licenseResponse) +{ + DRM_INFO_LOG("KeySessionImpl::ProcessLicenseResponse enter."); + std::lock_guard lock(mutex_); + int32_t retCode = DRM_OK; + + if (keySessionServiceProxy_ == nullptr) { + DRM_ERR_LOG("KeySessionImpl::ProcessLicenseResponse keySessionServiceProxy_ is null"); + return DRM_SERVICE_ERROR; + } + retCode = keySessionServiceProxy_->ProcessLicenseResponse(keyId, licenseResponse); + if (retCode != DRM_OK) { + DRM_ERR_LOG("KeySessionImpl::ProcessLicenseResponse failed, retCode: %{public}d", retCode); + return DRM_SERVICE_ERROR; + } + DRM_INFO_LOG("KeySessionImpl::ProcessLicenseResponse exit"); + return DRM_OK; +} + +int KeySessionImpl::GenerateOfflineReleaseRequest(std::vector &keyId, std::vector &releaseRequest) +{ + DRM_INFO_LOG("KeySessionImpl::GenerateOfflineReleaseRequest enter."); + std::lock_guard lock(mutex_); + int32_t retCode = DRM_OK; + + if (keySessionServiceProxy_ == nullptr) { + DRM_ERR_LOG("KeySessionImpl::GenerateOfflineReleaseRequest keySessionServiceProxy_ is null"); + return DRM_SERVICE_ERROR; + } + + retCode = keySessionServiceProxy_->GenerateOfflineReleaseRequest(keyId, releaseRequest); + if (retCode != DRM_OK) { + DRM_ERR_LOG("KeySessionImpl::GenerateOfflineReleaseRequest failed, retCode: %{public}d", retCode); + return DRM_SERVICE_ERROR; + } + + DRM_INFO_LOG("KeySessionImpl::GenerateOfflineReleaseRequest exit"); + return DRM_OK; +} + +int KeySessionImpl::ProcessOfflineReleaseResponse(std::vector &keyId, std::vector &releaseResponse) +{ + DRM_INFO_LOG("KeySessionImpl::ProcessOfflineReleaseResponse enter."); + std::lock_guard lock(mutex_); + int32_t retCode = DRM_OK; + + if (keySessionServiceProxy_ == nullptr) { + DRM_ERR_LOG("KeySessionImpl::ProcessOfflineReleaseResponse keySessionServiceProxy_ is null"); + return DRM_SERVICE_ERROR; + } + retCode = keySessionServiceProxy_->ProcessOfflineReleaseResponse(keyId, releaseResponse); + if (retCode != DRM_OK) { + DRM_ERR_LOG("KeySessionImpl::ProcessOfflineReleaseResponse failed, retCode: %{public}d", retCode); + return DRM_SERVICE_ERROR; + } + DRM_INFO_LOG("KeySessionImpl::ProcessOfflineReleaseResponse exit"); + return DRM_OK; +} + +sptr KeySessionImpl::GetDecryptModule() +{ + DRM_INFO_LOG("KeySessionImpl::GetDecryptModule enter."); + if (mediaDecryptModuleImpl_ == nullptr) { + sptr decryptModuleProxy = nullptr; + sptr localDecryptModuleImpl = nullptr; + + int retCode = DRM_OK; + if (keySessionServiceProxy_ == nullptr) { + DRM_ERR_LOG("MediaDecryptModuleImpl::keySessionServiceProxy_ == nullptr"); + return nullptr; + } + retCode = keySessionServiceProxy_->CreateMediaDecryptModule(decryptModuleProxy); + if (retCode == DRM_OK) { + if (decryptModuleProxy != nullptr) { + localDecryptModuleImpl = new(std::nothrow) MediaDecryptModuleImpl(decryptModuleProxy); + if (localDecryptModuleImpl == nullptr) { + DRM_ERR_LOG("Failed to alloc new MediaDecryptModuleImpl"); + return nullptr; + } else { + mediaDecryptModuleImpl_ = localDecryptModuleImpl; + } + } else { + DRM_ERR_LOG("Failed to Create MediaDecryptModuleImpl because decryptModuleProxy nullptr"); + return nullptr; + } + } else { + DRM_ERR_LOG("Failed to keySessionServiceProxy_->CreateMediaDecryptModule!, %{public}d", retCode); + return nullptr; + } + } + DRM_INFO_LOG("KeySessionImpl::GetDecryptModule exit."); + return mediaDecryptModuleImpl_; +} + +int32_t KeySessionImpl::CheckLicenseStatus(std::vector &infoMap) +{ + DRM_INFO_LOG("KeySessionImpl::CheckLicenseStatus enter."); + std::lock_guard lock(mutex_); + int32_t retCode = DRM_OK; + + if (keySessionServiceProxy_ == nullptr) { + DRM_ERR_LOG("KeySessionImpl::CheckLicenseStatus keySessionServiceProxy_ is null"); + return DRM_SERVICE_ERROR; + } + retCode = keySessionServiceProxy_->CheckLicenseStatus(infoMap); + if (retCode != DRM_OK) { + DRM_ERR_LOG("KeySessionImpl::CheckLicenseStatus failed, retCode: %{public}d", retCode); + return DRM_SERVICE_ERROR; + } + DRM_INFO_LOG("KeySessionImpl::CheckLicenseStatus exit"); + return DRM_OK; +} + +int32_t KeySessionImpl::RestoreOfflineKeys(std::vector &keyId) +{ + DRM_ERR_LOG("KeySessionImpl::RestoreOfflineKeys enter."); + std::lock_guard lock(mutex_); + int32_t retCode = DRM_OK; + + if (keySessionServiceProxy_ == nullptr) { + DRM_ERR_LOG("KeySessionImpl::RestoreOfflineKeys keySessionServiceProxy_ is null"); + return DRM_SERVICE_ERROR; + } + retCode = keySessionServiceProxy_->RestoreOfflineKeys(keyId); + if (retCode != DRM_OK) { + DRM_ERR_LOG("KeySessionImpl::RestoreOfflineKeys failed, retCode: %{public}d", retCode); + return DRM_SERVICE_ERROR; + } + DRM_INFO_LOG("KeySessionImpl::RestoreOfflineKeys exit"); + return DRM_OK; +} + +int32_t KeySessionImpl::RemoveOfflineKeys(std::vector &keyId) +{ + DRM_INFO_LOG("KeySessionImpl::RemoveOfflineKeys enter."); + std::lock_guard lock(mutex_); + int32_t retCode = DRM_OK; + for (auto a : keyId) { + DRM_ERR_LOG("--keyId:%{public}u",a); + } + if (keySessionServiceProxy_ == nullptr) { + DRM_ERR_LOG("KeySessionImpl::RemoveOfflineKeys keySessionServiceProxy_ is null"); + return DRM_SERVICE_ERROR; + } + retCode = keySessionServiceProxy_->RemoveOfflineKeys(keyId); + if (retCode != DRM_OK) { + DRM_ERR_LOG("KeySessionImpl::RemoveOfflineKeys failed, retCode: %{public}d", retCode); + return DRM_SERVICE_ERROR; + } + DRM_INFO_LOG("KeySessionImpl::RemoveOfflineKeys exit"); + return DRM_OK; +} + +int32_t KeySessionImpl::GetOfflineKeyIds(std::vector> &keyIds) +{ + DRM_INFO_LOG("KeySessionImpl::GetOfflineKeyIds enter."); + std::lock_guard lock(mutex_); + int32_t retCode = DRM_OK; + + if (keySessionServiceProxy_ == nullptr) { + DRM_ERR_LOG("KeySessionImpl::GetOfflineKeyIds keySessionServiceProxy_ is null"); + return DRM_SERVICE_ERROR; + } + retCode = keySessionServiceProxy_->GetOfflineKeyIds(keyIds); + if (retCode != DRM_OK) { + DRM_ERR_LOG("KeySessionImpl::GetOfflineKeyIds failed, retCode: %{public}d", retCode); + return DRM_SERVICE_ERROR; + } + DRM_INFO_LOG("KeySessionImpl::GetOfflineKeyIds exit"); + return DRM_OK; +} + +int32_t KeySessionImpl::RemoveLicenses() +{ + DRM_INFO_LOG("KeySessionImpl::RemoveLicenses enter."); + std::lock_guard lock(mutex_); + int32_t retCode = DRM_OK; + + if (keySessionServiceProxy_ == nullptr) { + DRM_ERR_LOG("KeySessionImpl::RemoveLicenses keySessionServiceProxy_ is null"); + return DRM_SERVICE_ERROR; + } + retCode = keySessionServiceProxy_->RemoveLicenses(); + if (retCode != DRM_OK) { + DRM_ERR_LOG("KeySessionImpl::RemoveLicenses failed, retCode: %{public}d", retCode); + return DRM_SERVICE_ERROR; + } + DRM_INFO_LOG("KeySessionImpl::RemoveLicenses exit"); + return DRM_OK; +} + +int KeySessionImpl::GetOfflineKeyState(std::vector &keyId, IKeySessionService::OfflineKeyState &state) +{ + DRM_INFO_LOG("KeySessionImpl::GetOfflineKeyState enter."); + std::lock_guard lock(mutex_); + int32_t retCode = DRM_OK; + + if (keySessionServiceProxy_ == nullptr) { + DRM_ERR_LOG("KeySessionImpl::GetOfflineKeyState keySessionServiceProxy_ is null"); + return DRM_SERVICE_ERROR; + } + retCode = keySessionServiceProxy_->GetOfflineKeyState(keyId, state); + if (retCode != DRM_OK) { + DRM_ERR_LOG("KeySessionImpl::GetOfflineKeyState failed, retCode: %{public}d", retCode); + return DRM_SERVICE_ERROR; + } + DRM_INFO_LOG("KeySessionImpl::GetOfflineKeyState exit."); + return DRM_OK; +} + +sptr KeySessionImpl::GetKeySessionServiceProxy() +{ + DRM_INFO_LOG("KeySessionImpl::GetKeySessionServiceProxy enter."); + if (keySessionServiceProxy_ != nullptr) { + DRM_DEBUG_LOG("KeySessionImpl KeySessionServiceProxy is not nullptr"); + DRM_DEBUG_LOG("0x%{public}06" PRIXPTR " is keySessionServiceProxy's native pointer in KeySessionImpl", FAKE_POINTER(keySessionServiceProxy_.GetRefPtr())); + DRM_DEBUG_LOG("And it's count is, %{public}d", keySessionServiceProxy_->GetSptrRefCount()); + } + DRM_INFO_LOG("KeySessionImpl::GetKeySessionServiceProxy enter."); + return keySessionServiceProxy_; +} + +sptr KeySessionImpl::GetKeySessionApplicationCallback() +{ + DRM_INFO_LOG("KeySessionImpl::GetKeySessionApplicationCallback enter."); + return keySessionNapiCallback_; +} + +int32_t KeySessionImpl::SetKeySessionServiceCallback(sptr &callback) +{ + DRM_INFO_LOG("KeySessionImpl::SetKeySessionServiceCallback"); + std::lock_guard lock(mutex_); + int32_t retCode = DRM_OK; + if (keySessionServiceProxy_ == nullptr) { + DRM_ERR_LOG("KeySessionImpl::SetKeySessionServiceCallback keySessionServiceProxy_ is null"); + return DRM_SERVICE_ERROR; + } + retCode = keySessionServiceProxy_->SetKeySessionServiceCallback(callback); + if (retCode != DRM_OK) { + DRM_ERR_LOG("KeySessionImpl::SetKeySessionServiceCallback failed, retCode: %{public}d", retCode); + return DRM_SERVICE_ERROR; + } + DRM_INFO_LOG("KeySessionImpl::SetKeySessionServiceCallback exit."); + return DRM_OK; +} + +int32_t KeySessionImpl::SetKeySessionCallback(const sptr &callback) +{ + DRM_DEBUG_LOG("KeySessionImpl:0x%{public}06" PRIXPTR " SetKeySessionCallback in", FAKE_POINTER(this)); + CHECK_AND_RETURN_RET_LOG(callback != nullptr, INVALID_ARGUMENT, "callback is nullptr"); + keySessionNapiCallback_ = callback; + + int32_t errCode = DRM_ERROR; + keySessionServiceCallback_ = new(std::nothrow) KeySessionStatusCallback(this); + if (keySessionServiceCallback_ == nullptr) { + DRM_ERR_LOG("KeySessionImpl:: KeySessionStatusCallback alloc failed"); + return errCode; + } + errCode = this->SetKeySessionServiceCallback(keySessionServiceCallback_); + return errCode; +} + +int32_t KeySessionStatusCallback::OnKeySessionKeyExpired(const KeyStatus status) +{ + DRM_INFO_LOG("KeySessionStatusCallback::OnKeySessionKeyExpired enter."); + if (keySessionImpl_ != nullptr) { + sptr callback = keySessionImpl_->GetKeySessionApplicationCallback(); + if (callback != nullptr) { + callback->OnKeySessionKeyExpired(DrmEvent::DRM_EVENT_KEY_EXPIRED, status); + return DRM_SUCCESS; + } + } + DRM_ERR_LOG("KeySessionStatusCallback:: OnKeySessionKeyExpired failed"); + return DRM_ERROR; +} + +int32_t KeySessionStatusCallback::OnKeySessionReclaimed(const SessionStatus status) +{ + DRM_INFO_LOG("KeySessionStatusCallback::OnKeySessionReclaimed enter."); + if (keySessionImpl_ != nullptr) { + sptr callback = keySessionImpl_->GetKeySessionApplicationCallback(); + if (callback != nullptr) { + callback->OnKeySessionReclaimed(DrmEvent::DRM_EVENT_KEY_SESSION_RECLAIMED, status); + return DRM_SUCCESS; + } + } + DRM_ERR_LOG("KeySessionStatusCallback:: OnKeySessionReclaimed failed"); + return DRM_ERROR; +} + +} // DrmStandard +} // OHOS \ No newline at end of file diff --git a/frameworks/native/drm/media_decrypt_module_impl.cpp b/frameworks/native/drm/media_decrypt_module_impl.cpp new file mode 100644 index 00000000..a09eebb5 --- /dev/null +++ b/frameworks/native/drm/media_decrypt_module_impl.cpp @@ -0,0 +1,89 @@ +/* + * Copyright (c) 2023-2024 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#include "media_decrypt_module_impl.h" +#include "drm_log.h" +#include "drm_error_code.h" + +namespace OHOS { +namespace DrmStandard { + +MediaDecryptModuleImpl::MediaDecryptModuleImpl(sptr &decryptModule) +{ + DRM_DEBUG_LOG("Enter Init MediaDecryptModuleImpl()"); + serviceProxy_ = decryptModule; +} + +MediaDecryptModuleImpl::~MediaDecryptModuleImpl() +{ + DRM_DEBUG_LOG("~MediaDecryptModuleImpl"); + if (serviceProxy_ != nullptr) { + serviceProxy_ = nullptr; + } +} + +int32_t MediaDecryptModuleImpl::Release() +{ + DRM_INFO_LOG("MediaDecryptModuleImpl::Release enter."); + int errCode = DRM_OK; + if (serviceProxy_ != nullptr) { + errCode = serviceProxy_->Release(); + serviceProxy_ = nullptr; + } + DRM_INFO_LOG("MediaDecryptModuleImpl::Release exit."); + return errCode; +} + +int32_t MediaDecryptModuleImpl::DecryptData(bool secureDecodrtState, IMediaDecryptModuleService::CryptInfo &cryptInfo, uint64_t srcBuffer, + uint64_t dstBuffer) +{ + DRM_INFO_LOG("MediaDecryptModuleImpl::DecryptData enter"); + std::lock_guard lock(mutex_); + int32_t retCode = DRM_OK; + + if (serviceProxy_ == nullptr) { + DRM_ERR_LOG("MediaDecryptModuleImpl::DecryptData serviceProxy_ is null"); + return DRM_SERVICE_ERROR; + } + retCode = serviceProxy_->DecryptData(secureDecodrtState, cryptInfo, srcBuffer, dstBuffer); + if (retCode != DRM_OK) { + DRM_ERR_LOG("MediaDecryptModuleImpl::DecryptData failed, retCode: %{public}d", retCode); + return DRM_SERVICE_ERROR; + } + + DRM_INFO_LOG("MediaDecryptModuleImpl::DecryptData exit"); + return DRM_OK; +} + +int32_t MediaDecryptModuleImpl::RequireSecureDecoderModule(std::string &mimeType, bool *status) +{ + DRM_INFO_LOG("MediaDecryptModuleImpl::RequireSecureDecoderModule enter."); + std::lock_guard lock(mutex_); + int32_t retCode = DRM_OK; + + if (serviceProxy_ == nullptr) { + DRM_ERR_LOG("MediaDecryptModuleImpl::RequireSecureDecoderModule serviceProxy_ is null"); + return DRM_SERVICE_ERROR; + } + retCode = serviceProxy_->RequireSecureDecoderModule(mimeType, status); + if (retCode != DRM_OK) { + DRM_ERR_LOG("status: %{public}d", *status); + return retCode; + } + DRM_INFO_LOG("MediaDecryptModuleImpl::RequireSecureDecoderModule exit."); + return DRM_OK; +} + +} // DrmStandard +} // OHOS \ No newline at end of file diff --git a/frameworks/native/drm/media_key_system_factory_impl.cpp b/frameworks/native/drm/media_key_system_factory_impl.cpp new file mode 100644 index 00000000..84cacec1 --- /dev/null +++ b/frameworks/native/drm/media_key_system_factory_impl.cpp @@ -0,0 +1,179 @@ +/* + * Copyright (c) 2023-2024 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "media_key_system_factory_impl.h" +#include "i_mediakeysystem_service.h" + +namespace OHOS{ +namespace DrmStandard{ + +sptr MediaKeySystemFactoryImpl::mediaKeySystemFactoryImpl_; + +MediaKeySystemFactoryImpl::MediaKeySystemFactoryImpl() +{ + DRM_DEBUG_LOG("MediaKeySystemFactoryImpl:0x%{public}06" PRIXPTR "MediaKeySystemFactoryImpl Instances create", FAKE_POINTER(this)); + Init(); +} + +MediaKeySystemFactoryImpl::~MediaKeySystemFactoryImpl() +{ + DRM_INFO_LOG("MediaKeySystemFactoryImpl::~MediaKeySystemFactoryImpl enter."); + if (serviceProxy_ != nullptr) { + serviceProxy_ = nullptr; + } + DRM_DEBUG_LOG("MediaKeySystemFactoryImpl:0x%{public}06" PRIXPTR "MediaKeySystemFactoryImpl Instances destroy", FAKE_POINTER(this)); + DRM_INFO_LOG("MediaKeySystemFactoryImpl::~MediaKeySystemFactoryImpl exit."); + deathRecipient_ = nullptr; +} + +sptr &MediaKeySystemFactoryImpl::GetInstance() +{ + DRM_INFO_LOG("MediaKeySystemFactoryImpl::GetInstance enter."); + if (MediaKeySystemFactoryImpl::mediaKeySystemFactoryImpl_ == nullptr) { + DRM_DEBUG_LOG("Initializing MediaKeySystemFactoryImpl for first time!"); + MediaKeySystemFactoryImpl::mediaKeySystemFactoryImpl_ = new(std::nothrow) MediaKeySystemFactoryImpl(); + if (MediaKeySystemFactoryImpl::mediaKeySystemFactoryImpl_ == nullptr) { + DRM_ERR_LOG("MediaKeySystemFactoryImpl::GetInstance failed to new MediaKeySystemFactoryImpl"); + } + } + DRM_INFO_LOG("MediaKeySystemFactoryImpl::GetInstance exit."); + return MediaKeySystemFactoryImpl::mediaKeySystemFactoryImpl_; +} + +void MediaKeySystemFactoryImpl::Init() +{ + DRM_INFO_LOG("MediaKeySystemFactoryImpl::Init enter."); + sptr object = nullptr; + + auto samgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); + if (samgr == nullptr) { + DRM_ERR_LOG("Failed to get System ability manager"); + return; + } + object = samgr->GetSystemAbility(MEDIA_KEY_SYSTEM_SERVICE_ID); + if (object == nullptr) { + DRM_ERR_LOG("MediaKeySystemFactoryImpl::GetSystemAbility() is failed"); + return; + } + serviceProxy_ = iface_cast(object); + if (serviceProxy_ == nullptr) { + DRM_ERR_LOG("MediaKeySystemFactoryImpl::init serviceProxy_ is null."); + return; + } + pid_t pid = 0; + deathRecipient_ = new(std::nothrow) DrmDeathRecipient(pid); + + deathRecipient_->SetNotifyCb(std::bind(&MediaKeySystemFactoryImpl::MediaKeySystemServerDied, this, std::placeholders::_1)); + bool result = object->AddDeathRecipient(deathRecipient_); + if (!result) { + DRM_ERR_LOG("failed to add deathRecipient"); + return; + } + DRM_INFO_LOG("MediaKeySystemFactoryImpl::Init exit."); +} + +void MediaKeySystemFactoryImpl::MediaKeySystemServerDied(pid_t pid) +{ + DRM_ERR_LOG("MediaKeySystemServer has died, pid:%{public}d!", pid); + if (serviceProxy_ != nullptr) { + (void)serviceProxy_->AsObject()->RemoveDeathRecipient(deathRecipient_); + serviceProxy_ = nullptr; + } + deathRecipient_ = nullptr; +} + +bool MediaKeySystemFactoryImpl::IsMediaKeySystemSupported(std::string &uuid) +{ + DRM_INFO_LOG("MediaKeySystemFactoryImpl::IsMediaKeySystemSupported enter."); + std::lock_guard lock(mutex_); + int32_t retCode = DRM_OK; + bool isSurpported = false; + if (serviceProxy_ == nullptr) { + DRM_ERR_LOG("MediaKeySystemFactoryImpl::IsMediaKeySystemSupported serviceProxy_ is null"); + return isSurpported; + } + retCode = serviceProxy_->IsMediaKeySystemSupported(uuid, &isSurpported); + if (retCode != DRM_OK) { + DRM_ERR_LOG("MediaKeySystemFactoryImpl::IsMediaKeySystemSupported failed, retCode: %{public}d", retCode); + } + DRM_INFO_LOG("MediaKeySystemFactoryImpl::IsMediaKeySystemSupported exit."); + + return isSurpported; +} + +bool MediaKeySystemFactoryImpl::IsMediaKeySystemSupported(std::string &uuid, std::string &mimeType) +{ + DRM_INFO_LOG("MediaKeySystemFactoryImpl::IsMediaKeySystemSupported enter."); + std::lock_guard lock(mutex_); + int32_t retCode = DRM_OK; + bool isSurpported = false; + + if (serviceProxy_ == nullptr) { + DRM_ERR_LOG("MediaKeySystemFactoryImpl::IsMediaKeySystemSupported serviceProxy_ is null"); + return isSurpported; + } + retCode = serviceProxy_->IsMediaKeySystemSupported(uuid, mimeType, &isSurpported); + if (retCode != DRM_OK) { + DRM_ERR_LOG("MediaKeySystemFactoryImpl::IsMediaKeySystemSupported failed, retCode: %{public}d", retCode); + } + DRM_INFO_LOG("MediaKeySystemFactoryImpl::IsMediaKeySystemSupported exit."); + + return isSurpported; +} + +bool MediaKeySystemFactoryImpl::IsMediaKeySystemSupported(std::string &uuid, std::string &mimeType, IKeySessionService::SecurityLevel securityLevel) +{ + DRM_INFO_LOG("MediaKeySystemFactoryImpl::IsMediaKeySystemSupported enter."); + std::lock_guard lock(mutex_); + int32_t retCode = DRM_OK; + bool isSurpported = false; + + if (serviceProxy_ == nullptr) { + DRM_ERR_LOG("MediaKeySystemFactoryImpl::IsMediaKeySystemSupported serviceProxy_ is null"); + return isSurpported; + } + retCode = serviceProxy_->IsMediaKeySystemSupported(uuid, mimeType, securityLevel, &isSurpported); + if (retCode != DRM_OK) { + DRM_ERR_LOG("MediaKeySystemFactoryImpl::IsMediaKeySystemSupported failed, retCode: %{public}d", retCode); + } + DRM_INFO_LOG("MediaKeySystemFactoryImpl::IsMediaKeySystemSupported exit."); + + return isSurpported; +} + +int32_t MediaKeySystemFactoryImpl::CreateMediaKeySystem(std::string &uuid, sptr *mediaKeySystemImpl) +{ + DRM_INFO_LOG("MediaKeySystemFactoryImpl:: CreateMediaKeySystem enter."); + sptr localMediaKeySystemImpl = nullptr; + int retCode = DRM_OK; + + if (serviceProxy_ == nullptr) { + DRM_ERR_LOG("MediaKeySystemFactoryImpl:: serviceProxy_ == nullptr"); + return DRM_SERVICE_ERROR; + } + + retCode = serviceProxy_->CreateMediaKeySystem(uuid); + if (retCode == DRM_OK) { + } else { + DRM_ERR_LOG("Failed to get session object from mediakeysystem service!, %{public}d", retCode); + return DRM_SERVICE_ERROR; + } + *mediaKeySystemImpl = localMediaKeySystemImpl; + DRM_INFO_LOG("MediaKeySystemFactoryImpl:: CreateMediaKeySystem exit."); + return DRM_OK; +} + +} // namespace DrmStandard +} // namespace OHOS \ No newline at end of file diff --git a/frameworks/native/drm/media_key_system_impl.cpp b/frameworks/native/drm/media_key_system_impl.cpp new file mode 100644 index 00000000..757bd22f --- /dev/null +++ b/frameworks/native/drm/media_key_system_impl.cpp @@ -0,0 +1,202 @@ +/* + * Copyright (c) 2023-2024 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "media_key_system_impl.h" +#include "i_mediakeysystem_service.h" + +namespace OHOS{ +namespace DrmStandard{ + +MediaKeySystemImpl::MediaKeySystemImpl(sptr &mediaKeysystem) +{ + DRM_DEBUG_LOG("MediaKeySystemImpl:0x%{public}06" PRIXPTR "MediaKeySystemImpl Instances create", FAKE_POINTER(this)); + serviceProxy_ = mediaKeysystem; +} + +MediaKeySystemImpl::~MediaKeySystemImpl() +{ + serviceProxy_ = nullptr; +} + +int32_t MediaKeySystemImpl::Release() +{ + DRM_INFO_LOG("MediaKeySystemImpl::Release enter."); + int errCode = DRM_UNKNOWN_ERROR; + if (serviceProxy_ != nullptr) { + errCode = serviceProxy_->Release(); + if (errCode != DRM_OK) { + DRM_ERR_LOG("Failed to Release keySystem!, %{public}d", errCode); + return errCode; + } + serviceProxy_ = nullptr; + } else { + DRM_ERR_LOG("serviceProxy_ == nullptr"); + return -1; + } + DRM_INFO_LOG("MediaKeySystemImpl::Release exit."); + return DRM_OK; +} + +int MediaKeySystemImpl::GenerateKeySystemRequest(IMediaKeySystemService::RequestType type, std::vector &request, std::string &defaultUrl) +{ + DRM_INFO_LOG("MediaKeySystemImpl::GenerateKeySystemRequest enter. type:%{public}d.", type); + std::lock_guard lock(mutex_); + int32_t retCode = DRM_OK; + + if (serviceProxy_ == nullptr) { + DRM_ERR_LOG("MediaKeySystemImpl::GenerateKeySystemRequest serviceProxy_ is null"); + return DRM_SERVICE_ERROR; + } + retCode = serviceProxy_->GenerateKeySystemRequest(type, request, defaultUrl); + if (retCode != DRM_OK) { + DRM_ERR_LOG("MediaKeySystemImpl::GenerateKeySystemRequest failed, retCode: %{public}d", retCode); + return DRM_SERVICE_ERROR; + } + DRM_INFO_LOG("MediaKeySystemImpl::GenerateKeySystemRequest exit."); + return DRM_OK; +} + +int MediaKeySystemImpl::ProcessKeySystemResponse(IMediaKeySystemService::RequestType type, const std::vector &response) +{ + DRM_INFO_LOG("MediaKeySystemImpl::ProcessKeySystemResponse enter."); + std::lock_guard lock(mutex_); + int32_t retCode = DRM_OK; + + if (serviceProxy_ == nullptr) { + DRM_ERR_LOG("MediaKeySystemImpl::ProcessKeySystemResponse serviceProxy_ is null"); + return DRM_SERVICE_ERROR; + } + retCode = serviceProxy_->ProcessKeySystemResponse(type, response); + if (retCode != DRM_OK) { + DRM_ERR_LOG("MediaKeySystemImpl::ProcessKeySystemResponse failed, retCode: %{public}d", retCode); + return DRM_SERVICE_ERROR; + } + DRM_INFO_LOG("MediaKeySystemImpl::ProcessKeySystemResponse exit."); + return DRM_OK; +} + +int MediaKeySystemImpl::SetConfiguration(IMediaKeySystemService::ConfigType type, std::string &propertyName, std::string &value) +{ + DRM_INFO_LOG("MediaKeySystemImpl::SetConfiguration enter, configType:%{public}d, propertyName:%{public}s, value:%{public}s.", (int)type, propertyName.c_str(), value.c_str()); + std::lock_guard lock(mutex_); + int32_t retCode = DRM_OK; + + if (serviceProxy_ == nullptr) { + DRM_ERR_LOG("MediaKeySystemImpl::SetConfiguration serviceProxy_ is null"); + return DRM_SERVICE_ERROR; + } + retCode = serviceProxy_->SetConfiguration(type, propertyName, value); + if (retCode != DRM_OK) { + DRM_ERR_LOG("MediaKeySystemImpl::SetConfiguration failed, retCode: %{public}d", retCode); + return DRM_SERVICE_ERROR; + } + DRM_INFO_LOG("MediaKeySystemImpl::SetConfiguration exit."); + return DRM_OK; +} + +int MediaKeySystemImpl::GetConfiguration(IMediaKeySystemService::ConfigType configType, std::string &propertyName, std::string &value) +{ + DRM_INFO_LOG("MediaKeySystemImpl::GetConfiguration enter, configType:%{public}d, propertyName:%{public}s.", (int)configType, propertyName.c_str()); + std::lock_guard lock(mutex_); + int32_t retCode = DRM_OK; + + if (serviceProxy_ == nullptr) { + DRM_ERR_LOG("MediaKeySystemImpl::GetConfiguration serviceProxy_ is null"); + return DRM_SERVICE_ERROR; + } + retCode = serviceProxy_->GetConfiguration(configType, propertyName, value); + if (retCode != DRM_OK) { + DRM_ERR_LOG("MediaKeySystemImpl::GetConfiguration failed, retCode: %{public}d", retCode); + return DRM_SERVICE_ERROR; + } + + DRM_INFO_LOG("MediaKeySystemImpl::GetConfiguration exit, value:%{public}s.", value.c_str()); + return DRM_OK; +} + +int MediaKeySystemImpl::CreateKeySession(IKeySessionService::SecurityLevel securityLevel, sptr *keySessionImpl) +{ + DRM_INFO_LOG("MediaKeySystemImpl::CreateKeySession enter."); + sptr keySessionProxy = nullptr; + sptr localKeySessionImpl = nullptr; + int retCode = DRM_OK; + + if (serviceProxy_ == nullptr) { + DRM_ERR_LOG("MediaKeySystemImpl:: serviceProxy_ == nullptr"); + return DRM_SERVICE_ERROR; + } + + retCode = serviceProxy_->CreateKeySession(securityLevel, keySessionProxy); + if (retCode == DRM_OK) { + if (keySessionProxy != nullptr) { + localKeySessionImpl = new(std::nothrow) KeySessionImpl(keySessionProxy); + if (localKeySessionImpl == nullptr) { + DRM_ERR_LOG("Failed to new KeySessionImpl"); + return DRM_ALLOC_ERROR; + } + } else { + DRM_ERR_LOG("Failed to CreateKeySessionImpl with session is null"); + return DRM_UNKNOWN_ERROR; + } + } else { + DRM_ERR_LOG("Failed to get session object from mediakeysystem service!, %{public}d", retCode); + return DRM_SERVICE_ERROR; + } + *keySessionImpl = localKeySessionImpl; + DRM_INFO_LOG("MediaKeySystemImpl::CreateKeySession exit."); + return DRM_OK; +} + +int32_t MediaKeySystemImpl::GetMetric(std::vector &infoMap) +{ + DRM_INFO_LOG("MediaKeySystemImpl::GetMetric enter."); + std::lock_guard lock(mutex_); + int32_t retCode = DRM_OK; + + if (serviceProxy_ == nullptr) { + DRM_ERR_LOG("MediaKeySystemImpl::GetMetric serviceProxy_ is null"); + return DRM_SERVICE_ERROR; + } + retCode = serviceProxy_->GetMetric(infoMap); + if (retCode != DRM_OK) { + DRM_ERR_LOG("MediaKeySystemImpl::GetMetric failed, retCode: %{public}d", retCode); + return DRM_SERVICE_ERROR; + } + DRM_INFO_LOG("MediaKeySystemImpl::GetMetric exit."); + return DRM_OK; +} + +int MediaKeySystemImpl::GetSecurityLevel(IKeySessionService::SecurityLevel *securityLevel) +{ + DRM_INFO_LOG("MediaKeySystemImpl::GetSecurityLevel enter."); + std::lock_guard lock(mutex_); + int32_t retCode = DRM_OK; + + if (serviceProxy_ == nullptr) { + DRM_ERR_LOG("MediaKeySystemImpl::GetSecurityLevel serviceProxy_ is null"); + return DRM_SERVICE_ERROR; + } + retCode = serviceProxy_->GetSecurityLevel((IKeySessionService::SecurityLevel *)securityLevel); + DRM_ERR_LOG("MediaKeySystemImpl::GetSecurityLevel 277"); + if (retCode != DRM_OK) { + DRM_ERR_LOG("MediaKeySystemImpl::GetSecurityLevel failed, retCode: %{public}d", retCode); + return DRM_SERVICE_ERROR; + } + DRM_INFO_LOG("MediaKeySystemImpl::GetSecurityLevel exit."); + return DRM_OK; +} + +} // namespace DrmStandard +} // namespace OHOS \ No newline at end of file diff --git a/interfaces/inner_api/native/drm/key_session_impl.h b/interfaces/inner_api/native/drm/key_session_impl.h new file mode 100644 index 00000000..3e42319f --- /dev/null +++ b/interfaces/inner_api/native/drm/key_session_impl.h @@ -0,0 +1,89 @@ +/* + * Copyright (c) 2023-2024 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#ifndef OHOS_DRM_KEY_SESSION_IMPL_H_ +#define OHOS_DRM_KEY_SESSION_IMPL_H_ + +#include "media_decrypt_module_impl.h" +#include "i_keysession_service.h" +#include "i_keysession_service_callback.h" +#include "key_session_service_callback_stub.h" +#include "drm_napi_utils.h" +#include "ipc_skeleton.h" +#include "iservice_registry.h" +#include "nocopyable.h" +#include + +namespace OHOS { +namespace DrmStandard { + +class KeySessionImplCallback : public RefBase { +public: + KeySessionImplCallback() = default; + virtual ~KeySessionImplCallback() = default; + virtual void OnKeySessionKeyExpired(const std::string eventType, const KeyStatus status) = 0; + virtual void OnKeySessionReclaimed(const std::string eventType, const SessionStatus status) = 0; +}; + +class KeySessionImpl : public RefBase { +public: + explicit KeySessionImpl(sptr &keySession); + ~KeySessionImpl(); + int32_t Release(); + int32_t Init(); + sptr GetDecryptModule(); + int32_t GenerateLicenseRequest(IKeySessionService::DrmInfo &drmInfo, IKeySessionService::LicenseInfo &licenseInfo); + int32_t ProcessLicenseResponse(std::vector &keyId, std::vector &licenseResponse); + int32_t GenerateOfflineReleaseRequest(std::vector &keyId, std::vector &releaseRequest); + int32_t ProcessOfflineReleaseResponse(std::vector &keyId, std::vector &releaseReponse); + int32_t CheckLicenseStatus(std::vector &infoMap); + int32_t RestoreOfflineKeys(std::vector &keyId); + int32_t RemoveOfflineKeys(std::vector &keyId); + int32_t GetOfflineKeyIds(std::vector> &keyIds); + int32_t RemoveLicenses(); + int32_t GetOfflineKeyState(std::vector &keyId, IKeySessionService::OfflineKeyState &state); + sptr GetKeySessionServiceProxy(); + sptr GetKeySessionApplicationCallback(); + int32_t SetKeySessionCallback(const sptr &callback); + int32_t SetKeySessionServiceCallback(sptr &callback); +private: + sptr keySessionNapiCallback_; + sptr keySessionServiceCallback_; + sptr keySessionServiceProxy_; + sptr mediaDecryptModuleImpl_; + std::mutex mutex_; +}; + +class KeySessionStatusCallback : public KeySessionServiceCallbackStub { +public: + KeySessionStatusCallback() : keySessionImpl_(nullptr) {}; + explicit KeySessionStatusCallback(const sptr &KeySessionImpl) + : keySessionImpl_(KeySessionImpl) {} + + ~KeySessionStatusCallback() { + keySessionImpl_ = nullptr; + } + + int32_t OnKeySessionKeyExpired(const KeyStatus status) override; + int32_t OnKeySessionReclaimed(const SessionStatus status) override; + +private: + sptr keySessionImpl_; +}; + +} // DrmStandard +} // OHOS + + +#endif \ No newline at end of file diff --git a/interfaces/inner_api/native/drm/media_decrypt_module_impl.h b/interfaces/inner_api/native/drm/media_decrypt_module_impl.h new file mode 100644 index 00000000..23420e55 --- /dev/null +++ b/interfaces/inner_api/native/drm/media_decrypt_module_impl.h @@ -0,0 +1,45 @@ +/* + * Copyright (c) 2023-2024 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#ifndef OHOS_DRM_MEDIA_DECRYPT_MODULE_IMPL_H_ +#define OHOS_DRM_MEDIA_DECRYPT_MODULE_IMPL_H_ + +#include "ipc_skeleton.h" +#include "iservice_registry.h" +#include "nocopyable.h" +#include "i_mediadecryptmodule_service.h" +#include "drm_log.h" +#include "drm_error_code.h" +#include "drm_napi_utils.h" +#include + +namespace OHOS{ +namespace DrmStandard{ + +class MediaDecryptModuleImpl : public RefBase { +public: + explicit MediaDecryptModuleImpl(sptr &decryptModule); + ~MediaDecryptModuleImpl(); + int32_t Release(); + int32_t DecryptData(bool secureDecodrtState, IMediaDecryptModuleService::CryptInfo &cryptInfo, uint64_t srcBuffer, uint64_t dstBuffer); + int32_t RequireSecureDecoderModule(std::string &mimeType, bool *status); +private: + std::mutex mutex_; + sptr serviceProxy_; +}; + +} // DrmStandard +} // OHOS + +#endif \ No newline at end of file diff --git a/interfaces/inner_api/native/drm/media_key_system_factory_impl.h b/interfaces/inner_api/native/drm/media_key_system_factory_impl.h new file mode 100644 index 00000000..7977aa51 --- /dev/null +++ b/interfaces/inner_api/native/drm/media_key_system_factory_impl.h @@ -0,0 +1,54 @@ +/* + * Copyright (c) 2023-2024 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef OHOS_DRM_MEDIA_KEY_SYSTEMP_FACTORY_IMPL_H +#define OHOS_DRM_MEDIA_KEY_SYSTEMP_FACTORY_IMPL_H + +#include "media_key_system_impl.h" +#include "system_ability_definition.h" +#include "ipc_skeleton.h" +#include "iservice_registry.h" +#include "nocopyable.h" +#include "i_mediakeysystemfactory_service.h" +#include "drm_death_recipient.h" +#include "drm_log.h" +#include "drm_error_code.h" +#include "drm_napi_utils.h" + +namespace OHOS{ +namespace DrmStandard{ + +class MediaKeySystemFactoryImpl : public RefBase { +public: + MediaKeySystemFactoryImpl(); + ~MediaKeySystemFactoryImpl(); + void Init(); + static sptr &GetInstance(); + bool IsMediaKeySystemSupported(std::string &uuid); + bool IsMediaKeySystemSupported(std::string &uuid, std::string &mimeType); + bool IsMediaKeySystemSupported(std::string &uuid, std::string &mimeType, IKeySessionService::SecurityLevel securityLevel); + int32_t CreateMediaKeySystem(std::string &uuid, sptr *mediaKeySystemImpl); + +private: + void MediaKeySystemServerDied(pid_t pid); + std::mutex mutex_; + static sptr mediaKeySystemFactoryImpl_; + sptr serviceProxy_; + sptr deathRecipient_; +}; + +} // DrmStandard +} //OHOS +#endif // OHOS_DRM_MEDIA_KEY_SYSTEMP_FACTORY_IMPL_H \ No newline at end of file diff --git a/interfaces/inner_api/native/drm/media_key_system_impl.h b/interfaces/inner_api/native/drm/media_key_system_impl.h new file mode 100644 index 00000000..0a57c264 --- /dev/null +++ b/interfaces/inner_api/native/drm/media_key_system_impl.h @@ -0,0 +1,53 @@ +/* + * Copyright (c) 2023-2024 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef OHOS_DRM_MEDIA_KEY_SYSTEMP_IMPL_H +#define OHOS_DRM_MEDIA_KEY_SYSTEMP_IMPL_H + +#include "key_session_impl.h" +#include "system_ability_definition.h" +#include "ipc_skeleton.h" +#include "iservice_registry.h" +#include "nocopyable.h" +#include "i_mediakeysystem_service.h" +#include "drm_death_recipient.h" +#include "drm_log.h" +#include "drm_error_code.h" +#include "drm_napi_utils.h" + +namespace OHOS{ +namespace DrmStandard{ + +class MediaKeySystemImpl : public RefBase { +public: + explicit MediaKeySystemImpl(sptr &mediaKeysystem); + ~MediaKeySystemImpl(); + int32_t Release(); + int32_t GenerateKeySystemRequest(IMediaKeySystemService::RequestType type, std::vector &request, std::string &defaultUrl); + int32_t ProcessKeySystemResponse(IMediaKeySystemService::RequestType type, const std::vector &response); + int32_t SetConfiguration(IMediaKeySystemService::ConfigType type, std::string &propertyName, std::string &value); + int32_t GetConfiguration(IMediaKeySystemService::ConfigType configType, std::string &propertyName, std::string &value); + int32_t CreateKeySession(IKeySessionService::SecurityLevel securityLevel, sptr *keySessionImpl); + int32_t GetMetric(std::vector &infoMap); + int32_t GetSecurityLevel(IKeySessionService::SecurityLevel *securityLevel); + +private: + std::mutex mutex_; + sptr serviceProxy_; +}; + +} // DrmStandard +} //OHOS +#endif // OHOS_DRM_MEDIA_KEY_SYSTEMP_IMPL_H \ No newline at end of file diff --git a/interfaces/kits/js/drm_napi/@ohos.multimedia.drm.d.ts b/interfaces/kits/js/drm_napi/@ohos.multimedia.drm.d.ts new file mode 100644 index 00000000..cd3ca736 --- /dev/null +++ b/interfaces/kits/js/drm_napi/@ohos.multimedia.drm.d.ts @@ -0,0 +1,531 @@ +/* +* Copyright (C) 2021 Huawei Device Co., Ltd. +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +import {ErrorCallback, AsyncCallback, Callback} from './basic'; +import { Context } from './app/context'; + +/** + * @name drm + * @since 8 + */ +declare namespace drm { + /** + * Creates a Mediakeysystem instance. + * @since 8 + * @syscap SystemCapability.Multimedia.Drm.Core + * @param uuid Used to point a Digital Right Managements solution. + * @param callback Callback used to return the Mediakeysystem instance. + */ + function getMediaKeySystem(uuid: string, callback: AsyncCallback): void; + + /** + * Creates a MediaKeySystem instance. + * @since 8 + * @syscap SystemCapability.Multimedia.Drm.Core + * @param uuid Used to point a Digital Right Managements solution. + * @returns Promise used to return the MediaKeySystem instance. + */ + function getMediaKeySystem(uuid: string): Promise; + + /** + * Judge whether a system that specifies UUID, mimetype and security level is supported. + * @since 8 + * @syscap SystemCapability.Multimedia.Drm.Core + * @param uuid Used to point a Digital Right Managements solution. + * @param mimeType Used to specifies the meida type. + * @param level Used to specifies the SecurityLevel. + * @returns Whether these conditions will be met. + */ + function isMediaKeySystemSupported(uuid: string, mimeType: string, level: SecurityLevel): boolean; + + /** + * Judge whether a system that specifies UUID, mimetype and security level is supported. + * @param uuid Used to point a Digital Right Managements solution. + * @param mimeType Used to specifies the meida type. + * @returns Whether these conditions will be met. + * @since 8 + * @syscap SystemCapability.Multimedia.Drm.Core + */ + function isMediaKeySystemSupported(uuid: string, mimeType: string): boolean; + + /** + * Judge whether a system that specifies UUID, mimetype and security level is supported. + * @param uuid Used to point a Digital Right Managements solution. + * @returns Whether these conditions will be met. + * @since 8 + * @syscap SystemCapability.Multimedia.Drm.Core + */ + function isMediaKeySystemSupported(uuid: string): boolean; + + /** + * Manages and record key sessions. Before calling an MediaKeySystem method, we must use getMediaKeySystem + * to get a MediaKeySystem instance, then we can call functions. + * @since 8 + * @syscap SystemCapability.Multimedia.Drm.Core + */ + interface MediaKeySystem { + + /** + * Get the specified configuration. + * @since 8 + * @syscap SystemCapability.Multimedia.Drm.Core + * @param configType Used to specify the config type. + * @param propertyName Used to specify the property name. + * @returns The result. + */ + getConfiguration(configType: ConfigType, propertyName: PropertyName): string; + + /** + * Set the specified configuration. + * @since 8 + * @syscap SystemCapability.Multimedia.Drm.Core + * @param configType Used to specify the config type. + * @param propertyName Used to specify the property name. + * @param value The value to be set. + */ + setConfiguration(configType: ConfigType, propertyName: PropertyName, value: string): void; + + /** + * Generate the media key system provision request. + * @since 8 + * @syscap SystemCapability.Multimedia.Drm.Core + * @param type Used to specify the request type. + * @returns Promise used to return a DrmRequest + */ + generateKeySystemRequest(type: RequestType): Promise; + + /** + * Process the response corresponding the key system request obtained by the application. + * @since 8 + * @syscap SystemCapability.Multimedia.Drm.Core + * @param type Used to specify the request type. + * @param response Response corresponding to the request. + */ + processKeySystemResponse(type: RequestType, response: Uint8Array): Promise; + + /** + * Create a key session instance. + * @since 8 + * @syscap SystemCapability.Multimedia.Drm.Core + * @param level Used to specify the Security level. + * @returns Promise used to return a KeySession instance. + */ + createKeySession(level: SecurityLevel): Promise; + + /** + * Get performance statistics information.That includes currentSessionNum, version, decryptNumber, + * and errorDecryptNumber. + * @since 8 + * @syscap SystemCapability.Multimedia.Drm.Core + * @returns A map that includes perfaormence index and corresponding statistics. + */ + getMetric(): {[key: string]: string}; + + /** + * Get security level. + * @since 8 + * @syscap SystemCapability.Multimedia.Drm.Core + * @returns Promise used to return SecurityLevel. + */ + getSecurityLevel(): Promise; + } + + /** + * Provide functions and keep a decrypt module. Before calling an KeySession method, we must use MediaKeySystem's + * createKeySession to get a key session instance. + * @since 8 + * @syscap SystemCapability.Multimedia.Drm.Core + */ + interface KeySession { + + /** + * Generate the license request. + * @since 8 + * @syscap SystemCapability.Multimedia.Drm.Core + * @param mimeType Media type. + * @param initData pssh after base64. + * @param keyType Offline or online. + */ + generateLicenseRequest(mimeType: string, initData: Uint8Array, keyType: number): Promise; + + /** + * Process the response corresponding the license request obtained by the application. + * @since 8 + * @syscap SystemCapability.Multimedia.Drm.Core + * @param response The response. + * @returns Return keyID. + */ + processLicenseResponse(response: Uint8Array): Promise; + + /** + * Check the license status + * @since 8 + * @syscap SystemCapability.Multimedia.Drm.Core + * @returns License status pair. + */ + checkLicenseStatus(): Promise; + + /** + * Release the resource before the session gonna be unused. + * @since 8 + * @syscap SystemCapability.Multimedia.Drm.Core + */ + release(): void; + + /** + * Remove license. + * @since 8 + * @syscap SystemCapability.Multimedia.Drm.Core + */ + removeLicenses(): void; + + /** + * Generate offline license request. + * @since 8 + * @syscap SystemCapability.Multimedia.Drm.Core + * @param keyId The keyId specifies which media content's license request should be generated. + * @returns Return the license request. + */ + generateOfflineReleaseRequest(keyId: Uint8Array): Promise; + + /** + * Process offline license response. + * @since 8 + * @syscap SystemCapability.Multimedia.Drm.Core + * @param keyId The keyId specifies which media content's license it is + * @param response The offline license. + */ + processOfflineReleaseResponse(keyId: Uint8Array, response: Uint8Array): void; + + /** + * Restore offline license response. + * @since 8 + * @syscap SystemCapability.Multimedia.Drm.Core + * @param keyId The keyId specifies which license should be restore. + */ + restoreOfflineKeys(keyId: Uint8Array): void; + + /** + * Get the list of offline keyIds. + * @since 8 + * @syscap SystemCapability.Multimedia.Drm.Core + * @returns The list of offline keyIds. + */ + getOfflineKeyIds(): Uint8Array[]; + + /** + * Remove license corresponding to the keyId. + * @since 8 + * @syscap SystemCapability.Multimedia.Drm.Core + * @param keyId The keyId specifies which license should be remove. + */ + removeOfflineKeys(keyId: Uint8Array): void; + + /** + * Get offline license status corresponding to the keyId. + * @since 8 + * @syscap SystemCapability.Multimedia.Drm.Core + * @param keyId The keyId specifies license. + * @returns OfflineKeyState. + */ + getOfflineKeyState(keyId: Uint8Array): Promise; + + /** + * Get decrypt module. + * @since 8 + * @syscap SystemCapability.Multimedia.Drm.Core + * @returns Return the media decrypt module instance. + */ + getDecryptModule(): Promise; + + /** + * Register or unregister listens for drm events. + * @since 8 + * @syscap SystemCapability.Multimedia.Drm.Core + * @param type Type of the drm event to listen for. + * @param callback Callback used to listen for the key system required event. + */ + on(type: 'keySystemRequired', callback: Callback): void; + off(type: 'keySystemRequired'): void; + + /** + * Register or unregister listens for drm events. + * @since 8 + * @syscap SystemCapability.Multimedia.Drm.Core + * @param type Type of the drm event to listen for. + * @param callback Callback used to listen for the key required event. + */ + on(type: 'keyRequired', callback: Callback): void; + off(type: 'keyRequired'): void; + + /** + * Register or unregister listens for drm events. + * @since 8 + * @syscap SystemCapability.Multimedia.Drm.Core + * @param type Type of the drm event to listen for. + * @param callback Callback used to listen for the key required event. + */ + on(type: 'keyExpired', callback: Callback): void; + off(type: 'keyExpired'): void; + + /** + * Register or unregister listens for drm events. + * @since 8 + * @syscap SystemCapability.Multimedia.Drm.Core + * @param type Type of the drm event to listen for. + * @param callback Callback used to listen for the vendor defined event. + */ + on(type: 'vendorDefined', callback: Callback): void; + off(type: 'vendorDefined'): void; + + /** + * Register or unregister listens for drm events. + * @since 8 + * @syscap SystemCapability.Multimedia.Drm.Core + * @param type Type of the drm event to listen for. + * @param callback Callback used to listen for keySession reclaimed event. + */ + on(type: 'keySessionReclaimed', callback: Callback): void; + off(type: 'keySessionReclaimed'): void; + } + + /** + * Provide decrypt functions. Before we gonna use a decrypt module, we shoud call getDecryptModule. + * @since 8 + * @syscap SystemCapability.Multimedia.Drm.Core + */ + interface MediaDecryptModule { + + /** + * When the decrypt content need a secure decoder, return true, otherwise return false. + * @since 8 + * @syscap SystemCapability.Multimedia.Drm.Core + * @param mimeType The media content type. + * @returns Whether secure decoder is needed. + */ + requireSecureDecoderModule(mimeType: string): boolean; + + /** + * Decrypt content. + * @since 8 + * @syscap SystemCapability.Multimedia.Drm.Core + * @param secureDecoderStatus Secure decoder status. + * @param cryptInfo Crypt info. + * @param srcBuffer Source buffer. + * @param dstBuffer Destination buffer. + */ + decryptData(secureDecoderStatus: boolean, cryptInfo: CryptInfo, srcBuffer: number, + dstBuffer: number): Promise; + + /** + * Release the resource that a decrypt module owned. When a decrypt module will be unused, + * we should call this function. + * @since 8 + * @syscap SystemCapability.Multimedia.Drm.Core + */ + release(): void; + } + + /** + * Enumerates event types of listener. + * @since 8 + * @syscap SystemCapability.Multimedia.Drm.Core + */ + enum ListenerType { + LISTENER_DRM_EVENT = 200, + LISTENER_PROVISION_REQUIRED = 201, + LISTENER_KEY_NEEDED = 202, + LISTENER_KEY_EXPIRED = 203, + LISTENER_VENDOR_DEFINED = 204, + LISTENER_KEYSESSION_RECLAIMED = 205, + LISTENER_EXPIRATION_UPDATE = 206, + LISTENER_KEY_CHANGE = 207, + LISTENER_KEYSESSION_LOSE = 208, + } + + /** + * Enumerates which properties we can get. + * @since 8 + * @syscap SystemCapability.Multimedia.Drm.Core + */ + enum PropertyName { + PROPERTY_DEVICE_VENDOR = "vendor", + PROPERTY_DEVICE_VERSION = "version", + PROPERTY_DEVICE_DESCRIPTION = "description", + PROPERTY_DEVICE_ALGORITHMS = "algorithms", + PROPERTY_DEVICE_UNIQUE_ID = "deviceUniqueId", + PROPERTY_SESSION_MAX = "maxSessionNum", + PROPERTY_SESSION_CURRENT = "currentSessionNum", + PROPERTY_OUTPUT_HDCP_MAX = "maxHDCPLevel", + PROPERTY_OUTPUT_HDCP_CURRENT = "currentHDCPLevel", + } + + /** + * Enumerates hdcp level. + * @since 8 + * @syscap SystemCapability.Multimedia.Drm.Core + */ + enum HdcpLevel { + HDCP_UNKNOWN, + HDCP_NONE, + HDCP_V1, + HDCP_V2, + HDCP_V2_1, + HDCP_V2_2, + HDCP_V2_3, + HDCP_NO_OUTPUT = 0x7fff, + } + + /** + * Enumerates which configurations. + * @since 8 + * @syscap SystemCapability.Multimedia.Drm.Core + */ + enum ConfigType { + CONFIGTYPE_DEVICEPROPERTY = 0, + CONFIGTYPE_KEYSESSION = 1, + CONFIGTYPE_OUTPUTPROTECTTYPE = 2, + } + + /** + * Enumerates license type. + * @since 8 + * @syscap SystemCapability.Multimedia.Drm.Core + */ + enum KeyType { + KEYTYPE_OFFLINE = 0, + KEYTYPE_ONLINE, + } + + /** + * Enumerates offline license state. + * @since 8 + * @syscap SystemCapability.Multimedia.Drm.Core + */ + enum OfflineKeyState { + OFFLINEKEYSTATE_UNKNOWN = 0, + OFFLINEKEYSTATE_USABLE = 1, + OFFLINEKEYSTATE_INACTIVE = 2, + } + + /** + * Enumerates license request types. + * @since 8 + * @syscap SystemCapability.Multimedia.Drm.Core + */ + enum RequestType { + REQUEST_TYPE_UNKNOWN = 0, + REQUEST_TYPE_INITIAL = 1, + REQUEST_TYPE_RENEWAL = 2, + REQUEST_TYPE_RELEASE = 3, + REQUEST_TYPE_NONE = 4, + REQUEST_TYPE_UPDATE = 5, + REQUEST_TYPE_DOWNLOADCERT = 6, + } + + /** + * Enumerates security level. + * @since 8 + * @syscap SystemCapability.Multimedia.Drm.Core + */ + enum SecurityLevel { + SECURITY_LEVEL_UNKNOWN = 0, + SECURITY_LEVEL_SW_CRYPTO = 1, + SECURITY_LEVEL_SW_DECODE = 2, + SECURITY_LEVEL_HW_CRYPTO = 3, + SECURITY_LEVEL_HW_DECODE = 4, + SECURITY_LEVEL_HW_ALL = 5, + SECURITY_LEVEL_MAX = 6, + } + + /** + * Provides the drm request definations. + * @since 8 + * @syscap SystemCapability.Multimedia.Drm.Core + */ + interface DrmRequest { + keyRequestType: RequestType; + mData: Uint8Array; + mDefaultURL: string; + } + + /** + * Used to indicates the license status with a key and its value. + * @since 8 + * @syscap SystemCapability.Multimedia.Drm.Core + */ + interface KeyValue { + name: string; + value: string; + } + + /** + * Used to indicates the length of clear bytes and decrypt length in a subsample. + * @since 8 + * @syscap SystemCapability.Multimedia.Drm.Core + */ + interface SubSample { + clearHeaderLen: number; + payLoadLen: number; + } + + /** + * Decryption pattern. + * @since 8 + * @syscap SystemCapability.Multimedia.Drm.Core + */ + interface Pattern { + encryptBlocks: number; + skipBlocks: number; + } + + /** + * Enumerates encryption Algorithm types. + * @since 8 + * @syscap SystemCapability.Multimedia.Drm.Core + */ + enum CryptAlgorithmType { + ALGTYPE_UNENCRYPTED = 0, + ALGTYPE_AES_CTR = 1, + ALGTYPE_AES_WV = 2, + ALGTYPE_AES_CBC = 3, + ALGTYPE_SM4_CBC = 4, + } + + /** + * Provides the defination of encryption info. + * @since 8 + * @syscap SystemCapability.Multimedia.Drm.Core + */ + interface CryptInfo { + type: CryptAlgorithmType; + keyId: Uint8Array; + iv: Uint8Array; + pattern: Pattern; + subSample: SubSample[]; + } + + /** + * Provides the defination of decryption data buffer. + * @since 8 + * @syscap SystemCapability.Multimedia.Drm.Core + */ + interface DecryptDataBuffer { + bufferLen: number; + buffer: ArrayBuffer; + } +} + +export default drm; \ No newline at end of file diff --git a/interfaces/kits/js/drm_napi/BUILD.gn b/interfaces/kits/js/drm_napi/BUILD.gn new file mode 100644 index 00000000..5679c97e --- /dev/null +++ b/interfaces/kits/js/drm_napi/BUILD.gn @@ -0,0 +1,65 @@ +import("//build/ohos.gni") +import("//build/ohos/ace/ace.gni") + +js_declaration("drm_js") { + part_name = "multimedia_drm_framework" + sources = [ "./@ohos.multimedia.drm.d.ts" ] +} + +ohos_copy("drm_declaration") { + sources = [ "./@ohos.multimedia.drm.d.ts" ] + outputs = [ target_out_dir + "/$target_name/" ] + module_source_dir = target_out_dir + "/$target_name" + module_install_name = "" +} + +ohos_shared_library("drm_napi") { + include_dirs = [ + "//foundation/multimedia/drm_framework/frameworks/js/common", + "//foundation/multimedia/drm_framework/frameworks/js/drm_napi", + "//foundation/multimedia/drm_framework/frameworks/native/drm", + "//foundation/multimedia/drm_framework/frameworks/native/common", + "//foundation/multimedia/drm_framework/interfaces/inner_api/native/drm", + "//foundation/multimedia/drm_framework/interfaces/kits/js/drm_napi/include", + "//foundation/multimedia/drm_framework/services/drm_service/", + "//foundation/multimedia/drm_framework/services/drm_service/client/include", + "//foundation/multimedia/drm_framework/services/drm_service/server/include", + "//foundation/multimedia/drm_framework/services/drm_service/ipc", + "//foundation/multimedia/drm_framework/services/utils/include", + "//foundation/multimedia/graphic/graphic_2d/interfaces/inner_api/surface", + "//foundation//arkui/napi/interfaces/kits", + ] + sources = [ + "//foundation/multimedia/drm_framework/frameworks/js/drm_napi/key_session_callback_napi.cpp", + "//foundation/multimedia/drm_framework/frameworks/js/drm_napi/media_key_system_callback_napi.cpp", + "//foundation/multimedia/drm_framework/frameworks/js/drm_napi/drm_napi.cpp", + "//foundation/multimedia/drm_framework/frameworks/js/drm_napi/drm_enum_napi.cpp", + "//foundation/multimedia/drm_framework/frameworks/js/drm_napi/media_key_system_napi.cpp", + "//foundation/multimedia/drm_framework/frameworks/js/drm_napi/key_session_napi.cpp", + "//foundation/multimedia/drm_framework/frameworks/js/drm_napi/media_decrypt_module_napi.cpp", + "//foundation/multimedia/drm_framework/frameworks/js/drm_napi/native_module_ohos_drm.cpp", + ] + deps = [ + "//foundation/multimedia/drm_framework/frameworks/native:drm_framework", + ] + external_deps = [ + "access_token:libtokenid_sdk", + "c_utils:utils", + "graphic_2d:surface", + "hisysevent:libhisysevent", + "hitrace:hitrace_meter", + "hilog:libhilog", + "ipc:ipc_core", + "napi:ace_napi", + ] + sanitize = { + cfi = true + cfi_cross_dso = true + debug = true + } + cflags = [ "-fPIC" ] + cflags_cc = cflags + relative_install_dir = "module/multimedia" + part_name = "multimedia_drm_framework" + subsystem_name = "multimedia" +} diff --git a/interfaces/kits/js/drm_napi/include/drm_enum_napi.h b/interfaces/kits/js/drm_napi/include/drm_enum_napi.h new file mode 100644 index 00000000..c1e13c3e --- /dev/null +++ b/interfaces/kits/js/drm_napi/include/drm_enum_napi.h @@ -0,0 +1,38 @@ +/* + * Copyright (c) 2023-2024 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#ifndef OHOS_DRM_ENUM_NAPI_H_ +#define OHOS_DRM_ENUM_NAPI_H_ + +#include "key_session_napi.h" +#include "media_decrypt_module_napi.h" +#include "media_key_system_napi.h" +#include "drm_log.h" +#include "drm_error_code.h" + +namespace OHOS { +namespace DrmStandard { +class DrmEnumNapi { +public: + DrmEnumNapi() = default; + ~DrmEnumNapi() = default; + static napi_value Init(napi_env env, napi_value exports); + +private: + static napi_value JsEnumIntInit(napi_env env, napi_value exports); + static napi_value JsEnumStringInit(napi_env env, napi_value exports); +}; +} // namespace DrmStandard +} // namespace OHOS +#endif // OHOS_DRM_ENUM_NAPI_H_ \ No newline at end of file diff --git a/interfaces/kits/js/drm_napi/include/drm_napi.h b/interfaces/kits/js/drm_napi/include/drm_napi.h new file mode 100644 index 00000000..69e43347 --- /dev/null +++ b/interfaces/kits/js/drm_napi/include/drm_napi.h @@ -0,0 +1,46 @@ +/* + * Copyright (c) 2023-2024 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#ifndef OHOS_DRM_DRM_NAPI_H_ +#define OHOS_DRM_DRM_NAPI_H_ + +#include "media_key_system_napi.h" +#include "key_session_napi.h" +#include "drm_log.h" +#include "drm_error_code.h" +#include "drm_napi_utils.h" + +namespace OHOS { +namespace DrmStandard { + +static const char DRM_NAPI_CLASS_NAME[] = "drm"; + +class DrmNapi { +public: + DrmNapi(); + ~DrmNapi(); + static napi_value Init(napi_env env, napi_value exports); + static napi_value CreateMediaKeySystemInstance(napi_env env, napi_callback_info info); + static napi_value IsMediaKeySystemSupported(napi_env env, napi_callback_info info); +private: + static napi_value DrmNapiConstructor(napi_env env, napi_callback_info info); + static void DrmNapiDestructor(napi_env env, void *nativeObject, void *finalize); + static thread_local napi_ref sConstructor_; + napi_env env_; + napi_ref wrapper_; +}; + +} // DrmStandard +} // OHOS +#endif // OHOS_DRM_DRM_NAPI_H_ \ No newline at end of file diff --git a/interfaces/kits/js/drm_napi/include/key_session_callback_napi.h b/interfaces/kits/js/drm_napi/include/key_session_callback_napi.h new file mode 100644 index 00000000..efb3dda6 --- /dev/null +++ b/interfaces/kits/js/drm_napi/include/key_session_callback_napi.h @@ -0,0 +1,45 @@ +/* + * Copyright (c) 2023-2024 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef OHOS_DRM_KeySessionCallbackNapi_H_ +#define OHOS_DRM_KeySessionCallbackNapi_H_ + +#include "key_session_impl.h" +#include "napi/native_api.h" +#include "napi/native_node_api.h" +#include "common_napi.h" + +namespace OHOS { +namespace DrmStandard { + +class KeySessionCallbackNapi : public KeySessionImplCallback { +public: + explicit KeySessionCallbackNapi(); + virtual ~KeySessionCallbackNapi(); + void SetCallbackReference(const std::string eventType, sptr callbackPair); + void ClearCallbackReference(const std::string eventType); + void OnKeySessionKeyExpired(const std::string eventType, const KeyStatus status) override; + void OnKeySessionReclaimed(const std::string eventType, const SessionStatus status) override; + +private: + std::mutex mutex_; + std::map> callbackMap_; +}; + +} // namespace DrmStandard +} // namespace OHOS + + +#endif \ No newline at end of file diff --git a/interfaces/kits/js/drm_napi/include/key_session_napi.h b/interfaces/kits/js/drm_napi/include/key_session_napi.h new file mode 100644 index 00000000..6054c97d --- /dev/null +++ b/interfaces/kits/js/drm_napi/include/key_session_napi.h @@ -0,0 +1,73 @@ +/* + * Copyright (c) 2023-2024 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#ifndef OHOS_DRM_KEY_SESSION_H_ +#define OHOS_DRM_KEY_SESSION_H_ + +#include "key_session_impl.h" +#include "napi/native_api.h" +#include "napi/native_node_api.h" +#include "key_session_callback_napi.h" +#include "drm_log.h" +#include "drm_napi_utils.h" +#include "drm_error_code.h" + +#include + +namespace OHOS { +namespace DrmStandard { +static const char KEY_SESSION_NAPI_CLASS_NAME[] = "KeySession"; +class KeySessionNapi { +public: + KeySessionNapi(); + ~KeySessionNapi(); + static napi_value Init(napi_env env, napi_value exports); + static napi_value CreateKeySession(napi_env env, sptr keySessionImpl); + static napi_value Release(napi_env env, napi_callback_info info); + static napi_value GenerateLicenseRequest(napi_env env, napi_callback_info info); + static napi_value ProcessLicenseResponse(napi_env env, napi_callback_info info); + static napi_value GenerateOfflineReleaseRequest(napi_env env, napi_callback_info info); + static napi_value ProcessOfflineReleaseResponse(napi_env env, napi_callback_info info); + static napi_value CheckLicenseStatus(napi_env env, napi_callback_info info); + static napi_value RestoreOfflineKeys(napi_env env, napi_callback_info info); + static napi_value RemoveOfflineKeys(napi_env env, napi_callback_info info); + static napi_value GetOfflineKeyIds(napi_env env, napi_callback_info info); + static napi_value RemoveLicenses(napi_env env, napi_callback_info info); + static napi_value GetDecryptModule(napi_env env, napi_callback_info info); + static napi_value AddEventListener(napi_env env, napi_callback_info info); + static napi_value DeleteEventListener(napi_env env, napi_callback_info info); + static napi_value GetOfflineKeyState(napi_env env, napi_callback_info info); + static napi_value SetEventCallback(napi_env env, napi_callback_info info); + static napi_value UnsetEventCallback(napi_env env, napi_callback_info info); + static bool SetKeySessionNativeProperty(napi_env env, napi_value obj, const std::string &name, sptr keySessionImpl); + void SetEventCallbackReference(const std::string eventType, sptr callbackPair); + void ClearEventCallbackReference(const std::string eventType); +private: + static napi_value KeySessionNapiConstructor(napi_env env, napi_callback_info info); + static void KeySessionNapiDestructor(napi_env env, void *nativeObject, void *finalize); + + napi_env env_; + napi_ref wrapper_; + static thread_local napi_ref sConstructor_; + std::mutex mutex_; + static thread_local sptr sKeySessionImpl_; + sptr keySessionImpl_; + sptr keySessionCallbackNapi_; +}; + +} // DrmStandard +} // OHOS + + +#endif // OHOS_DRM_KEY_SESSION_H_ \ No newline at end of file diff --git a/interfaces/kits/js/drm_napi/include/media_decrypt_module_napi.h b/interfaces/kits/js/drm_napi/include/media_decrypt_module_napi.h new file mode 100644 index 00000000..17cb7c5c --- /dev/null +++ b/interfaces/kits/js/drm_napi/include/media_decrypt_module_napi.h @@ -0,0 +1,53 @@ +/* + * Copyright (c) 2023-2024 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#ifndef OHOS_DRM_DECYPT_MODULE_H_ +#define OHOS_DRM_DECYPT_MODULE_H_ + +#include "media_decrypt_module_impl.h" +#include "napi/native_api.h" +#include "napi/native_node_api.h" +#include "drm_log.h" +#include "drm_napi_utils.h" +#include "drm_error_code.h" + +namespace OHOS { +namespace DrmStandard { +static const char DECYPT_MODULE_NAPI_CLASS_NAME[] = "decryptModule"; +class MediaDecryptModuleNapi { +public: + MediaDecryptModuleNapi(); + ~MediaDecryptModuleNapi(); + static napi_value Init(napi_env env, napi_value exports); + static napi_value GetDecryptModule(napi_env env, sptr decryptModuleImpl); + static napi_value Release(napi_env env, napi_callback_info info); + static napi_value RequireSecureDecoderModule(napi_env env, napi_callback_info info); + static napi_value DecryptData(napi_env env, napi_callback_info info); +private: + static napi_value MediaDecryptModuleNapiConstructor(napi_env env, napi_callback_info info); + static void MediaDecryptModuleNapiDestructor(napi_env env, void *nativeObject, void *finalize); + + napi_env env_; + napi_ref wrapper_; + static thread_local napi_ref sConstructor_; + static thread_local sptr sMediaDecryptModuleImpl_; + sptr mediaDecryptModuleImpl_; + +}; + +} // DrmStandard +} // OHOS + + +#endif // OHOS_DRM_DECYPT_MODULE_H_ \ No newline at end of file diff --git a/interfaces/kits/js/drm_napi/include/media_key_system_callback_napi.h b/interfaces/kits/js/drm_napi/include/media_key_system_callback_napi.h new file mode 100644 index 00000000..a0dcfb7f --- /dev/null +++ b/interfaces/kits/js/drm_napi/include/media_key_system_callback_napi.h @@ -0,0 +1,14 @@ +/* + * Copyright (c) 2023-2024 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ \ No newline at end of file diff --git a/interfaces/kits/js/drm_napi/include/media_key_system_napi.h b/interfaces/kits/js/drm_napi/include/media_key_system_napi.h new file mode 100644 index 00000000..f4cc95e4 --- /dev/null +++ b/interfaces/kits/js/drm_napi/include/media_key_system_napi.h @@ -0,0 +1,78 @@ +/* + * Copyright (c) 2023-2024 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef OHOS_DRM_MEDIA_KEY_SYSTEM_NAPI_H_ +#define OHOS_DRM_MEDIA_KEY_SYSTEM_NAPI_H_ + +#include "media_key_system_impl.h" +#include "media_key_system_factory_impl.h" +#include "key_session_impl.h" +#include "drm_log.h" +#include "drm_napi_utils.h" + +#include "napi/native_api.h" +#include "napi/native_node_api.h" +#include "hilog/log.h" +#include + +#include +#include +#include +#include +#include +#include +#include +#include + +namespace OHOS { +namespace DrmStandard { + +static const char MEDIA_KEY_SYSTEM_NAPI_CLASS_NAME[] = "MediaKeySystem"; + +class MediaKeySystemNapi { +public: + static napi_value Init(napi_env env, napi_value exports); + MediaKeySystemNapi(); + ~MediaKeySystemNapi(); + napi_value Release(napi_env env, napi_callback_info info); + //NOTE:static means kind of PIDs enjoy the same mediakeysystem? + static napi_value IsMediaKeySystemSupported(napi_env env, napi_callback_info info); + static napi_value CreateMediaKeySystemInstance(napi_env env, napi_callback_info info); + static napi_value SetConfiguration(napi_env env, napi_callback_info info); + static napi_value GetConfiguration(napi_env env, napi_callback_info info); + static napi_value GenerateKeySystemRequest(napi_env env, napi_callback_info info); + static napi_value ProcessKeySystemResponse(napi_env env, napi_callback_info info); + static napi_value CreateKeySession(napi_env env, napi_callback_info info); + static napi_value GetMetric(napi_env env, napi_callback_info info); + static napi_value GetSecurityLevel(napi_env env, napi_callback_info info); +private: + // void ErrorCallback(MediaServiceExtErrCode errCode, std::string errMsg = "unknown"); + // void SetCallbackReference(const std::string &callbackName, std::shared_ptr ref); + // void CancelCallback(); + static napi_value MediaKeySystemNapiConstructor(napi_env env, napi_callback_info info); + static void MediaKeySystemNapiDestructor(napi_env env, void *nativeObject, void *finalize); + + static thread_local napi_ref sConstructor_; + napi_env env_; + napi_ref wrapper_; + sptr mediaKeySystemImpl_; + static thread_local sptr sMediaKeySystemImpl_; + + //std::shared_ptr mediaKeySystemCb_ = nullptr; +}; +} // namespace DrmStandard +} // namespace OHOS + +#endif // OHOS_DRM_MEDIA_KEY_SYSTEM_NAPI_H_ diff --git a/interfaces/kits/js/drm_napi/include/native_module_ohos_drm.h b/interfaces/kits/js/drm_napi/include/native_module_ohos_drm.h new file mode 100644 index 00000000..5496e5de --- /dev/null +++ b/interfaces/kits/js/drm_napi/include/native_module_ohos_drm.h @@ -0,0 +1,25 @@ +/* + * Copyright (c) 2023-2024 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef NATIVE_MODULE_OHOS_DRM_H_ +#define NATIVE_MODULE_OHOS_DRM_H_ + +#include "media_key_system_napi.h" +#include "media_key_system_callback_napi.h" +#include "key_session_napi.h" +#include "media_decrypt_module_napi.h" +#include "drm_napi.h" +#include "drm_enum_napi.h" +#endif /* NATIVE_MODULE_OHOS_DRM_H_*/ \ No newline at end of file diff --git a/patches/patches.json b/patches/patches.json new file mode 100644 index 00000000..1038eafb --- /dev/null +++ b/patches/patches.json @@ -0,0 +1,9 @@ +{ + "patches":[ + { + "project":"productdefine_common", + "path":"productdefine/common", + "pr_url":"https://gitee.com/openharmony/productdefine_common/pulls/733" + } + ] +} diff --git a/sa_profile/3012.json b/sa_profile/3012.json new file mode 100644 index 00000000..985c590c --- /dev/null +++ b/sa_profile/3012.json @@ -0,0 +1,12 @@ +{ + "process": "drm_service", + "systemability": [ + { + "name": 3012, + "libpath": "libdrm_service.z.so", + "run-on-create": true, + "distributed": false, + "dump_level": 1 + } + ] +} \ No newline at end of file diff --git a/sa_profile/BUILD.gn b/sa_profile/BUILD.gn new file mode 100644 index 00000000..096c6f4c --- /dev/null +++ b/sa_profile/BUILD.gn @@ -0,0 +1,20 @@ +# Copyright (c) 2023-2024 Huawei Device Co., Ltd. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import("//build/ohos/sa_profile/sa_profile.gni") + +ohos_sa_profile("drm_service_sa_profile") { + sources = [ "3012.json" ] + + part_name = "multimedia_drm_framework" +} diff --git a/services/drm_service/BUILD.gn b/services/drm_service/BUILD.gn new file mode 100644 index 00000000..92639695 --- /dev/null +++ b/services/drm_service/BUILD.gn @@ -0,0 +1,87 @@ +import("//build/ohos.gni") + +ohos_shared_library("drm_service") { + install_enable = true + + include_dirs = [ + "//foundation/graphic/graphic_2d/frameworks/surface/include", + "//foundation/multimedia/drm_framework/services/drm_service/client/include", + "//foundation/multimedia/drm_framework/services/drm_service/server/include", + "//foundation/multimedia/drm_framework/services/drm_service/ipc", + "//foundation/multimedia/drm_framework/interfaces/inner_api/native", + "//foundation/multimedia/drm_framework/interfaces/inner_api/native/drm", + "//foundation/multimedia/drm_framework/services/utils/include", + "//base/security/access_token/interfaces/innerkits/accesstoken/include", + "//third_party/glib/glibmemdfx", + "//drivers/hdf_core/interfaces/inner_api/utils", + "//drivers/hdf_core/adapter/khdf/uniproton/osal/include", + "//drivers/hdf_core/adapter/uhdf2/ipc/include", + "//drivers/hdf_core/interfaces/inner_api/ipc", + "//drivers/hdf_core/interfaces/inner_api/hdi", + "//foundation//arkui/napi/interfaces/kits", + ] + include_dirs += [ + "//utils/system/safwk/native/include", + ] + + sources = [ + "//foundation/multimedia/drm_framework/services/drm_service/server/src/drm_host_manager.cpp", + "//foundation/multimedia/drm_framework/services/drm_service/server/src/mediakeysystemfactory_service.cpp", + "//foundation/multimedia/drm_framework/services/drm_service/server/src/mediakeysystemfactory_service_stub.cpp", + "//foundation/multimedia/drm_framework/services/drm_service/server/src/mediakeysystem_service.cpp", + "//foundation/multimedia/drm_framework/services/drm_service/server/src/mediakeysystem_service_stub.cpp", + "//foundation/multimedia/drm_framework/services/drm_service/server/src/key_session_service.cpp", + "//foundation/multimedia/drm_framework/services/drm_service/server/src/key_session_service_stub.cpp", + "//foundation/multimedia/drm_framework/services/drm_service/server/src/media_decrypt_module_service.cpp", + "//foundation/multimedia/drm_framework/services/drm_service/server/src/media_decrypt_module_service_stub.cpp", + "//foundation/multimedia/drm_framework/services/drm_service/client/src/key_session_service_callback_proxy.cpp" + ] + + deps = [ + ] + + external_deps = [ + "ability_base:want", + "access_token:libaccesstoken_sdk", + "access_token:libprivacy_sdk", + "bundle_framework:appexecfwk_base", + "bundle_framework:appexecfwk_core", + "hilog:libhilog", + "hitrace:hitrace_meter", + "hicollie:libhicollie", + "hisysevent:libhisysevent", + "ipc:ipc_core", + "ipc:ipc_single", + "safwk:system_ability_fwk", + "samgr:samgr_proxy", + "c_utils:utils", + "hdf_core:libhdi", + "napi:ace_napi", + ] + + cflags = [ + "-Wall", + "-fPIC", + ] + + if (target_cpu == "arm") { + cflags += [ "-DBINDER_IPC_32BIT" ] + } + + sanitize = { + cfi = true + cfi_cross_dso = true + debug = true + } + + if (use_musl) { + if (use_jemalloc && use_jemalloc_dfx_intf) { + cflags += [ "-DCONFIG_USE_JEMALLOC_DFX_INTF" ] + } + } + + cflags_cc = cflags + + subsystem_name = "multimedia" + part_name = "multimedia_drm_framework" +} diff --git a/services/drm_service/client/include/key_session_service_callback_proxy.h b/services/drm_service/client/include/key_session_service_callback_proxy.h new file mode 100644 index 00000000..d6eb18c3 --- /dev/null +++ b/services/drm_service/client/include/key_session_service_callback_proxy.h @@ -0,0 +1,38 @@ +/* + * Copyright (c) 2023-2024 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef OHOS_DRM_KEY_SESSION_SERVICE_CALLBACK_PROXY_H +#define OHOS_DRM_KEY_SESSION_SERVICE_CALLBACK_PROXY_H + +#include "i_keysession_service_callback.h" +#include "iremote_proxy.h" + +namespace OHOS { +namespace DrmStandard { + +class KeySessionServiceCallbackProxy : public IRemoteProxy { +public: + explicit KeySessionServiceCallbackProxy(const sptr &impl); + virtual ~KeySessionServiceCallbackProxy() = default; + int32_t OnKeySessionKeyExpired(const KeyStatus status) override; + int32_t OnKeySessionReclaimed(const SessionStatus status) override; +private: + static inline BrokerDelegator delegator_; +}; + +} // DrmStandard +} // OHOS + +#endif // OHOS_DRM_KEY_SESSION_SERVICE_CALLBACK_PROXY_H \ No newline at end of file diff --git a/services/drm_service/client/include/key_session_service_proxy.h b/services/drm_service/client/include/key_session_service_proxy.h new file mode 100644 index 00000000..b9103343 --- /dev/null +++ b/services/drm_service/client/include/key_session_service_proxy.h @@ -0,0 +1,47 @@ +/* + * Copyright (c) 2023-2024 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef OHOS_DRM_KEY_SESSION_PROXY_H +#define OHOS_DRM_KEY_SESSION_PROXY_H + +#include "i_keysession_service.h" +#include "iremote_proxy.h" + +namespace OHOS { +namespace DrmStandard { +class KeySessionServiceProxy : public IRemoteProxy { +public: + explicit KeySessionServiceProxy(const sptr &impl); + virtual ~KeySessionServiceProxy() = default; + int32_t Release() override; + int32_t CreateMediaDecryptModule(sptr &decryptModule) override; + int32_t GenerateLicenseRequest(IKeySessionService::DrmInfo &drmInfo, IKeySessionService::LicenseInfo &licenseInfo) override; + int32_t ProcessLicenseResponse(std::vector &keyId, std::vector &licenseResponse) override; + int32_t GenerateOfflineReleaseRequest(std::vector &keyId, std::vector &releaseRequest) override; + int32_t ProcessOfflineReleaseResponse(std::vector &keyId, std::vector &releaseReponse) override; + int32_t CheckLicenseStatus(std::vector &infoMap) override; + int32_t RestoreOfflineKeys(std::vector &keyId) override; + int32_t RemoveOfflineKeys(std::vector &keyId) override; + int32_t GetOfflineKeyIds(std::vector> &keyIds) override; + int32_t RemoveLicenses() override; + int32_t GetOfflineKeyState(std::vector &keyId, IKeySessionService::OfflineKeyState &state) override; + int32_t SetKeySessionServiceCallback(sptr &callback) override; +private: + static inline BrokerDelegator delegator_; +}; +} // DrmStandard +} // OHOS + +#endif \ No newline at end of file diff --git a/services/drm_service/client/include/media_decrypt_module_service_proxy.h b/services/drm_service/client/include/media_decrypt_module_service_proxy.h new file mode 100644 index 00000000..880e3116 --- /dev/null +++ b/services/drm_service/client/include/media_decrypt_module_service_proxy.h @@ -0,0 +1,41 @@ +/* + * Copyright (c) 2023-2024 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef OHOS_DRM_MEDIA_DECRYPT_MODULE_SERVICE_PROXY_H +#define OHOS_DRM_MEDIA_DECRYPT_MODULE_SERVICE_PROXY_H + +#include "i_mediadecryptmodule_service.h" +#include "remote_request_code.h" +#include "iremote_proxy.h" +#include "drm_log.h" +#include "drm_napi_utils.h" + +namespace OHOS { +namespace DrmStandard { +class MediaDecryptModuleServiceProxy : public IRemoteProxy { +public: + explicit MediaDecryptModuleServiceProxy(const sptr &impl); + virtual ~MediaDecryptModuleServiceProxy() = default; + int32_t Release() override; + int32_t DecryptData(bool secureDecodrtState, IMediaDecryptModuleService::CryptInfo &cryptInfo, uint64_t srcBuffer, uint64_t dstBuffer) override; + int32_t RequireSecureDecoderModule(std::string &mimeType, bool *status) override; + +private: + static inline BrokerDelegator delegator_; +}; +} // DrmStandard +} // OHOS + +#endif // OHOS_DRM_MEDIA_KEY_SYSTEM_SERVICE_PROXY_H \ No newline at end of file diff --git a/services/drm_service/client/include/mediakeysystem_service_proxy.h b/services/drm_service/client/include/mediakeysystem_service_proxy.h new file mode 100644 index 00000000..82d5cea3 --- /dev/null +++ b/services/drm_service/client/include/mediakeysystem_service_proxy.h @@ -0,0 +1,43 @@ +/* + * Copyright (c) 2023-2024 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef OHOS_DRM_MEDIA_KEY_SYSTEM_SERVICE_PROXY_H +#define OHOS_DRM_MEDIA_KEY_SYSTEM_SERVICE_PROXY_H + +#include "i_mediakeysystem_service.h" +#include "iremote_proxy.h" +#include + +namespace OHOS { +namespace DrmStandard { +class MediaKeySystemServiceProxy : public IRemoteProxy { +public: + explicit MediaKeySystemServiceProxy(const sptr &impl); + virtual ~MediaKeySystemServiceProxy() = default; + int32_t Release() override; + int32_t GenerateKeySystemRequest(IMediaKeySystemService::RequestType type, std::vector &request, std::string &defaultUrl) override; + int32_t ProcessKeySystemResponse(IMediaKeySystemService::RequestType type, const std::vector &response) override; + int32_t SetConfiguration(IMediaKeySystemService::ConfigType type, std::string &propertyName, std::string &value) override; + int32_t GetConfiguration(IMediaKeySystemService::ConfigType configType, std::string &propertyName, std::string &value) override; + int32_t CreateKeySession(IKeySessionService::SecurityLevel securityLevel, sptr &keySessionProxy) override; + int32_t GetMetric(std::vector &infoMap) override; + int32_t GetSecurityLevel(IKeySessionService::SecurityLevel *securityLevel) override; +private: + static inline BrokerDelegator delegator_; +}; +} // DrmStandard +} // OHOS + +#endif // OHOS_DRM_MEDIA_KEY_SYSTEM_SERVICE_PROXY_H diff --git a/services/drm_service/client/include/mediakeysystemfactory_service_proxy.h b/services/drm_service/client/include/mediakeysystemfactory_service_proxy.h new file mode 100644 index 00000000..19081d13 --- /dev/null +++ b/services/drm_service/client/include/mediakeysystemfactory_service_proxy.h @@ -0,0 +1,40 @@ +/* + * Copyright (c) 2023-2024 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef OHOS_DRM_MEDIA_KEY_SYSTEM_FACTORY_SERVICE_PROXY_H +#define OHOS_DRM_MEDIA_KEY_SYSTEM_FACTORY_SERVICE_PROXY_H + +#include "i_mediakeysystemfactory_service.h" +#include "iremote_proxy.h" +#include + +namespace OHOS { +namespace DrmStandard { +class MediaKeySystemFactoryServiceProxy : public IRemoteProxy { +public: + explicit MediaKeySystemFactoryServiceProxy(const sptr &impl); + virtual ~MediaKeySystemFactoryServiceProxy() = default; + int32_t IsMediaKeySystemSupported(std::string &uuid, bool *isSurpported) override; + int32_t IsMediaKeySystemSupported(std::string &uuid, std::string &mimeType, bool *isSurpported) override; + int32_t IsMediaKeySystemSupported(std::string &uuid, std::string &mimeType, int32_t securityLevel, bool *isSurpported) override; + int32_t CreateMediaKeySystem(std::string &uuid) override; + +private: + static inline BrokerDelegator delegator_; +}; +} // DrmStandard +} // OHOS + +#endif // OHOS_DRM_MEDIA_KEY_SYSTEM_FACTORY_SERVICE_PROXY_H diff --git a/services/drm_service/client/src/key_session_service_callback_proxy.cpp b/services/drm_service/client/src/key_session_service_callback_proxy.cpp new file mode 100644 index 00000000..29c1423e --- /dev/null +++ b/services/drm_service/client/src/key_session_service_callback_proxy.cpp @@ -0,0 +1,69 @@ +/* + * Copyright (c) 2023-2024 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "key_session_service_callback_proxy.h" +#include "drm_log.h" +#include "remote_request_code.h" + +namespace OHOS { +namespace DrmStandard { + +KeySessionServiceCallbackProxy::KeySessionServiceCallbackProxy(const sptr &impl) + : IRemoteProxy(impl) { }; + +int32_t KeySessionServiceCallbackProxy::OnKeySessionKeyExpired(const KeyStatus status) +{ + MessageParcel data; + MessageParcel reply; + MessageOption option; + DRM_INFO_LOG("KeySessionServiceCallbackProxy OnKeySessionKeyExpired called"); + if (!data.WriteInterfaceToken(GetDescriptor())) { + DRM_ERR_LOG("KeySessionServiceCallbackProxy OnKeySessionKeyExpired Write interface token failed"); + return IPC_PROXY_ERR; + } + if (!data.WriteInt32(status)) { + DRM_ERR_LOG("KeySessionServiceCallbackProxy OnKeySessionKeyExpired Write status failed"); + return IPC_PROXY_ERR; + } + int error = Remote()->SendRequest(KEY_SESSION_SERVICE_CALLBACK_ON_KEY_EXPIRED, data, reply, option); + if (error != ERR_NONE) { + DRM_ERR_LOG("KeySessionServiceCallbackProxy OnKeySessionKeyExpired failed, error: %{public}d", error); + } + return error; +} + +int32_t KeySessionServiceCallbackProxy::OnKeySessionReclaimed(const SessionStatus status) +{ + MessageParcel data; + MessageParcel reply; + MessageOption option; + DRM_INFO_LOG("KeySessionServiceCallbackProxy OnKeySessionReclaimed called"); + if (!data.WriteInterfaceToken(GetDescriptor())) { + DRM_ERR_LOG("KeySessionServiceCallbackProxy OnKeySessionReclaimed Write interface token failed"); + return IPC_PROXY_ERR; + } + if (!data.WriteInt32(status)) { + DRM_ERR_LOG("KeySessionServiceCallbackProxy OnKeySessionReclaimed Write status failed"); + return IPC_PROXY_ERR; + } + int error = Remote()->SendRequest(KEY_SESSION_SERVICE_CALLBACK_ON_KEY_SESSION_RECLAIMED, data, reply, option); + if (error != ERR_NONE) { + DRM_ERR_LOG("KeySessionServiceCallbackProxy OnKeySessionReclaimed failed, error: %{public}d", error); + } + return error; +} + +} // DrmStandard +} // namespace OHOS \ No newline at end of file diff --git a/services/drm_service/client/src/key_session_service_proxy.cpp b/services/drm_service/client/src/key_session_service_proxy.cpp new file mode 100644 index 00000000..a07cc8fd --- /dev/null +++ b/services/drm_service/client/src/key_session_service_proxy.cpp @@ -0,0 +1,443 @@ +/* + * Copyright (c) 2023-2024 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "key_session_service_proxy.h" +#include "drm_log.h" +#include "remote_request_code.h" + +namespace OHOS { +namespace DrmStandard { + +KeySessionServiceProxy::KeySessionServiceProxy(const sptr &impl) + : IRemoteProxy(impl) +{ + DRM_INFO_LOG("KeySessionServiceProxy Initialized."); +} + +int32_t KeySessionServiceProxy::CreateMediaDecryptModule(sptr &decryptModule) +{ + DRM_INFO_LOG("KeySessionServiceProxy::CreateMediaDecryptModule enter."); + MessageParcel data; + MessageParcel reply; + MessageOption option; + + if (!data.WriteInterfaceToken(GetDescriptor())) { + DRM_ERR_LOG("KeySessionServiceProxy CreateMediaDecryptModule Write interface token failed"); + return IPC_PROXY_ERR; + } + + int error = KeySessionServiceProxy::Remote()->SendRequest(CREATE_MEDIA_DECRYPT_MODULE, data, reply, option); + if (error != ERR_NONE) { + DRM_ERR_LOG("KeySessionServiceProxy::CreateMediaDecryptModule failed, error: %{public}d", error); + return error; + } + + auto remoteObject = reply.ReadRemoteObject(); + if (remoteObject != nullptr) { + decryptModule = iface_cast(remoteObject); + } else { + DRM_ERR_LOG("KeySessionServiceProxy CreateMediaDecryptModule decryptModule is nullptr"); + error = IPC_PROXY_ERR; + } + DRM_INFO_LOG("KeySessionServiceProxy::CreateMediaDecryptModule exit."); + return error; +} + + +int32_t KeySessionServiceProxy::Release() +{ + DRM_INFO_LOG("KeySessionServiceProxy::Release enter."); + MessageParcel data; + MessageParcel reply; + MessageOption option; + + if (!data.WriteInterfaceToken(GetDescriptor())) { + DRM_ERR_LOG("KeySessionServiceProxy Release Write interface token failed"); + return IPC_PROXY_ERR; + } + int error = KeySessionServiceProxy::Remote()->SendRequest(KEY_SESSION_RELEASE, data, reply, option); + if (error != ERR_NONE) { + DRM_ERR_LOG("KeySessionServiceProxy Release failed, error: %{public}d", error); + } + DRM_INFO_LOG("KeySessionServiceProxy::Release exit."); + return error; +} + +int32_t KeySessionServiceProxy::GenerateLicenseRequest(IKeySessionService::DrmInfo &drmInfo, IKeySessionService::LicenseInfo &licenseInfo) +{ + DRM_INFO_LOG("KeySessionServiceProxy::GenerateLicenseRequest enter."); + MessageParcel data; + MessageParcel reply; + MessageOption option; + if (!data.WriteInterfaceToken(KeySessionServiceProxy::GetDescriptor())) { + DRM_ERR_LOG("KeySessionServiceProxy GenerateLicenseRequest Write interface token failed"); + return IPC_PROXY_ERR; + } + + if (!data.WriteInt32(drmInfo.keyType)) { + DRM_ERR_LOG("KeySessionServiceProxy GenerateLicenseRequest Write keyType failed"); + return IPC_PROXY_ERR; + } + if (!data.WriteString(drmInfo.mimeType)) { + DRM_ERR_LOG("KeySessionServiceProxy GenerateLicenseRequest Write mimeType failed"); + return IPC_PROXY_ERR; + } + if (!data.WriteInt32(drmInfo.indexInfo.size())) { + DRM_ERR_LOG("KeySessionServiceProxy GenerateLicenseRequest Write indexInfo.size failed"); + return IPC_PROXY_ERR; + } + + if (!data.WriteBuffer(drmInfo.indexInfo.data(), drmInfo.indexInfo.size())) { + DRM_ERR_LOG("KeySessionServiceProxy GenerateLicenseRequest Write indexInfo.size failed"); + return IPC_PROXY_ERR; + } + int error = KeySessionServiceProxy::Remote()->SendRequest(MEDIA_KEY_SESSION_GENERATE_LICENSE_REQUEST, data, reply, option); + if (error != ERR_NONE) { + DRM_ERR_LOG("KeySessionServiceProxy::GenerateLicenseRequest failed, error: %{public}d", error); + return error; + } + + licenseInfo.requestType = (IKeySessionService::RequestType)reply.ReadInt32(); + uint32_t dataSize = reply.ReadInt32(); + for (int i = 0; i < dataSize; i++) { + licenseInfo.mData.push_back(reply.ReadUint8()); + } + + licenseInfo.mDefaultURL = reply.ReadString(); + DRM_INFO_LOG("KeySessionServiceProxy::GenerateLicenseRequest exit."); + return reply.ReadInt32(); +} + +int32_t KeySessionServiceProxy::ProcessLicenseResponse(std::vector &keyId, std::vector &licenseResponse) +{ + DRM_INFO_LOG("KeySessionServiceProxy::ProcessLicenseResponse enter."); + MessageParcel data; + MessageParcel reply; + MessageOption option; + + if (!data.WriteInterfaceToken(KeySessionServiceProxy::GetDescriptor())) { + DRM_ERR_LOG("KeySessionServiceProxy ProcessLicenseResponse Write interface token failed"); + return IPC_PROXY_ERR; + } + if (!data.WriteInt32(licenseResponse.size())) { + DRM_ERR_LOG("KeySessionServiceProxy ProcessLicenseResponse Write licenseResponse size failed"); + return IPC_PROXY_ERR; + } + for (auto response : licenseResponse) { + if (!data.WriteUint8(response)) { + DRM_ERR_LOG("KeySessionServiceProxy ProcessLicenseResponse Write licenseResponse failed"); + return IPC_PROXY_ERR; + } + } + int error = KeySessionServiceProxy::Remote()->SendRequest(MEDIA_KEY_SESSION_PROCESS_LICENSE_RESPONSE, data, reply, option); + if (error != ERR_NONE) { + DRM_ERR_LOG("KeySessionServiceProxy::ProcessLicenseResponse failed, error: %{public}d", error); + return error; + } + uint32_t keyIdSize = reply.ReadInt32(); + for (int i = 0; i < keyIdSize; i++) { + keyId.push_back(reply.ReadUint8()); + } + DRM_INFO_LOG("KeySessionServiceProxy::ProcessLicenseResponse exit."); + return reply.ReadInt32(); +} + +int32_t KeySessionServiceProxy::GenerateOfflineReleaseRequest(std::vector &keyId, std::vector &releaseRequest) +{ + DRM_INFO_LOG("KeySessionServiceProxy::GenerateOfflineReleaseRequest enter."); + MessageParcel data; + MessageParcel reply; + MessageOption option; + + if (!data.WriteInterfaceToken(KeySessionServiceProxy::GetDescriptor())) { + DRM_ERR_LOG("KeySessionServiceProxy GenerateOfflineReleaseRequest Write interface token failed"); + return IPC_PROXY_ERR; + } + if (!data.WriteInt32(keyId.size())) { + DRM_ERR_LOG("KeySessionServiceProxy GenerateOfflineReleaseRequest Write keyId size failed"); + return IPC_PROXY_ERR; + } + for (auto id : keyId) { + if (!data.WriteUint8(id)) { + DRM_ERR_LOG("KeySessionServiceProxy GenerateOfflineReleaseRequest Write keyId failed"); + return IPC_PROXY_ERR; + } + } + + int error = KeySessionServiceProxy::Remote()->SendRequest(MEDIA_KEY_SESSION_GENERATE_OFFLINE_RELEASE_REQUEST, data, reply, option); + if (error != ERR_NONE) { + DRM_ERR_LOG("KeySessionServiceProxy::GenerateOfflineReleaseRequest failed, error: %{public}d", error); + return error; + } + uint32_t requestSize = reply.ReadInt32(); + for (int i = 0; i < requestSize; i++) { + releaseRequest.push_back(reply.ReadUint8()); + } + DRM_INFO_LOG("KeySessionServiceProxy::GenerateOfflineReleaseRequest exit."); + return reply.ReadInt32(); +} + +int32_t KeySessionServiceProxy::ProcessOfflineReleaseResponse(std::vector &keyId, std::vector &releaseReponse) +{ + DRM_INFO_LOG("KeySessionServiceProxy::ProcessOfflineReleaseResponse enter."); + MessageParcel data; + MessageParcel reply; + MessageOption option; + + if (!data.WriteInterfaceToken(KeySessionServiceProxy::GetDescriptor())) { + DRM_ERR_LOG("KeySessionServiceProxy ProcessOfflineReleaseResponse Write interface token failed"); + return IPC_PROXY_ERR; + } + + if (!data.WriteInt32(keyId.size())) { + DRM_ERR_LOG("KeySessionServiceProxy ProcessOfflineReleaseResponse Write keyId size failed"); + return IPC_PROXY_ERR; + } + for (auto id : keyId) { + if (!data.WriteUint8(id)) { + DRM_ERR_LOG("KeySessionServiceProxy ProcessOfflineReleaseResponse Write keyId failed"); + return IPC_PROXY_ERR; + } + } + if (!data.WriteInt32(releaseReponse.size())) { + DRM_ERR_LOG("KeySessionServiceProxy ProcessOfflineReleaseResponse Write releaseReponse size failed"); + return IPC_PROXY_ERR; + } + for (auto response : releaseReponse) { + if (!data.WriteUint8(response)) { + DRM_ERR_LOG("KeySessionServiceProxy ProcessOfflineReleaseResponse Write releaseReponse failed"); + return IPC_PROXY_ERR; + } + } + int error = KeySessionServiceProxy::Remote()->SendRequest(MEDIA_KEY_SESSION_PROCESS_OFFLINE_RELEASE_RESPONSE, data, reply, option); + if (error != ERR_NONE) { + DRM_ERR_LOG("KeySessionServiceProxy::ProcessOfflineReleaseResponse failed, error: %{public}d", error); + return error; + } + + DRM_INFO_LOG("KeySessionServiceProxy::ProcessOfflineReleaseResponse exit."); + return reply.ReadInt32(); +} + +int32_t KeySessionServiceProxy::CheckLicenseStatus(std::vector &infoMap) +{ + DRM_INFO_LOG("KeySessionServiceProxy::GenerateOfflineReleaseRequest enter."); + MessageParcel data; + MessageParcel reply; + MessageOption option; + + if (!data.WriteInterfaceToken(KeySessionServiceProxy::GetDescriptor())) { + DRM_ERR_LOG("KeySessionServiceProxy GenerateOfflineReleaseRequest Write interface token failed"); + return IPC_PROXY_ERR; + } + + int error = KeySessionServiceProxy::Remote()->SendRequest(MEDIA_KEY_SESSION_GENERATE_CHECK_LICENSE_STATUS, data, reply, option); + if (error != ERR_NONE) { + DRM_ERR_LOG("KeySessionServiceProxy::GenerateOfflineReleaseRequest failed, error: %{public}d", error); + return error; + } + int infoMapSize = reply.ReadInt32(); + for (int i = 0; i < infoMapSize; i++) { + KeyValue keyValue; + keyValue.name = reply.ReadString(); + keyValue.value = reply.ReadString(); + infoMap.push_back(keyValue); + } + DRM_INFO_LOG("KeySessionServiceProxy::GenerateOfflineReleaseRequest exit."); + return reply.ReadInt32(); +} + +int32_t KeySessionServiceProxy::RestoreOfflineKeys(std::vector &keyId) +{ + DRM_INFO_LOG("KeySessionServiceProxy::RestoreOfflineKeys enter."); + MessageParcel data; + MessageParcel reply; + MessageOption option; + + if (!data.WriteInterfaceToken(KeySessionServiceProxy::GetDescriptor())) { + DRM_ERR_LOG("KeySessionServiceProxy RestoreOfflineKeys Write interface token failed"); + return IPC_PROXY_ERR; + } + + if (!data.WriteInt32(keyId.size())) { + DRM_ERR_LOG("KeySessionServiceProxy RestoreOfflineKeys Write keyId size failed"); + return IPC_PROXY_ERR; + } + for (auto id : keyId) { + if (!data.WriteUint8(id)) { + DRM_ERR_LOG("KeySessionServiceProxy RestoreOfflineKeys Write keyId failed"); + return IPC_PROXY_ERR; + } + } + + int error = KeySessionServiceProxy::Remote()->SendRequest(MEDIA_KEY_SESSION_RESTORE_OFFLINEKEYS, data, reply, option); + if (error != ERR_NONE) { + DRM_ERR_LOG("KeySessionServiceProxy::RestoreOfflineKeys failed, error: %{public}d", error); + return error; + } + + DRM_INFO_LOG("KeySessionServiceProxy::RestoreOfflineKeys exit."); + return reply.ReadInt32(); +} + +int32_t KeySessionServiceProxy::RemoveOfflineKeys(std::vector &keyId) +{ + DRM_INFO_LOG("KeySessionServiceProxy::RemoveOfflineKeys enter."); + MessageParcel data; + MessageParcel reply; + MessageOption option; + + if (!data.WriteInterfaceToken(KeySessionServiceProxy::GetDescriptor())) { + DRM_ERR_LOG("KeySessionServiceProxy RemoveOfflineKeys Write interface token failed"); + return IPC_PROXY_ERR; + } + + if (!data.WriteInt32(keyId.size())) { + DRM_ERR_LOG("KeySessionServiceProxy RestoreOfflineKeys Write keyId size failed"); + return IPC_PROXY_ERR; + } + for (auto id : keyId) { + if (!data.WriteUint8(id)) { + DRM_ERR_LOG("KeySessionServiceProxy RestoreOfflineKeys Write keyId failed"); + return IPC_PROXY_ERR; + } + } + + int error = KeySessionServiceProxy::Remote()->SendRequest(MEDIA_KEY_SESSION_REMOVE_OFFLINEKEYS, data, reply, option); + if (error != ERR_NONE) { + DRM_ERR_LOG("KeySessionServiceProxy::RemoveOfflineKeys failed, error: %{public}d", error); + return error; + } + + DRM_INFO_LOG("KeySessionServiceProxy::RemoveOfflineKeys exit."); + return reply.ReadInt32(); +} + +int32_t KeySessionServiceProxy::GetOfflineKeyIds(std::vector> &keyIds) +{ + DRM_INFO_LOG("KeySessionServiceProxy::GetOfflineKeyIds enter."); + MessageParcel data; + MessageParcel reply; + MessageOption option; + + if (!data.WriteInterfaceToken(KeySessionServiceProxy::GetDescriptor())) { + DRM_ERR_LOG("KeySessionServiceProxy GetOfflineKeyIds Write interface token failed"); + return IPC_PROXY_ERR; + } + int32_t error = KeySessionServiceProxy::Remote()->SendRequest(MEDIA_KEY_SESSION_GET_OFFLINEKEYIDS, data, reply, option); + if (error != ERR_NONE) { + DRM_ERR_LOG("KeySessionServiceProxy::GetOfflineKeyIds failed, error: %{public}d", error); + return error; + } + int32_t keyIdsSize = reply.ReadInt32(); + for (int i = 0; i < keyIdsSize; i++) { + int32_t keyIdSize = reply.ReadInt32(); + std::vector keyId; + const uint8_t* keyIdBuf = static_cast(reply.ReadBuffer(keyIdSize)); + if (keyIdBuf == nullptr) { + DRM_ERR_LOG("KeySessionServiceProxy::GetOfflineKeyIds ReadBuffer failed"); + return error; + } + keyId.assign(keyIdBuf, keyIdBuf + keyIdSize); + keyIds.push_back(keyId); + } + DRM_INFO_LOG("KeySessionServiceProxy::GetOfflineKeyIds exit."); + return reply.ReadInt32(); +} + +int32_t KeySessionServiceProxy::RemoveLicenses() +{ + DRM_INFO_LOG("KeySessionServiceProxy::RemoveLicenses enter."); + MessageParcel data; + MessageParcel reply; + MessageOption option; + + if (!data.WriteInterfaceToken(KeySessionServiceProxy::GetDescriptor())) { + DRM_ERR_LOG("KeySessionServiceProxy RemoveLicenses Write interface token failed"); + return IPC_PROXY_ERR; + } + + int error = KeySessionServiceProxy::Remote()->SendRequest(MEDIA_KEY_SESSION_REMOVE_LICENSE, data, reply, option); + if (error != ERR_NONE) { + DRM_ERR_LOG("KeySessionServiceProxy::RemoveLicenses failed, error: %{public}d", error); + return error; + } + + DRM_INFO_LOG("KeySessionServiceProxy::RemoveLicenses exit."); + return reply.ReadInt32(); +} + +int32_t KeySessionServiceProxy::GetOfflineKeyState(std::vector &keyId, IKeySessionService::OfflineKeyState &state) +{ + DRM_INFO_LOG("KeySessionServiceProxy::GetOfflineKeyState enter."); + MessageParcel data; + MessageParcel reply; + MessageOption option; + + if (!data.WriteInterfaceToken(KeySessionServiceProxy::GetDescriptor())) { + DRM_ERR_LOG("KeySessionServiceProxy GetOfflineKeyState Write interface token failed"); + return IPC_PROXY_ERR; + } + if (!data.WriteInt32(keyId.size())) { + DRM_ERR_LOG("KeySessionServiceProxy GetOfflineKeyState Write keyId size failed"); + return IPC_PROXY_ERR; + } + for (auto id : keyId) { + if (!data.WriteUint8(id)) { + DRM_ERR_LOG("KeySessionServiceProxy GetOfflineKeyState Write keyId failed"); + return IPC_PROXY_ERR; + } + } + + int error = KeySessionServiceProxy::Remote()->SendRequest(MEDIA_KEY_SESSION_GET_OFFLINEKEY_STATE, data, reply, option); + if (error != ERR_NONE) { + DRM_ERR_LOG("KeySessionServiceProxy::GetOfflineKeyState failed, error: %{public}d", error); + return error; + } + state = (IKeySessionService::OfflineKeyState)reply.ReadInt32(); + DRM_INFO_LOG("KeySessionServiceProxy::GetOfflineKeyState exit."); + return reply.ReadInt32(); +} + +int32_t KeySessionServiceProxy::SetKeySessionServiceCallback(sptr &callback) +{ + DRM_INFO_LOG("KeySessionServiceProxy::SetKeySessionServiceCallback enter."); + MessageParcel data; + MessageParcel reply; + MessageOption option; + + if (callback == nullptr) { + DRM_ERR_LOG("KeySessionServiceProxy SetCallback callback is null"); + return IPC_PROXY_ERR; + } + + if (!data.WriteInterfaceToken(GetDescriptor())) { + DRM_ERR_LOG("KeySessionServiceProxy SetCallback Write interface token failed"); + return IPC_PROXY_ERR; + } + if (!data.WriteRemoteObject(callback->AsObject())) { + DRM_ERR_LOG("KeySessionServiceProxy SetCallback write CameraServiceCallback obj failed"); + return IPC_PROXY_ERR; + } + + int error = Remote()->SendRequest(MEDIA_KEY_SESSION_SET_CALLBACK, data, reply, option); + if (error != ERR_NONE) { + DRM_ERR_LOG("KeySessionServiceProxy SetCallback failed, error: %{public}d", error); + } + return error; +} + +} // DrmStandard +} // OHOS \ No newline at end of file diff --git a/services/drm_service/client/src/media_decrypt_module_service_proxy.cpp b/services/drm_service/client/src/media_decrypt_module_service_proxy.cpp new file mode 100644 index 00000000..40ec35fe --- /dev/null +++ b/services/drm_service/client/src/media_decrypt_module_service_proxy.cpp @@ -0,0 +1,163 @@ +/* + * Copyright (c) 2023-2024 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#include "media_decrypt_module_service_proxy.h" +#include +#include +#include +#include "ashmem.h" + +namespace OHOS { +namespace DrmStandard { + +MediaDecryptModuleServiceProxy::MediaDecryptModuleServiceProxy(const sptr &impl) + : IRemoteProxy(impl) +{ + DRM_DEBUG_LOG("MediaKeySystemServiceProxy Initialized."); +} + +int32_t MediaDecryptModuleServiceProxy::Release() +{ + DRM_INFO_LOG("MediaDecryptModuleServiceProxy::Release() enter."); + MessageParcel data; + MessageParcel reply; + MessageOption option; + + if (!data.WriteInterfaceToken(GetDescriptor())) { + DRM_ERR_LOG("MediaDecryptModuleServiceProxy Release Write interface token failed"); + return IPC_PROXY_ERR; + } + + int error = Remote()->SendRequest(DECRYPT_MODULE_RELEASE, data, reply, option); + if (error != ERR_NONE) { + DRM_ERR_LOG("MediaDecryptModuleServiceProxy::Release failed, error: %{public}d", error); + return error; + } + + DRM_INFO_LOG("MediaDecryptModuleServiceProxy::Release() exit."); + return reply.ReadInt32(); +} + +int32_t MediaDecryptModuleServiceProxy::DecryptData(bool secureDecodrtState, IMediaDecryptModuleService::CryptInfo &cryptInfo, uint64_t srcBuffer, uint64_t dstBuffer) +{ + DRM_INFO_LOG("MediaDecryptModuleServiceProxy::DecryptData enter."); + MessageParcel data; + MessageParcel reply; + MessageOption option; + + if (!data.WriteInterfaceToken(GetDescriptor())) { + DRM_ERR_LOG("MediaDecryptModuleServiceProxy DecryptData Write interface token failed"); + return IPC_PROXY_ERR; + } + + if (!data.WriteBool(secureDecodrtState)) { + DRM_ERR_LOG("MediaDecryptModuleServiceProxy DecryptData Write secureDecodrtState failed"); + return IPC_PROXY_ERR; + } + + if (!data.WriteInt32(cryptInfo.type)) { + DRM_ERR_LOG("MediaDecryptModuleServiceProxy DecryptData Write cryptInfo.type failed"); + return IPC_PROXY_ERR; + } + + if (!data.WriteInt32(cryptInfo.keyId.size())) { + DRM_ERR_LOG("MediaDecryptModuleServiceProxy DecryptData Write cryptInfo.keyId size failed"); + return IPC_PROXY_ERR; + } + + for (auto keyId : cryptInfo.keyId) { + if (!data.WriteUint8(keyId)) { + DRM_ERR_LOG("MediaDecryptModuleServiceProxy DecryptData Write cryptInfo.keyId failed"); + return IPC_PROXY_ERR; + } + } + + if (!data.WriteInt32(cryptInfo.iv.size())) { + DRM_ERR_LOG("MediaDecryptModuleServiceProxy DecryptData Write cryptInfo.iv size failed"); + return IPC_PROXY_ERR; + } + + for (auto iv : cryptInfo.iv) { + if (!data.WriteUint8(iv)) { + DRM_ERR_LOG("MediaDecryptModuleServiceProxy DecryptData Write cryptInfo.iv failed"); + return IPC_PROXY_ERR; + } + } + + if (!data.WriteInt32(cryptInfo.pattern.encryptBlocks)) { + DRM_ERR_LOG("MediaDecryptModuleServiceProxy DecryptData Write cryptInfo.pattern.encryptBlocks failed"); + return IPC_PROXY_ERR; + } + + if (!data.WriteInt32(cryptInfo.pattern.skipBlocks)) { + DRM_ERR_LOG("MediaDecryptModuleServiceProxy DecryptData Write cryptInfo.pattern.skipBlocks failed"); + return IPC_PROXY_ERR; + } + if (!data.WriteInt32(cryptInfo.subSample.size())) { + DRM_ERR_LOG("MediaDecryptModuleServiceProxy DecryptData Write cryptInfo.subSample.size() failed"); + return IPC_PROXY_ERR; + } + for (size_t i = 0; i < cryptInfo.subSample.size(); i++) { + if (!data.WriteInt32(cryptInfo.subSample[i].clearHeaderLen)) { + DRM_ERR_LOG("MediaDecryptModuleServiceProxy DecryptData Write cryptInfo.subSample.clearHeaderLen failed"); + return IPC_PROXY_ERR; + } + if (!data.WriteInt32(cryptInfo.subSample[i].payLoadLen)) { + DRM_ERR_LOG("MediaDecryptModuleServiceProxy DecryptData Write cryptInfo.subSample.payLoadLen failed"); + return IPC_PROXY_ERR; + } + } + (void)data.WriteFileDescriptor(srcBuffer); + (void)data.WriteFileDescriptor(dstBuffer); + + int error = Remote()->SendRequest(DECRYPT_MODULE_DECRYPT_DATA, data, reply, option); + if (error != ERR_NONE) { + DRM_ERR_LOG("MediaDecryptModuleServiceProxy DecryptData failed, error: %{public}d", error); + return error; + } + + DRM_INFO_LOG("MediaDecryptModuleServiceProxy::DecryptData exit."); + return reply.ReadInt32(); +} + +int32_t MediaDecryptModuleServiceProxy::RequireSecureDecoderModule(std::string &mimeType, bool *status) +{ + DRM_INFO_LOG("MediaDecryptModuleServiceProxy::RequireSecureDecoderModule enter."); + MessageParcel data; + MessageParcel reply; + MessageOption option; + + if (!data.WriteInterfaceToken(GetDescriptor())) { + DRM_ERR_LOG("MediaDecryptModuleServiceProxy RequireSecureDecoderModule Write interface token failed"); + return IPC_PROXY_ERR; + } + + if (!data.WriteString(mimeType)) { + DRM_ERR_LOG("MediaDecryptModuleServiceProxy RequireSecureDecoderModule Write response failed"); + return IPC_PROXY_ERR; + } + + int error = Remote()->SendRequest(DECRYPT_MODULE_REQUIRE_SECURE_DECODER, data, reply, option); + if (error != ERR_NONE) { + DRM_ERR_LOG("MediaDecryptModuleServiceProxy RequireSecureDecoderModule failed, error: %{public}d", error); + return error; + } + *status = reply.ReadBool(); + + DRM_INFO_LOG("MediaDecryptModuleServiceProxy::RequireSecureDecoderModule exit."); + return reply.ReadInt32(); +} + +} // DrmStandard +} // OHOS \ No newline at end of file diff --git a/services/drm_service/client/src/mediakeysystem_service_proxy.cpp b/services/drm_service/client/src/mediakeysystem_service_proxy.cpp new file mode 100644 index 00000000..90b9463d --- /dev/null +++ b/services/drm_service/client/src/mediakeysystem_service_proxy.cpp @@ -0,0 +1,278 @@ +/* + * Copyright (c) 2023-2024 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "mediakeysystem_service_proxy.h" +#include "remote_request_code.h" +#include "drm_log.h" +#include "drm_error_code.h" + +namespace OHOS { +namespace DrmStandard { + +MediaKeySystemServiceProxy::MediaKeySystemServiceProxy(const sptr &impl) + : IRemoteProxy(impl) +{ + DRM_DEBUG_LOG("MediaKeySystemServiceProxy Initialized."); +} + +int32_t MediaKeySystemServiceProxy::Release() +{ + DRM_INFO_LOG("MediaKeySystemServiceProxy::Release enter."); + MessageParcel data; + MessageParcel reply; + MessageOption option; + + if (!data.WriteInterfaceToken(GetDescriptor())) { + DRM_ERR_LOG("MediaKeySystemServiceProxy Release Write interface token failed"); + return IPC_PROXY_ERR; + } + int error = MediaKeySystemServiceProxy::Remote()->SendRequest(MEDIA_KEY_SYSTEM_RELEASE, data, reply, option); + if (error != ERR_NONE) { + DRM_ERR_LOG("MediaKeySystemServiceProxy Release failed, error: %{public}d", error); + return error; + } + DRM_INFO_LOG("MediaKeySystemServiceProxy::Release exit."); + return 0; +} + +int32_t MediaKeySystemServiceProxy::GenerateKeySystemRequest(IMediaKeySystemService::RequestType type, std::vector &request, std::string &defaultUrl) +{ + DRM_INFO_LOG("MediaKeySystemServiceProxy::GenerateKeySystemRequest enter. type:%{public}d.", type); + MessageParcel data; + MessageParcel reply; + MessageOption option; + + if (!data.WriteInterfaceToken(MediaKeySystemServiceProxy::GetDescriptor())) { + DRM_ERR_LOG("MediaKeySystemServiceProxy GenerateKeySystemRequest two params Write interface token failed"); + return IPC_PROXY_ERR; + } + + if (!data.WriteInt32((int)type)) { + DRM_ERR_LOG("MediaKeySystemServiceProxy GenerateKeySystemRequest two params Write RequestType failed"); + return IPC_PROXY_ERR; + } + + uint32_t error = MediaKeySystemServiceProxy::Remote()->SendRequest(MEDIA_KEY_SYSTEM_GENERATE_KEYSYSTEM_REQUEST, data, reply, option); + if (error != ERR_NONE) { + DRM_ERR_LOG("MediaKeySystemServiceProxy::GenerateKeySystemRequest failed, error: %{public}d", error); + return error; + } + + uint32_t requestSize = reply.ReadInt32(); + for (uint32_t i = 0; i < requestSize; i++) { + request.push_back(reply.ReadUint8()); + } + defaultUrl = reply.ReadString(); + + DRM_INFO_LOG("MediaKeySystemServiceProxy::GenerateKeySystemRequest exit."); + return reply.ReadInt32(); +} + +int32_t MediaKeySystemServiceProxy::ProcessKeySystemResponse(IMediaKeySystemService::RequestType type, const std::vector &response) +{ + DRM_INFO_LOG("MediaKeySystemServiceProxy::ProcessKeySystemResponse enter. type:%{public}d.", type); + MessageParcel data; + MessageParcel reply; + MessageOption option; + + if (!data.WriteInterfaceToken(GetDescriptor())) { + DRM_ERR_LOG("MediaKeySystemServiceProxy ProcessKeySystemResponse Write interface token failed"); + return IPC_PROXY_ERR; + } + + if (!data.WriteInt32((int)type)) { + DRM_ERR_LOG("MediaKeySystemServiceProxy ProcessKeySystemResponse Write RequestType failed"); + return IPC_PROXY_ERR; + } + + if (!data.WriteInt32(response.size())) { + DRM_ERR_LOG("MediaKeySystemServiceProxy ProcessKeySystemResponse Write response size failed"); + return IPC_PROXY_ERR; + } + + for (auto res : response) { + if (!data.WriteUint8(res)) { + DRM_ERR_LOG("MediaKeySystemServiceProxy ProcessKeySystemResponse Write response failed"); + return IPC_PROXY_ERR; + } + } + + int error = Remote()->SendRequest(MEDIA_KEY_SYSTEM_GENERATE_KEYSYSTEM_RESPONSE, data, reply, option); + if (error != ERR_NONE) { + DRM_ERR_LOG("MediaKeySystemServiceProxy ProcessKeySystemResponse failed, error: %{public}d", error); + return error; + } + + DRM_INFO_LOG("MediaKeySystemServiceProxy::ProcessKeySystemResponse exit."); + return reply.ReadInt32(); +} + +int32_t MediaKeySystemServiceProxy::SetConfiguration(IMediaKeySystemService::ConfigType type, std::string &propertyName, std::string &value) +{ + DRM_INFO_LOG("MediaKeySystemServiceProxy::SetConfiguration enter, configType:%{public}d, propertyName:%{public}s, value:%{public}s.", (int)type, propertyName.c_str(), value.c_str()); + MessageParcel data; + MessageParcel reply; + MessageOption option; + + if (!data.WriteInterfaceToken(GetDescriptor())) { + DRM_ERR_LOG("MediaKeySystemServiceProxy SetConfiguration Write interface token failed"); + return IPC_PROXY_ERR; + } + + if (!data.WriteInt32((int32_t)type)) { + DRM_ERR_LOG("MediaKeySystemServiceProxy SetConfiguration Write RequestType failed"); + return IPC_PROXY_ERR; + } + + if (!data.WriteString(propertyName)) { + DRM_ERR_LOG("MediaKeySystemServiceProxy SetConfiguration Write response failed"); + return IPC_PROXY_ERR; + } + if (!data.WriteString(value)) { + DRM_ERR_LOG("MediaKeySystemServiceProxy SetConfiguration Write response failed"); + return IPC_PROXY_ERR; + } + + int error = Remote()->SendRequest(MEDIA_KEY_SYSTEM_SETCONFIGURATION, data, reply, option); + if (error != ERR_NONE) { + DRM_ERR_LOG("MediaKeySystemServiceProxy SetConfiguration failed, error: %{public}d", error); + return error; + } + + DRM_INFO_LOG("MediaKeySystemServiceProxy::SetConfiguration exit."); + return reply.ReadInt32(); +} + +int32_t MediaKeySystemServiceProxy::GetSecurityLevel(IKeySessionService::SecurityLevel *securityLevel) +{ + DRM_INFO_LOG("MediaKeySystemServiceProxy::GetSecurityLevel enter."); + MessageParcel data; + MessageParcel reply; + MessageOption option; + + if (!data.WriteInterfaceToken(MediaKeySystemServiceProxy::GetDescriptor())) { + DRM_ERR_LOG("MediaKeySystemServiceProxy GetSecurityLevel Write interface token failed"); + return IPC_PROXY_ERR; + } + + int error = MediaKeySystemServiceProxy::Remote()->SendRequest(MEDIA_KEY_SYSTEM_GETSECURITYLEVEL, data, reply, option); + if (error != ERR_NONE) { + DRM_ERR_LOG("MediaKeySystemServiceProxy GetSecurityLevel failed, error: %{public}d", error); + return error; + } + + *securityLevel = (IKeySessionService::SecurityLevel)reply.ReadInt32(); + + DRM_INFO_LOG("MediaKeySystemServiceProxy::GetSecurityLevel exit."); + return reply.ReadInt32(); +} + +int32_t MediaKeySystemServiceProxy::GetConfiguration(IMediaKeySystemService::ConfigType configType, std::string &propertyName, std::string &value) +{ + DRM_INFO_LOG("MediaKeySystemServiceProxy::GetConfiguration enter, configType:%{public}d, propertyName:%{public}s.", (int)configType, propertyName.c_str()); + MessageParcel data; + MessageParcel reply; + MessageOption option; + + if (!data.WriteInterfaceToken(GetDescriptor())) { + DRM_ERR_LOG("MediaKeySystemServiceProxy GetConfiguration Write interface token failed"); + return IPC_PROXY_ERR; + } + + if (!data.WriteInt32((int)configType)) { + DRM_ERR_LOG("MediaKeySystemServiceProxy GetConfiguration Write RequestType failed"); + return IPC_PROXY_ERR; + } + + if (!data.WriteString(propertyName)) { + DRM_ERR_LOG("MediaKeySystemServiceProxy GetConfiguration Write response failed"); + return IPC_PROXY_ERR; + } + + int error = Remote()->SendRequest(MEDIA_KEY_SYSTEM_GETCONFIGURATION, data, reply, option); + if (error != ERR_NONE) { + DRM_ERR_LOG("MediaKeySystemServiceProxy GetConfiguration failed, error: %{public}d", error); + return error; + } + + value = reply.ReadString(); + + DRM_INFO_LOG("MediaKeySystemServiceProxy::GetConfiguration exit."); + return reply.ReadInt32(); +} + +int32_t MediaKeySystemServiceProxy::CreateKeySession(IKeySessionService::SecurityLevel securityLevel, sptr &keySessionProxy) +{ + DRM_INFO_LOG("MediaKeySystemServiceProxy::CreateKeySession enter, securityLevel:%{public}d.", securityLevel); + MessageParcel data; + MessageParcel reply; + MessageOption option; + + if (!data.WriteInterfaceToken(GetDescriptor())) { + DRM_ERR_LOG("MediaKeySystemServiceProxy CreateKeySession Write interface token failed"); + return IPC_PROXY_ERR; + } + if (!data.WriteInt32(securityLevel)) { + DRM_ERR_LOG("MediaKeySystemServiceProxy CreateKeySession Write format failed"); + return IPC_PROXY_ERR; + } + + int error = Remote()->SendRequest(MEDIA_KEY_SYSTEM_CREATE_KEY_SESSION, data, reply, option); + if (error != ERR_NONE) { + DRM_ERR_LOG("MediaKeySystemServiceProxy::CreateKeySession failed, error: %{public}d", error); + return error; + } + + auto remoteObject = reply.ReadRemoteObject(); + if (remoteObject != nullptr) { + keySessionProxy = iface_cast(remoteObject); + } else { + DRM_ERR_LOG("MediaKeySystemServiceProxy CreateKeySession keySessionProxy is nullptr"); + error = IPC_PROXY_ERR; + } + DRM_INFO_LOG("MediaKeySystemServiceProxy::CreateKeySession exit."); + return error; +} + +int32_t MediaKeySystemServiceProxy::GetMetric(std::vector &infoMap) +{ + DRM_INFO_LOG("MediaKeySystemServiceProxy::GetMetric enter."); + MessageParcel data; + MessageParcel reply; + MessageOption option; + + if (!data.WriteInterfaceToken(GetDescriptor())) { + DRM_ERR_LOG("MediaKeySystemServiceProxy GetMetric two params Write interface token failed"); + return IPC_PROXY_ERR; + } + + int error = Remote()->SendRequest(MEDIA_KEY_SYSTEM_GETMETRIC, data, reply, option); + if (error != ERR_NONE) { + DRM_ERR_LOG("MediaKeySystemServiceProxy::GetMetric failed, error: %{public}d", error); + return error; + } + int infoMapSize = reply.ReadInt32(); + for (int i = 0; i < infoMapSize; i++) { + IMediaKeySystemService::KeyValue keyValue; + keyValue.name = reply.ReadString(); + keyValue.value = reply.ReadString(); + infoMap.push_back(keyValue); + } + DRM_INFO_LOG("MediaKeySystemServiceProxy::GetMetric exit."); + return reply.ReadInt32(); +} + +} // DrmStandard +} // OHOS \ No newline at end of file diff --git a/services/drm_service/client/src/mediakeysystemfactory_service_proxy.cpp b/services/drm_service/client/src/mediakeysystemfactory_service_proxy.cpp new file mode 100644 index 00000000..b83c695a --- /dev/null +++ b/services/drm_service/client/src/mediakeysystemfactory_service_proxy.cpp @@ -0,0 +1,184 @@ +/* + * Copyright (c) 2023-2024 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "mediakeysystemfactory_service_proxy.h" +#include "remote_request_code.h" +#include "drm_log.h" +#include "drm_error_code.h" +#include + +namespace OHOS { +namespace DrmStandard { + +MediaKeySystemFactoryServiceProxy::MediaKeySystemFactoryServiceProxy(const sptr &impl) + : IRemoteProxy(impl) +{ + DRM_DEBUG_LOG("MediaKeySystemFactoryServiceProxy Initialized."); +} + +int32_t MediaKeySystemFactoryServiceProxy::IsMediaKeySystemSupported(std::string &uuid, bool *isSurpported) +{ + DRM_INFO_LOG("MediaKeySystemFactoryServiceProxy::IsMediaKeySystemSupported one param called, enter."); + MessageParcel data; + MessageParcel reply; + MessageOption option; + DRM_INFO_LOG("MediaKeySystemFactoryServiceProxy::IsMediaKeySystemSupported"); + if (!data.WriteInterfaceToken(GetDescriptor())) { + DRM_ERR_LOG("MediaKeySystemFactoryServiceProxy IsMediaKeySystemSupported one param Write interface token failed"); + return IPC_PROXY_ERR; + } + DRM_INFO_LOG("MediaKeySystemFactoryServiceProxy::IsMediaKeySystemSupported"); + + if (!data.WriteInt32(1)) { + DRM_ERR_LOG("MediaKeySystemFactoryServiceProxy IsMediaKeySystemSupported one param Write paramNum failed"); + return IPC_PROXY_ERR; + } + DRM_INFO_LOG("MediaKeySystemFactoryServiceProxy::IsMediaKeySystemSupported"); + + if (!data.WriteString(uuid)) { + DRM_ERR_LOG("MediaKeySystemFactoryServiceProxy IsMediaKeySystemSupported Write uuid failed"); + return IPC_PROXY_ERR; + } + DRM_INFO_LOG("MediaKeySystemFactoryServiceProxy::IsMediaKeySystemSupported"); + + int error = Remote()->SendRequest(MEDIA_KEY_SYSTEM_FACTORY_IS_MEDIA_KEY_SYSTEM_SURPPORTED, data, reply, option); + if (error != ERR_NONE) { + DRM_ERR_LOG("MediaKeySystemFactoryServiceProxy::IsMediaKeySystemSupported failed, error: %{public}d", error); + return error; + } + DRM_INFO_LOG("MediaKeySystemFactoryServiceProxy::IsMediaKeySystemSupported"); + + *isSurpported = reply.ReadBool(); + DRM_INFO_LOG("MediaKeySystemFactoryServiceProxy::IsMediaKeySystemSupported one param called, exit."); + + return error; +} + +int32_t MediaKeySystemFactoryServiceProxy::IsMediaKeySystemSupported(std::string &uuid, std::string &mimeType, bool *isSurpported) +{ + DRM_INFO_LOG("MediaKeySystemFactoryServiceProxy::IsMediaKeySystemSupported two param called, enter."); + MessageParcel data; + MessageParcel reply; + MessageOption option; + + if (!data.WriteInterfaceToken(GetDescriptor())) { + DRM_ERR_LOG("MediaKeySystemFactoryServiceProxy IsMediaKeySystemSupported two params Write interface token failed"); + return IPC_PROXY_ERR; + } + + if (!data.WriteInt32(2)) { + DRM_ERR_LOG("MediaKeySystemFactoryServiceProxy IsMediaKeySystemSupported two params Write paramNum failed"); + return IPC_PROXY_ERR; + } + + if (!data.WriteString(uuid)) { + DRM_ERR_LOG("MediaKeySystemFactoryServiceProxy IsMediaKeySystemSupported two params Write uuid failed"); + return IPC_PROXY_ERR; + } + + if (!data.WriteString(mimeType)) { + DRM_ERR_LOG("MediaKeySystemFactoryServiceProxy IsMediaKeySystemSupported two params Write mimeType failed"); + return IPC_PROXY_ERR; + } + + int error = Remote()->SendRequest(MEDIA_KEY_SYSTEM_FACTORY_IS_MEDIA_KEY_SYSTEM_SURPPORTED, data, reply, option); + if (error != ERR_NONE) { + DRM_ERR_LOG("MediaKeySystemFactoryServiceProxy::IsMediaKeySystemSupported failed, error: %{public}d", error); + return error; + } + + *isSurpported = reply.ReadBool(); + DRM_INFO_LOG("MediaKeySystemFactoryServiceProxy::IsMediaKeySystemSupported two param called, exit."); + return error; +} + +int32_t MediaKeySystemFactoryServiceProxy::IsMediaKeySystemSupported(std::string &uuid, std::string &mimeType, int32_t securityLevel, bool *isSurpported) +{ + DRM_INFO_LOG("MediaKeySystemFactoryServiceProxy::IsMediaKeySystemSupported three param called, enter."); + MessageParcel data; + MessageParcel reply; + MessageOption option; + + if (!data.WriteInterfaceToken(GetDescriptor())) { + DRM_ERR_LOG("MediaKeySystemFactoryServiceProxy IsMediaKeySystemSupported three params Write interface token failed"); + return IPC_PROXY_ERR; + } + + if (!data.WriteInt32(3)) { + DRM_ERR_LOG("MediaKeySystemFactoryServiceProxy IsMediaKeySystemSupported three params Write paramNum failed"); + return IPC_PROXY_ERR; + } + + if (!data.WriteString(uuid)) { + DRM_ERR_LOG("MediaKeySystemFactoryServiceProxy IsMediaKeySystemSupported three params Write uuid failed"); + return IPC_PROXY_ERR; + } + + if (!data.WriteString(mimeType)) { + DRM_ERR_LOG("MediaKeySystemFactoryServiceProxy IsMediaKeySystemSupported three params Write mimeType failed"); + return IPC_PROXY_ERR; + } + + if (!data.WriteInt32(securityLevel)) { + DRM_ERR_LOG("MediaKeySystemFactoryServiceProxy IsMediaKeySystemSupported three params Write securityLevel failed"); + return IPC_PROXY_ERR; + } + + int error = Remote()->SendRequest(MEDIA_KEY_SYSTEM_FACTORY_IS_MEDIA_KEY_SYSTEM_SURPPORTED, data, reply, option); + if (error != ERR_NONE) { + DRM_ERR_LOG("MediaKeySystemFactoryServiceProxy::IsMediaKeySystemSupported failed, error: %{public}d", error); + return error; + } + + *isSurpported = reply.ReadBool(); + DRM_INFO_LOG("MediaKeySystemFactoryServiceProxy::IsMediaKeySystemSupported three param called, exit."); + + return error; +} + +int32_t MediaKeySystemFactoryServiceProxy::CreateMediaKeySystem(std::string &uuid) +{ + DRM_INFO_LOG("MediaKeySystemFactoryServiceProxy::CreateMediaKeySystem enter."); + MessageParcel data; + MessageParcel reply; + MessageOption option; + std::wstring_convert, char16_t> converter; + if (!data.WriteInterfaceToken(GetDescriptor())) { + DRM_ERR_LOG("MediaKeySystemFactoryServiceProxy CreateMediaKeySystem Write interface token failed"); + return IPC_PROXY_ERR; + } + + if (!data.WriteString(uuid)) { + DRM_ERR_LOG("MediaKeySystemFactoryServiceProxy CreateMediaKeySystem Write format failed"); + return IPC_PROXY_ERR; + } + + int error = Remote()->SendRequest(MEDIA_KEY_SYSTEM_FACTORY_CREATE_MEDIA_KEYSYSTEM, data, reply, option); + if (error != ERR_NONE) { + DRM_ERR_LOG("MediaKeySystemFactoryServiceProxy::CreateMediaKeySystem failed, error: %{public}d", error); + return error; + } + + auto remoteObject = reply.ReadRemoteObject(); + if (remoteObject != nullptr) { + } else { + DRM_ERR_LOG("MediaKeySystemFactoryServiceProxy::CreateMediaKeySystem mediaKeySystemProxy is nullptr"); + error = IPC_PROXY_ERR; + } + DRM_INFO_LOG("MediaKeySystemFactoryServiceProxy::CreateMediaKeySystem exit."); + return error; +} +} // DrmStandard +} // OHOS \ No newline at end of file diff --git a/services/drm_service/ipc/drm_death_recipient.h b/services/drm_service/ipc/drm_death_recipient.h new file mode 100644 index 00000000..ce1efb49 --- /dev/null +++ b/services/drm_service/ipc/drm_death_recipient.h @@ -0,0 +1,38 @@ +#ifndef OHOS_DRM_DRM_DEATH_RECIPIENT_H +#define OHOS_DRM_DRM_DEATH_RECIPIENT_H + +#include "iremote_object.h" +#include "nocopyable.h" + +namespace OHOS { +namespace DrmStandard { + +class DrmDeathRecipient : public IRemoteObject::DeathRecipient, public NoCopyable { +public: + explicit DrmDeathRecipient(pid_t pid) : pid_(pid) {} + virtual ~DrmDeathRecipient() = default; + + void OnRemoteDied(const wptr &remote) override + { + (void) remote; + if (diedCb_ != nullptr) { + diedCb_(pid_); + } + } + + using NotifyCbFunc = std::function; + + void SetNotifyCb(NotifyCbFunc func) + { + diedCb_ = func; + } + +private: + pid_t pid_ = 0; + NotifyCbFunc diedCb_ = nullptr; +}; + +} // DrmStandard +} // OHOS + +#endif \ No newline at end of file diff --git a/services/drm_service/ipc/i_keysession_service.h b/services/drm_service/ipc/i_keysession_service.h new file mode 100644 index 00000000..a6b762ce --- /dev/null +++ b/services/drm_service/ipc/i_keysession_service.h @@ -0,0 +1,109 @@ +/* + * Copyright (c) 2023-2024 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef OHOS_DRM_IKEY_SESSION_H_ +#define OHOS_DRM_IKEY_SESSION_H_ + +#include "i_mediadecryptmodule_service.h" +#include "i_keysession_service_callback.h" +#include "iremote_broker.h" + +namespace OHOS { +namespace DrmStandard { + +class IKeySessionService : public IRemoteBroker { +public: + enum ListenerType { + LISTENER_DRM_EVENT = 200, + LISTENER_PROVISION_REQUIRED = 201, + LISTENER_KEY_NEEDED = 202, + LISTENER_KEY_EXPIRED = 203, + LISTENER_VENDOR_DEFINED = 204, + LISTENER_KEYSESSION_RECLAIMED = 205, + LISTENER_EXPIRATION_UPDATE = 206, + LISTENER_KEY_CHANGE = 207, + LISTENER_KEYSESSION_LOSE = 208, + }; + + struct KeyValue { + std::string name; + std::string value; + }; + + enum KeyType { + KEYTYPE_OFFLINE = 0, + KEYTYPE_ONLINE, + }; + + enum OfflineKeyState { + OFFLINEKEYSTATE_UNKNOWN = 0, + OFFLINEKEYSTATE_USABLE = 1, + OFFLINEKEYSTATE_INACTIVE = 2, + }; + + enum RequestType { + REQUEST_TYPE_UNKNOWN = 0, + REQUEST_TYPE_INITIAL = 1, + REQUEST_TYPE_RENEWAL = 2, + REQUEST_TYPE_RELEASE = 3, + REQUEST_TYPE_NONE = 4, + REQUEST_TYPE_UPDATE = 5, + REQUEST_TYPE_DOWNLOADCERT = 6, + }; + + enum SecurityLevel { + SECURITY_LEVEL_UNKNOWN = 0, + SECURITY_LEVEL_SW_CRYPTO = 1, + SECURITY_LEVEL_SW_DECODE = 2, + SECURITY_LEVEL_HW_CRYPTO = 3, + SECURITY_LEVEL_HW_DECODE = 4, + SECURITY_LEVEL_HW_ALL = 5, + SECURITY_LEVEL_MAX = 6, + }; + + struct DrmInfo { + KeyType keyType; + std::string mimeType; + std::vector indexInfo; + }; + + struct LicenseInfo { + RequestType requestType; + std::vector mData; + std::string mDefaultURL; + }; + + virtual ~IKeySessionService() = default; + virtual int32_t Release() = 0; + virtual int32_t CreateMediaDecryptModule(sptr &decryptModule) = 0; + virtual int32_t GenerateLicenseRequest(DrmInfo &drmInfo, LicenseInfo &licenseInfo) = 0; + virtual int32_t ProcessLicenseResponse(std::vector &keyId, std::vector &licenseResponse) = 0; + virtual int32_t GenerateOfflineReleaseRequest(std::vector &keyId, std::vector &releaseRequest) = 0; + virtual int32_t ProcessOfflineReleaseResponse(std::vector &keyId, std::vector &releaseReponse) = 0; + virtual int32_t CheckLicenseStatus(std::vector &infoMap) = 0; + virtual int32_t RestoreOfflineKeys(std::vector &keyId) = 0; + virtual int32_t RemoveOfflineKeys(std::vector &keyId) = 0; + virtual int32_t GetOfflineKeyIds(std::vector> &keyIds) = 0; + virtual int32_t RemoveLicenses() = 0; + virtual int32_t GetOfflineKeyState(std::vector &keyId, IKeySessionService::OfflineKeyState &state) = 0; + virtual int32_t SetKeySessionServiceCallback(sptr &callback) = 0; + DECLARE_INTERFACE_DESCRIPTOR(u"IKeySessionService"); +}; + +} // DrmStandard +} // OHOS + + +#endif // OHOS_DRM_IKEY_SESSION_H_ \ No newline at end of file diff --git a/services/drm_service/ipc/i_keysession_service_callback.h b/services/drm_service/ipc/i_keysession_service_callback.h new file mode 100644 index 00000000..60a148f3 --- /dev/null +++ b/services/drm_service/ipc/i_keysession_service_callback.h @@ -0,0 +1,45 @@ +/* + * Copyright (c) 2023-2024 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef OHOS_DRM_IKEY_SESSION_SERVICE_CALLBACK_H_ +#define OHOS_DRM_IKEY_SESSION_SERVICE_CALLBACK_H_ + +#include "iremote_broker.h" + +namespace OHOS { +namespace DrmStandard { + +enum KeyStatus { + KEY_STATUS_NORMAL = 0, + KEY_STATUS_EXPIRED, +}; + +enum SessionStatus { + SESSION_STATUS_NORMAL = 0, + SESSION_STATUS_RECLAIMED, +}; + +class IKeySessionServiceCallback : public IRemoteBroker { +public: + virtual int32_t OnKeySessionKeyExpired(const KeyStatus status) = 0; + virtual int32_t OnKeySessionReclaimed(const SessionStatus status) = 0; + + DECLARE_INTERFACE_DESCRIPTOR(u"IKeySessionServiceCallback"); +}; + +} // namespace DrmStandard +} // namespace OHOS + +#endif \ No newline at end of file diff --git a/services/drm_service/ipc/i_mediadecryptmodule_service.h b/services/drm_service/ipc/i_mediadecryptmodule_service.h new file mode 100644 index 00000000..864abf13 --- /dev/null +++ b/services/drm_service/ipc/i_mediadecryptmodule_service.h @@ -0,0 +1,63 @@ +/* + * Copyright (c) 2023-2024 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef OHOS_DRM_I_MEDIA_DECRYPT_MODULE_SERVICE_H +#define OHOS_DRM_I_MEDIA_DECRYPT_MODULE_SERVICE_H + +#include "i_mediadecryptmodule_service.h" +#include "ipc_types.h" +#include "iremote_broker.h" +#include "iremote_proxy.h" +#include "iremote_stub.h" + +namespace OHOS { +namespace DrmStandard { +class IMediaDecryptModuleService : public IRemoteBroker { +public: + struct SubSample { + int32_t clearHeaderLen; + int32_t payLoadLen; + }; + + struct Pattern { + int32_t encryptBlocks; + int32_t skipBlocks; + }; + + enum CryptAlgorithmType { + ALGTYPE_UNENCRYPTED, + ALGTYPE_AES_CTR, + ALGTYPE_AES_WV, + ALGTYPE_AES_CBC, + ALGTYPE_SM4_CBC, + }; + + struct CryptInfo { + CryptAlgorithmType type; + std::vector keyId; + std::vector iv; + Pattern pattern; + std::vector subSample; + }; + + virtual ~IMediaDecryptModuleService() = default; + virtual int32_t Release() = 0; + virtual int32_t DecryptData(bool secureDecodrtState, IMediaDecryptModuleService::CryptInfo &cryptInfo, uint64_t srcBuffer, uint64_t dstBuffer) = 0; + virtual int32_t RequireSecureDecoderModule(std::string &mimeType, bool *status) = 0; + DECLARE_INTERFACE_DESCRIPTOR(u"IMediaDecryptModuleService"); +}; +} // DrmStandard +} // OHOS +#endif // OHOS_DRM_I_MEDIA_DECRYPT_MODULE_SERVICE_H \ No newline at end of file diff --git a/services/drm_service/ipc/i_mediakeysystem_service.h b/services/drm_service/ipc/i_mediakeysystem_service.h new file mode 100644 index 00000000..30fdcc39 --- /dev/null +++ b/services/drm_service/ipc/i_mediakeysystem_service.h @@ -0,0 +1,75 @@ +/* + * Copyright (c) 2023-2024 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef OHOS_DRM_I_MEDIA_KEY_SYSTEM_SERVICE_H +#define OHOS_DRM_I_MEDIA_KEY_SYSTEM_SERVICE_H + +#include "i_keysession_service.h" +#include "ipc_types.h" +#include "iremote_broker.h" +#include "iremote_proxy.h" +#include "iremote_stub.h" + +namespace OHOS { +namespace DrmStandard { +class IMediaKeySystemService : public IRemoteBroker { +public: + enum RequestType { + REQUEST_TYPE_UNKNOWN = 0, + REQUEST_TYPE_INITIAL = 1, + REQUEST_TYPE_RENEWAL = 2, + REQUEST_TYPE_RELEASE = 3, + REQUEST_TYPE_NONE = 4, + REQUEST_TYPE_UPDATE = 5, + REQUEST_TYPE_DOWNLOADCERT = 6, + }; + + enum HdcpLevel { + HDCP_UNKNOWN, + HDCP_NONE, + HDCP_V1, + HDCP_V2, + HDCP_V2_1, + HDCP_V2_2, + HDCP_V2_3, + HDCP_NO_OUTPUT = 0x7fff, + }; + + enum ConfigType { + CONFIGTYPE_DEVICEPROPERTY = 0, + CONFIGTYPE_KEYSESSION = 1, + CONFIGTYPE_OUTPUTPROTECTTYPE = 2, + }; + + struct KeyValue { + std::string name; + std::string value; + }; + + virtual ~IMediaKeySystemService() = default; + virtual int32_t Release() = 0; + virtual int32_t GenerateKeySystemRequest(RequestType type, std::vector &request, std::string &defaultUrl) = 0; + virtual int32_t ProcessKeySystemResponse(RequestType type, const std::vector &response) = 0; + virtual int32_t SetConfiguration(ConfigType type, std::string &propertyName, std::string &value) = 0; + virtual int32_t GetConfiguration(ConfigType type, std::string &propertyName, std::string &value) = 0; + virtual int32_t CreateKeySession(IKeySessionService::SecurityLevel securityLevel, sptr &keySessionProxy) = 0; + virtual int32_t GetMetric(std::vector &infoMap) = 0; + virtual int32_t GetSecurityLevel(IKeySessionService::SecurityLevel *securityLevel) = 0; + + DECLARE_INTERFACE_DESCRIPTOR(u"IMediaKeySystemSystemService"); +}; +} // DrmStandard +} // OHOS +#endif // OHOS_DRM_I_MEDIA_KEY_SYSTEM_SERVICE_H diff --git a/services/drm_service/ipc/i_mediakeysystemfactory_service.h b/services/drm_service/ipc/i_mediakeysystemfactory_service.h new file mode 100644 index 00000000..9876a640 --- /dev/null +++ b/services/drm_service/ipc/i_mediakeysystemfactory_service.h @@ -0,0 +1,38 @@ +/* + * Copyright (c) 2023-2024 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef OHOS_DRM_I_MEDIA_KEY_SYSTEM_FACTORY_SERVICE_H +#define OHOS_DRM_I_MEDIA_KEY_SYSTEM_FACTORY_SERVICE_H + +#include "i_mediakeysystem_service.h" +#include "ipc_types.h" +#include "iremote_broker.h" +#include "iremote_proxy.h" +#include "iremote_stub.h" + +namespace OHOS { +namespace DrmStandard { +class IMediaKeySystemFactoryService : public IRemoteBroker { +public: + virtual ~IMediaKeySystemFactoryService() = default; + virtual int32_t IsMediaKeySystemSupported(std::string &uuid, bool *isSurpported) = 0; + virtual int32_t IsMediaKeySystemSupported(std::string &uuid, std::string &mimeType, bool *isSurpported) = 0; + virtual int32_t IsMediaKeySystemSupported(std::string &uuid, std::string &mimeType, int32_t securityLevel, bool *isSurpported) = 0; + virtual int32_t CreateMediaKeySystem(std::string &uuid) = 0; + DECLARE_INTERFACE_DESCRIPTOR(u"IMediaKeySystemSystemFactoryService"); +}; +} // DrmStandard +} // OHOS +#endif // OHOS_DRM_I_MEDIA_KEY_SYSTEM_FACTORY_SERVICE_H diff --git a/services/drm_service/ipc/i_standard_mediakeysystem_listener.h b/services/drm_service/ipc/i_standard_mediakeysystem_listener.h new file mode 100644 index 00000000..e69de29b diff --git a/services/drm_service/ipc/remote_request_code.h b/services/drm_service/ipc/remote_request_code.h new file mode 100644 index 00000000..3700ec22 --- /dev/null +++ b/services/drm_service/ipc/remote_request_code.h @@ -0,0 +1,70 @@ +/* + * Copyright (c) 2023-2024 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef OHOS_DRM_REMOTE_REQUEST_CODE_H +#define OHOS_DRM_REMOTE_REQUEST_CODE_H + +namespace OHOS { +namespace DrmStandard { + +enum MediaKeySystemFactoryServiceRequestCode { + MEDIA_KEY_SYSTEM_FACTORY_IS_MEDIA_KEY_SYSTEM_SURPPORTED = 0, + MEDIA_KEY_SYSTEM_FACTORY_CREATE_MEDIA_KEYSYSTEM = 1, +}; + +enum MediaKeySystemServiceRequestCode { + MEDIA_KEY_SYSTEM_CREATE_KEY_SESSION = 0, + MEDIA_KEY_SYSTEM_DESTROY_KEY_SESSION = 1, + MEDIA_KEY_SYSTEM_GENERATE_KEYSYSTEM_REQUEST = 2, + MEDIA_KEY_SYSTEM_GENERATE_KEYSYSTEM_RESPONSE = 3, + MEDIA_KEY_SYSTEM_SETCONFIGURATION = 4, + MEDIA_KEY_SYSTEM_GETCONFIGURATION = 5, + MEDIA_KEY_SYSTEM_GETMETRIC = 6, + MEDIA_KEY_SYSTEM_RELEASE = 7, + MEDIA_KEY_SYSTEM_GETSECURITYLEVEL = 8, +}; + +enum KeySessionServiceRequestCode { + CREATE_MEDIA_DECRYPT_MODULE = 0, + RELEASE_MEDIA_DECRYPT_MODULE = 1, + KEY_SESSION_RELEASE = 2, + MEDIA_KEY_SESSION_GENERATE_LICENSE_REQUEST = 3, + MEDIA_KEY_SESSION_PROCESS_LICENSE_RESPONSE = 4, + MEDIA_KEY_SESSION_GENERATE_OFFLINE_RELEASE_REQUEST = 5, + MEDIA_KEY_SESSION_PROCESS_OFFLINE_RELEASE_RESPONSE = 6, + MEDIA_KEY_SESSION_GENERATE_CHECK_LICENSE_STATUS = 7, + MEDIA_KEY_SESSION_RESTORE_OFFLINEKEYS = 8, + MEDIA_KEY_SESSION_REMOVE_OFFLINEKEYS = 9, + MEDIA_KEY_SESSION_GET_OFFLINEKEYIDS = 10, + MEDIA_KEY_SESSION_REMOVE_LICENSE = 11, + MEDIA_KEY_SESSION_GET_OFFLINEKEY_STATE = 12, + MEDIA_KEY_SESSION_SET_CALLBACK = 13, +}; + +enum MediaDecryptModuleServiceRequestCode { + DECRYPT_MODULE_DECRYPT_DATA = 0, + DECRYPT_MODULE_REQUIRE_SECURE_DECODER = 1, + DECRYPT_MODULE_RELEASE = 2, +}; + +enum KeySessionServiceCallbackRequestCode { + KEY_SESSION_SERVICE_CALLBACK_ON_KEY_EXPIRED = 0, + KEY_SESSION_SERVICE_CALLBACK_ON_KEY_SESSION_RECLAIMED, +}; + +} // namespace DrmStandard +} // namespace OHOS + +#endif // OHOS_DRM_REMOTE_REQUEST_CODE_H diff --git a/services/drm_service/server/include/drm_host_manager.h b/services/drm_service/server/include/drm_host_manager.h new file mode 100644 index 00000000..c03511b7 --- /dev/null +++ b/services/drm_service/server/include/drm_host_manager.h @@ -0,0 +1,49 @@ +#ifndef OHOS_DRM_DRM_HOST_MANAGER_H +#define OHOS_DRM_DRM_HOST_MANAGER_H +#include +#include +#include +#include +#include +#include +// #include "drm_interface_proxy.h" +// #include "drm_impl.h" +// #include "drm_types.h" +#include "i_mediakeysystem_service.h" +#include "iservstat_listener_hdi.h" + +namespace OHOS{ +namespace DrmStandard{ + +class DrmHostManager : public virtual RefBase, public HDI::ServiceManager::V1_0::ServStatListenerStub { +public: + class StatusCallback{ + public: + virtual ~StatusCallback() = default; + }; + class DrmHostDeathRecipient : public IRemoteObject::DeathRecipient { + public: + explicit DrmHostDeathRecipient() {}; + virtual ~DrmHostDeathRecipient() = default; + void OnRemoteDied(const wptr &remote) override; + }; + + explicit DrmHostManager(StatusCallback* statusCallback); + ~DrmHostManager() override; + + int32_t Init(void); + void DeInit(void); + void OnReceive(const HDI::ServiceManager::V1_0::ServiceStatus& status) override; + int32_t IsMediaKeySystemSupported(std::string &uuid, bool *isSurpported); + int32_t IsMediaKeySystemSupported(std::string &uuid, std::string &mimeType, bool *isSurpported); + int32_t IsMediaKeySystemSupported(std::string &uuid, std::string &mimeType, int32_t securityLevel, bool *isSurpported); + int32_t CreateMediaKeySystem(std::string &uuid); +private: + int32_t GetSevices(std::string &uuid, bool *isSurpported); +}; + +} // DrmStandard +} // OHOS + + +#endif //OHOS_DRM_DRM_HOST_MANAGER_H \ No newline at end of file diff --git a/services/drm_service/server/include/key_session_service.h b/services/drm_service/server/include/key_session_service.h new file mode 100644 index 00000000..8256e733 --- /dev/null +++ b/services/drm_service/server/include/key_session_service.h @@ -0,0 +1,65 @@ +/* + * Copyright (c) 2023-2024 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef OHOS_DRM_KEY_SESSION_SERVICE_H +#define OHOS_DRM_KEY_SESSION_SERVICE_H + +#include "key_session_service_stub.h" +#include "drm_log.h" +#include "drm_napi_utils.h" +#include "drm_host_manager.h" +#include +#include + +#include "media_decrypt_module_service.h" +namespace OHOS { +namespace DrmStandard { +class IKeySessionServiceOperatorsCallback; +class KeySessionService : public KeySessionServiceStub { +public: + explicit KeySessionService(); + ~KeySessionService(); + int32_t Release(); + int32_t CreateMediaDecryptModule(sptr &decryptModule); + int32_t SetKeySessionServiceOperatorsCallback(wptr callback); + int32_t CloseKeySessionServiceByCallback(); + int32_t GenerateLicenseRequest(IKeySessionService::DrmInfo &drmInfo, IKeySessionService::LicenseInfo &licenseInfo); + int32_t ProcessLicenseResponse(std::vector &keyId, std::vector &licenseResponse); + int32_t GenerateOfflineReleaseRequest(std::vector &keyId, std::vector &releaseRequest); + int32_t ProcessOfflineReleaseResponse(std::vector &keyId, std::vector &releaseResponse); + int32_t CheckLicenseStatus(std::vector &infoMap); + int32_t RestoreOfflineKeys(std::vector &keyId); + int32_t RemoveOfflineKeys(std::vector &keyId); + int32_t GetOfflineKeyIds(std::vector> &keyIds); + int32_t RemoveLicenses(); + int32_t GetOfflineKeyState(std::vector &keyId, OfflineKeyState &state); + int32_t SetKeySessionServiceCallback(sptr &callback); + void OnKeySessionKeyExpiredStatus(const KeyStatus status); + void OnKeySessionReclaimed(const SessionStatus status); + +private: +}; + +class IKeySessionServiceOperatorsCallback : public virtual RefBase { +public: + IKeySessionServiceOperatorsCallback() = default; + virtual ~IKeySessionServiceOperatorsCallback() = default; + virtual int32_t CloseKeySessionService(sptr sessionService) = 0; +}; + +} // DrmStandard +} // OHOS + +#endif // OHOS_DRM_KEY_SESSION_SERVICE_H \ No newline at end of file diff --git a/services/drm_service/server/include/key_session_service_callback_stub.h b/services/drm_service/server/include/key_session_service_callback_stub.h new file mode 100644 index 00000000..95461c89 --- /dev/null +++ b/services/drm_service/server/include/key_session_service_callback_stub.h @@ -0,0 +1,39 @@ +/* + * Copyright (c) 2023-2024 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef OHOS_DRM_KEY_SESSION_SERVICE_CALLBACK_STUB_H +#define OHOS_DRM_KEY_SESSION_SERVICE_CALLBACK_STUB_H + +#include "i_keysession_service_callback.h" +#include "iremote_stub.h" +#include "remote_request_code.h" + +namespace OHOS{ +namespace DrmStandard{ +class KeySessionServiceCallbackStub : public IRemoteStub { +public: + KeySessionServiceCallbackStub() = default; + ~KeySessionServiceCallbackStub() = default; + virtual int OnRemoteRequest(uint32_t code, MessageParcel &data, + MessageParcel &reply, MessageOption &option) override; + +private: + int32_t HandleOnKeySessionKeyExpired(MessageParcel& data); + int32_t HandleOnKeySessionReclaimed(MessageParcel& data); +}; + +}//namespace DrmStandard +}//namespace OHOS +#endif // OHOS_DRM_KEY_SESSION_SERVICE_CALLBACK_STUB_H \ No newline at end of file diff --git a/services/drm_service/server/include/key_session_service_stub.h b/services/drm_service/server/include/key_session_service_stub.h new file mode 100644 index 00000000..b0361d00 --- /dev/null +++ b/services/drm_service/server/include/key_session_service_stub.h @@ -0,0 +1,40 @@ +/* + * Copyright (c) 2023-2024 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef OHOS_DRM_KEY_SESSION_STUB_H +#define OHOS_DRM_KEY_SESSION_STUB_H + +#include +#include +#include "i_keysession_service.h" +#include "iremote_stub.h" +#include "remote_request_code.h" + +namespace OHOS{ +namespace DrmStandard{ +class KeySessionServiceStub : public IRemoteStub { +public: + KeySessionServiceStub() = default; + ~KeySessionServiceStub() = default; + virtual int32_t OnRemoteRequest(uint32_t code, MessageParcel &data, + MessageParcel &reply, MessageOption &option) override; + +private: + +}; + +}//namespace DrmStandard +}//namespace OHOS +#endif // OHOS_DRM_MEDIA_KEY_SYSTEM_SERVICE_STUB_H \ No newline at end of file diff --git a/services/drm_service/server/include/media_decrypt_module_service.h b/services/drm_service/server/include/media_decrypt_module_service.h new file mode 100644 index 00000000..62a5eeb8 --- /dev/null +++ b/services/drm_service/server/include/media_decrypt_module_service.h @@ -0,0 +1,44 @@ +/* + * Copyright (c) 2023-2024 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#ifndef OHOS_DRM_MediaDecryptModuleService_H_ +#define OHOS_DRM_MediaDecryptModuleService_H_ + +#include "media_decrypt_module_service_stub.h" +#include "drm_host_manager.h" +#include "drm_log.h" +#include "drm_error_code.h" +#include +#include + +namespace OHOS { +namespace DrmStandard { + +class MediaDecryptModuleService : public MediaDecryptModuleServiceStub +{ +public: + MediaDecryptModuleService(); + ~MediaDecryptModuleService(); + int32_t Release() override; + int32_t DecryptData(bool secureDecodrtState, IMediaDecryptModuleService::CryptInfo &cryptInfo, uint64_t srcBuffer, + uint64_t dstBuffer) override; + int32_t RequireSecureDecoderModule(std::string &mimeType, bool *status) override; + +private: +}; + +} // DrmStandard +} // OHOS + +#endif //OHOS_DRM_MediaDecryptModuleService_H_ \ No newline at end of file diff --git a/services/drm_service/server/include/media_decrypt_module_service_stub.h b/services/drm_service/server/include/media_decrypt_module_service_stub.h new file mode 100644 index 00000000..eb5e5a1b --- /dev/null +++ b/services/drm_service/server/include/media_decrypt_module_service_stub.h @@ -0,0 +1,43 @@ +/* + * Copyright (c) 2023-2024 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef OHOS_DRM_MEDIA_DECRYPT_MODULE_SERVICE_STUB_H_ +#define OHOS_DRM_MEDIA_DECRYPT_MODULE_SERVICE_STUB_H_ + +#include "i_mediadecryptmodule_service.h" +#include "remote_request_code.h" +#include "iremote_stub.h" + +#include +#include +#include +#include + +namespace OHOS{ +namespace DrmStandard{ +class MediaDecryptModuleServiceStub : public IRemoteStub { +public: + MediaDecryptModuleServiceStub(); + ~MediaDecryptModuleServiceStub(); + virtual int OnRemoteRequest(uint32_t code, MessageParcel &data, + MessageParcel &reply, MessageOption &option) override; + +private: +}; + +}//namespace DrmStandard +}//namespace OHOS + +#endif //OHOS_DRM_MEDIA_DECRYPT_MODULE_SERVICE_STUB_H_ \ No newline at end of file diff --git a/services/drm_service/server/include/mediakeysystem_service.h b/services/drm_service/server/include/mediakeysystem_service.h new file mode 100644 index 00000000..eb31498d --- /dev/null +++ b/services/drm_service/server/include/mediakeysystem_service.h @@ -0,0 +1,72 @@ +/* + * Copyright (c) 2023-2024 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef OHOS_DRM_MEDIA_KEY_SYSTEM_SERVICE_H +#define OHOS_DRM_MEDIA_KEY_SYSTEM_SERVICE_H + +#include "mediakeysystem_service_stub.h" +#include "key_session_service.h" +#include "drm_host_manager.h" +#include "drm_log.h" +#include "drm_error_code.h" +#include "iremote_stub.h" +#include "system_ability.h" + +#include "safe_map.h" +#include +#include +#include +#include +#include +#include +#include +#include + +namespace OHOS { +namespace DrmStandard { +class IMediaKeySystemServiceOperatorsCallback; +class MediaKeySystemService : public MediaKeySystemServiceStub, public DrmHostManager::StatusCallback, + public IKeySessionServiceOperatorsCallback { +public: + explicit MediaKeySystemService(); + ~MediaKeySystemService() override; + int32_t Release() override; + int32_t SetMediaKeySystemServiceOperatorsCallback(wptr callback); + int32_t CloseMediaKeySystemServiceByCallback(); + + int32_t GenerateKeySystemRequest(IMediaKeySystemService::RequestType type, std::vector &request, std::string &defaultUrl) override; + int32_t ProcessKeySystemResponse(IMediaKeySystemService::RequestType type, const std::vector &response) override; + int32_t SetConfiguration(IMediaKeySystemService::ConfigType type, std::string &propertyName, std::string &value) override; + int32_t GetConfiguration(IMediaKeySystemService::ConfigType type, std::string &propertyName, std::string &value) override; + int32_t CreateKeySession(IKeySessionService::SecurityLevel securityLevel, sptr &keySessionProxy) override; + //IMediaKeySystemServiceOperatorsCallback + int32_t CloseKeySessionService(sptr sessionService) override; + int32_t GetMetric(std::vector &infoMap) override; + int32_t GetSecurityLevel(IKeySessionService::SecurityLevel *securityLevel) override; + +private: +}; + +class IMediaKeySystemServiceOperatorsCallback : public virtual RefBase { +public: + IMediaKeySystemServiceOperatorsCallback() = default; + virtual ~IMediaKeySystemServiceOperatorsCallback() = default; + virtual int32_t CloseMediaKeySystemService(sptr mediaKeySystemService) = 0; +}; + +} // DrmStandard +} // OHOS + +#endif // OHOS_DRM_MEDIA_KEY_SYSTEM_SERVICE_H \ No newline at end of file diff --git a/services/drm_service/server/include/mediakeysystem_service_stub.h b/services/drm_service/server/include/mediakeysystem_service_stub.h new file mode 100644 index 00000000..51cc1578 --- /dev/null +++ b/services/drm_service/server/include/mediakeysystem_service_stub.h @@ -0,0 +1,43 @@ +/* + * Copyright (c) 2023-2024 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef OHOS_DRM_MEDIA_KEY_SYSTEM_SERVICE_STUB_H +#define OHOS_DRM_MEDIA_KEY_SYSTEM_SERVICE_STUB_H + +#include +#include +#include +#include +#include "i_mediakeysystem_service.h" +#include "iremote_stub.h" +#include "drm_death_recipient.h" +#include "remote_request_code.h" + +namespace OHOS{ +namespace DrmStandard{ +class MediaKeySystemServiceStub : public IRemoteStub { +public: + MediaKeySystemServiceStub(); + ~MediaKeySystemServiceStub(); + virtual int OnRemoteRequest(uint32_t code, MessageParcel &data, + MessageParcel &reply, MessageOption &option) override; + +private: + std::map> deathRecipientMap_; +}; + +}//namespace DrmStandard +}//namespace OHOS +#endif // OHOS_DRM_MEDIA_KEY_SYSTEM_SERVICE_STUB_H \ No newline at end of file diff --git a/services/drm_service/server/include/mediakeysystemfactory_service.h b/services/drm_service/server/include/mediakeysystemfactory_service.h new file mode 100644 index 00000000..5de91058 --- /dev/null +++ b/services/drm_service/server/include/mediakeysystemfactory_service.h @@ -0,0 +1,59 @@ +/* + * Copyright (c) 2023-2024 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef OHOS_DRM_MEDIA_KEY_SYSTEM_FACTORY_SERVICE_H +#define OHOS_DRM_MEDIA_KEY_SYSTEM_FACTORY_SERVICE_H + +#include "mediakeysystemfactory_service_stub.h" +#include "mediakeysystem_service.h" +#include "drm_host_manager.h" +#include "drm_log.h" +#include "drm_error_code.h" +#include "iremote_stub.h" +#include "system_ability.h" + +#include "safe_map.h" +#include +#include +#include +#include +#include +#include +#include +#include + +namespace OHOS { +namespace DrmStandard { +class MediaKeySystemFactoryService : public SystemAbility, public MediaKeySystemFactoryServiceStub, public DrmHostManager::StatusCallback, + public IMediaKeySystemServiceOperatorsCallback { + DECLARE_SYSTEM_ABILITY(MediaKeySystemFactoryService); +public: + DISALLOW_COPY_AND_MOVE(MediaKeySystemFactoryService); + explicit MediaKeySystemFactoryService(int32_t systemAbilityId, bool runOnCreate = true); + ~MediaKeySystemFactoryService() override; + void OnDump() override; + void OnStart() override; + void OnStop() override; + int32_t IsMediaKeySystemSupported(std::string &uuid, bool *isSurpported) override; + int32_t IsMediaKeySystemSupported(std::string &uuid, std::string &mimeType, bool *isSurpported) override; + int32_t IsMediaKeySystemSupported(std::string &uuid, std::string &mimeType, int32_t securityLevel, bool *isSurpported) override; + int32_t CreateMediaKeySystem(std::string &uuid) override; + int32_t CloseMediaKeySystemService(sptr mediaKeySystemService) override; +private: +}; +} // DrmStandard +} // OHOS + +#endif // OHOS_DRM_MEDIA_KEY_SYSTEM_FACTORY_SERVICE_H \ No newline at end of file diff --git a/services/drm_service/server/include/mediakeysystemfactory_service_stub.h b/services/drm_service/server/include/mediakeysystemfactory_service_stub.h new file mode 100644 index 00000000..7a65923a --- /dev/null +++ b/services/drm_service/server/include/mediakeysystemfactory_service_stub.h @@ -0,0 +1,43 @@ +/* + * Copyright (c) 2023-2024 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef OHOS_DRM_MEDIA_KEY_SYSTEM_FACTORY_SERVICE_STUB_H +#define OHOS_DRM_MEDIA_KEY_SYSTEM_FACTORY_SERVICE_STUB_H + +#include +#include +#include +#include +#include "i_mediakeysystemfactory_service.h" +#include "iremote_stub.h" +#include "drm_death_recipient.h" +#include "remote_request_code.h" + +namespace OHOS{ +namespace DrmStandard{ +class MediaKeySystemFactoryServiceStub : public IRemoteStub { +public: + MediaKeySystemFactoryServiceStub(); + ~MediaKeySystemFactoryServiceStub(); + virtual int OnRemoteRequest(uint32_t code, MessageParcel &data, + MessageParcel &reply, MessageOption &option) override; + +private: + std::map> deathRecipientMap_; +}; + +}//namespace DrmStandard +}//namespace OHOS +#endif // OHOS_DRM_MEDIA_KEY_SYSTEM_FACTORY_SERVICE_STUB_H \ No newline at end of file diff --git a/services/drm_service/server/src/drm_host_manager.cpp b/services/drm_service/server/src/drm_host_manager.cpp new file mode 100644 index 00000000..2571261b --- /dev/null +++ b/services/drm_service/server/src/drm_host_manager.cpp @@ -0,0 +1,74 @@ +#include "drm_host_manager.h" +#include "hdf_device_class.h" +#include "iremote_broker.h" +#include "iservmgr_hdi.h" +#include "drm_log.h" +#include "drm_error_code.h" +#include "drm_napi_utils.h" +#include "servmgr_hdi.h" + +namespace OHOS{ +namespace DrmStandard{ +using OHOS::HDI::ServiceManager::V1_0::IServiceManager; +void DrmHostManager::DrmHostDeathRecipient::OnRemoteDied(const wptr &remote) +{ + DRM_DEBUG_LOG("Remote died, do clean works."); +} + +DrmHostManager::DrmHostManager(StatusCallback* statusCallback) { +} + +DrmHostManager::~DrmHostManager() { +} + +int32_t DrmHostManager::Init(void) +{ + DRM_INFO_LOG("DrmHostManager::Init enter."); + DRM_INFO_LOG("DrmHostManager::Init exit."); + return DRM_OK; +} + +void DrmHostManager::DeInit(void) +{ + DRM_ERR_LOG("DrmHostManager::DeInit"); +} + +void DrmHostManager::OnReceive(const HDI::ServiceManager::V1_0::ServiceStatus& status) {} + +int32_t DrmHostManager::GetSevices(std::string &uuid, bool *isSurpported) +{ + DRM_INFO_LOG("DrmHostManager::GetSevices enter, uuid:%{public}s.", uuid.c_str()); + DRM_INFO_LOG("DrmHostManager::GetSevices exit, uuid:%{public}s.", uuid.c_str()); + return DRM_OK; +} + +int32_t DrmHostManager::IsMediaKeySystemSupported(std::string &uuid, bool *isSurpported) +{ + DRM_INFO_LOG("DrmHostManager::IsMediaKeySystemSupported one parameters enter, uuid:%{public}s.", uuid.c_str()); + DRM_INFO_LOG("DrmHostManager::IsMediaKeySystemSupported one parameters exit, isSurpported:%{public}d.", *isSurpported); + return DRM_OK; +} + +int32_t DrmHostManager::IsMediaKeySystemSupported(std::string &uuid, std::string &mimeType, bool *isSurpported) +{ + DRM_INFO_LOG("DrmHostManager::IsMediaKeySystemSupported two parameters enter, uuid:%{public}s, mimeType:%{public}s.", uuid.c_str(), mimeType.c_str()); + DRM_INFO_LOG("DrmHostManager::IsMediaKeySystemSupported two parameters exit, isSurpported:%{public}d.", *isSurpported); + return DRM_OK; +} + +int32_t DrmHostManager::IsMediaKeySystemSupported(std::string &uuid, std::string &mimeType, int32_t securityLevel, bool *isSurpported) +{ + DRM_INFO_LOG("DrmHostManager::IsMediaKeySystemSupported three parameters enter, uuid:%{public}s, mimeType:%{public}s, securityLevel:%{public}d.", uuid.c_str(), mimeType.c_str(), securityLevel); + DRM_INFO_LOG("DrmHostManager::IsMediaKeySystemSupported three parameters exit, isSurpported:%{public}d.", *isSurpported); + return DRM_OK; +} + +int32_t DrmHostManager::CreateMediaKeySystem(std::string &uuid) +{ + DRM_INFO_LOG("DrmHostManager::CreateMediaKeySystem enter."); + DRM_INFO_LOG("DrmHostManager::CreateMediaKeySystem exit."); + return DRM_OK; +} + +} // namespace DrmStandard +} // namespace OHOS \ No newline at end of file diff --git a/services/drm_service/server/src/key_session_service.cpp b/services/drm_service/server/src/key_session_service.cpp new file mode 100644 index 00000000..00c7998b --- /dev/null +++ b/services/drm_service/server/src/key_session_service.cpp @@ -0,0 +1,149 @@ +/* + * Copyright (c) 2023-2024 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "key_session_service.h" +#include "drm_log.h" +#include "ipc_skeleton.h" +namespace OHOS { +namespace DrmStandard { +static std::mutex sessionMutex_; +KeySessionService::KeySessionService() +{ + DRM_DEBUG_LOG("KeySessionService::KeySessionService."); +} + +KeySessionService::~KeySessionService() +{ + DRM_INFO_LOG("KeySessionService::~KeySessionService enter."); + DRM_INFO_LOG("KeySessionService::~KeySessionService exit."); +} + +int32_t KeySessionService::CloseKeySessionServiceByCallback() +{ + DRM_INFO_LOG("KeySessionService::CloseKeySessionServiceByCallback enter."); + DRM_INFO_LOG("KeySessionService::CloseKeySessionServiceByCallback exit."); + return DRM_OK; +} + +int32_t KeySessionService::Release() +{ + DRM_INFO_LOG("KeySessionService::Release enter."); + DRM_INFO_LOG("KeySessionService::Release exit."); + return DRM_OK; +} + +int32_t KeySessionService::SetKeySessionServiceOperatorsCallback(wptr callback) +{ + DRM_INFO_LOG("KeySessionService::SetKeySessionServiceOperatorsCallback enter."); + DRM_INFO_LOG("KeySessionService::SetKeySessionServiceOperatorsCallback exit."); + return DRM_OK; +} + +int32_t KeySessionService::GenerateLicenseRequest(IKeySessionService::DrmInfo &drmInfo, IKeySessionService::LicenseInfo &licenseInfo) +{ + DRM_INFO_LOG("KeySessionService::GenerateLicenseRequest enter."); + DRM_INFO_LOG("KeySessionService::GenerateLicenseRequest exit."); + return DRM_OK; +} + +int32_t KeySessionService::ProcessLicenseResponse(std::vector &keyId, std::vector &licenseResponse) +{ + DRM_INFO_LOG("KeySessionService::ProcessLicenseResponse enter."); + DRM_INFO_LOG("bKeySessionService::ProcessLicenseResponse exit."); + return DRM_OK; +} + +int32_t KeySessionService::GenerateOfflineReleaseRequest(std::vector &keyId, std::vector &releaseRequest) +{ + DRM_INFO_LOG("KeySessionService::GenerateOfflineReleaseRequest enter."); + DRM_INFO_LOG("KeySessionService::GenerateOfflineReleaseRequest exit."); + return DRM_OK; +} + +int32_t KeySessionService::ProcessOfflineReleaseResponse(std::vector &keyId, std::vector &releaseResponse) +{ + DRM_INFO_LOG("KeySessionService::ProcessOfflineReleaseResponse enter."); + DRM_INFO_LOG("KeySessionService::ProcessOfflineReleaseResponse exit."); + return DRM_OK; +} + +int32_t KeySessionService::CheckLicenseStatus(std::vector &infoMap) +{ + DRM_INFO_LOG("KeySessionService::CheckLicenseStatus enter."); + DRM_INFO_LOG("KeySessionService::CheckLicenseStatus exit."); + return DRM_OK; +} + +int32_t KeySessionService::RestoreOfflineKeys(std::vector &keyId) +{ + DRM_INFO_LOG("KeySessionService::RestoreOfflineKeys enter."); + DRM_INFO_LOG("KeySessionService::RestoreOfflineKeys exit."); + return DRM_OK; +} + +int32_t KeySessionService::RemoveOfflineKeys(std::vector &keyId) +{ + DRM_INFO_LOG("KeySessionService::RemoveOfflineKeys enter."); + DRM_INFO_LOG("KeySessionService::RemoveOfflineKeys exit."); + return DRM_OK; +} + +int32_t KeySessionService::GetOfflineKeyIds(std::vector> &keyIds) +{ + DRM_INFO_LOG("KeySessionService::GetOfflineKeyIds enter."); + DRM_INFO_LOG("KeySessionService::GetOfflineKeyIds exit."); + return DRM_OK; +} + +int32_t KeySessionService::RemoveLicenses() +{ + DRM_INFO_LOG("KeySessionService::RemoveLicenses enter."); + DRM_INFO_LOG("KeySessionService::RemoveLicenses exit."); + return DRM_OK; +} + +int32_t KeySessionService::CreateMediaDecryptModule(sptr &decryptModule) +{ + DRM_INFO_LOG("KeySessionService::CreateMediaDecryptModule enter."); + DRM_INFO_LOG("KeySessionService::CreateMediaDecryptModule exit."); + return DRM_OK; +} + +int32_t KeySessionService::GetOfflineKeyState(std::vector &keyId, IKeySessionService::OfflineKeyState &state) +{ + DRM_INFO_LOG("KeySessionService::GetOfflineKeyState enter."); + DRM_ERR_LOG("KeySessionService::GetOfflineKeyState exit."); + return DRM_OK; +} + +int32_t KeySessionService::SetKeySessionServiceCallback(sptr& callback) +{ + DRM_INFO_LOG("KeySessionService::SetKeySessionServiceCallback enter."); + DRM_INFO_LOG("KeySessionService::SetKeySessionServiceCallback exit."); + return DRM_OK; +} + +void KeySessionService::OnKeySessionKeyExpiredStatus(const KeyStatus status) +{ + DRM_INFO_LOG("KeySessionService::OnKeySessionKeyExpiredStatus status %{public}d.", status); +} + +void KeySessionService::OnKeySessionReclaimed(const SessionStatus status) +{ + DRM_INFO_LOG("KeySessionService::OnKeySessionReclaimed status %{public}d.", status); +} + +} // DrmStandard +} // OHOS \ No newline at end of file diff --git a/services/drm_service/server/src/key_session_service_callback_stub.cpp b/services/drm_service/server/src/key_session_service_callback_stub.cpp new file mode 100644 index 00000000..f6cd229e --- /dev/null +++ b/services/drm_service/server/src/key_session_service_callback_stub.cpp @@ -0,0 +1,79 @@ +/* + * Copyright (c) 2023-2024 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#include "key_session_service_callback_stub.h" +#include "remote_request_code.h" +#include "drm_log.h" +#include "drm_error_code.h" + +namespace OHOS { +namespace DrmStandard { +int32_t KeySessionServiceCallbackStub::OnRemoteRequest( + uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option) +{ + int32_t errCode = -1; + + if (data.ReadInterfaceToken() != GetDescriptor()) { + return errCode; + } + switch (code) { + case KEY_SESSION_SERVICE_CALLBACK_ON_KEY_EXPIRED: + errCode = KeySessionServiceCallbackStub::HandleOnKeySessionKeyExpired(data); + break; + case KEY_SESSION_SERVICE_CALLBACK_ON_KEY_SESSION_RECLAIMED: + errCode = KeySessionServiceCallbackStub::HandleOnKeySessionReclaimed(data); + break; + default: + DRM_ERR_LOG("KeySessionServiceCallbackStub request code %{public}u not handled", code); + errCode = IPCObjectStub::OnRemoteRequest(code, data, reply, option); + break; + } + + return errCode; +} + +int32_t KeySessionServiceCallbackStub::HandleOnKeySessionKeyExpired(MessageParcel& data) +{ + int32_t status = data.ReadInt32(); + DRM_INFO_LOG("KeySessionServiceCallbackStub::HandleOnKeySessionKeyExpired called status = %{public}d", status); + switch (status) + { + case KEY_STATUS_NORMAL: + return OnKeySessionKeyExpired(KEY_STATUS_NORMAL); + case KEY_STATUS_EXPIRED: + return OnKeySessionKeyExpired(KEY_STATUS_EXPIRED); + default: + DRM_ERR_LOG("HandleOnKeySessionKeyExpired can't cast status"); + return DRM_ERROR; + } +} + +int32_t KeySessionServiceCallbackStub::HandleOnKeySessionReclaimed(MessageParcel& data) +{ + int32_t status = data.ReadInt32(); + DRM_INFO_LOG("KeySessionServiceCallbackStub::HandleOnKeySessionReclaimed called status = %{public}d", status); + switch (status) + { + case SESSION_STATUS_NORMAL: + return OnKeySessionReclaimed(SESSION_STATUS_NORMAL); + case SESSION_STATUS_RECLAIMED: + return OnKeySessionReclaimed(SESSION_STATUS_RECLAIMED); + default: + DRM_ERR_LOG("HandleOnKeySessionReclaimed can't cast status"); + return DRM_ERROR; + } +} + +} // namespace DrmStandard +} // namespace OHOS \ No newline at end of file diff --git a/services/drm_service/server/src/key_session_service_stub.cpp b/services/drm_service/server/src/key_session_service_stub.cpp new file mode 100644 index 00000000..ec8aa2a8 --- /dev/null +++ b/services/drm_service/server/src/key_session_service_stub.cpp @@ -0,0 +1,268 @@ +/* + * Copyright (c) 2023-2024 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#include "key_session_service_stub.h" +#include "drm_error_code.h" +#include "drm_log.h" +#include "drm_napi_utils.h" +#include "ipc_skeleton.h" +#include "xcollie/xcollie.h" +#include "xcollie/xcollie_define.h" + +namespace OHOS { +namespace DrmStandard { + +int KeySessionServiceStub::OnRemoteRequest(uint32_t code, MessageParcel &data, + MessageParcel &reply, MessageOption &option) +{ + DRM_INFO_LOG("KeySessionServiceStub::OnRemoteRequest enter."); + int errCode = -1; + DRM_DEBUG_LOG("OnRemoteRequest, cmd = %{public}u", code); + DRM_DEBUG_LOG("0x%{public}06" PRIXPTR " is keySessionServiceStub", FAKE_POINTER(this)); + if (data.ReadInterfaceToken() != GetDescriptor()) { + DRM_ERR_LOG("KeySessionServiceStub: ReadInterfaceToken failed"); + return errCode; + } + + switch(code) { + case CREATE_MEDIA_DECRYPT_MODULE: { + DRM_INFO_LOG("KeySessionServiceStub GET_MEDIA_DECRYPT_MODULE enter."); + sptr decryptModuleServiceProxy = nullptr; + errCode = CreateMediaDecryptModule(decryptModuleServiceProxy); + if (errCode != ERR_NONE) { + DRM_ERR_LOG("KeySessionServiceStub CreateMediaDecryptModule failed : %{public}d", errCode); + return errCode; + } + if (!reply.WriteRemoteObject(decryptModuleServiceProxy->AsObject())) { + DRM_ERR_LOG("KeySessionServiceStub Write MediaDecryptModule obj failed"); + return IPC_STUB_WRITE_PARCEL_ERR; + } + DRM_INFO_LOG("KeySessionServiceStub GET_MEDIA_DECRYPT_MODULE exit."); + return DRM_OK; + } + case MEDIA_KEY_SESSION_GENERATE_LICENSE_REQUEST: { + DRM_INFO_LOG("KeySessionServiceStub MEDIA_KEY_SESSION_GENERATE_LICENSE_REQUEST enter."); + IKeySessionService::DrmInfo drmInfo; + IKeySessionService::LicenseInfo licenseInfo; + drmInfo.keyType = (OHOS::DrmStandard::IKeySessionService::KeyType)data.ReadInt32(); + drmInfo.mimeType = data.ReadString(); + uint32_t indexInfoSize = data.ReadInt32(); + const uint8_t* indexInfoBuf = static_cast(data.ReadBuffer(indexInfoSize)); + if (indexInfoBuf == nullptr) { + DRM_ERR_LOG("KeySessionServiceStub ReadBuffer failed"); + return IPC_STUB_WRITE_PARCEL_ERR; + } + drmInfo.indexInfo.assign(indexInfoBuf, indexInfoBuf + indexInfoSize); + int32_t ret = GenerateLicenseRequest(drmInfo, licenseInfo); + if (!reply.WriteInt32(licenseInfo.requestType)) { + DRM_ERR_LOG("KeySessionServiceStub Write GenerateLicenseRequest failed"); + return IPC_STUB_WRITE_PARCEL_ERR; + } + if (!reply.WriteInt32(licenseInfo.mData.size())) { + DRM_ERR_LOG("KeySessionServiceStub Write GenerateLicenseRequest failed"); + return IPC_STUB_WRITE_PARCEL_ERR; + } + + for (auto data : licenseInfo.mData) { + if (!reply.WriteUint8(data)) { + DRM_ERR_LOG("KeySessionServiceStub Write GenerateLicenseRequest failed"); + return IPC_STUB_WRITE_PARCEL_ERR; + } + } + if (!reply.WriteString(licenseInfo.mDefaultURL)) { + DRM_ERR_LOG("KeySessionServiceStub Write GenerateLicenseRequest failed"); + return IPC_STUB_WRITE_PARCEL_ERR; + } + DRM_INFO_LOG("KeySessionServiceStub MEDIA_KEY_SESSION_GENERATE_LICENSE_REQUEST exit."); + return ret; + } + case MEDIA_KEY_SESSION_PROCESS_LICENSE_RESPONSE: { + DRM_INFO_LOG("KeySessionServiceStub MEDIA_KEY_SYSTEM_PROCESS_LICENSE_RESPONSE enter."); + std::vector response; + std::vector keyId; + uint32_t responseSize = data.ReadInt32(); + for (int i = 0; i < responseSize; i++) { + response.push_back(data.ReadUint8()); + } + int32_t ret = ProcessLicenseResponse(keyId, response); + if (!reply.WriteInt32(keyId.size())) { + DRM_ERR_LOG("KeySessionServiceStub Write GenerateLicenseRequest failed"); + return IPC_STUB_WRITE_PARCEL_ERR; + } + for (auto id : keyId) { + if (!reply.WriteUint8(id)) { + DRM_ERR_LOG("KeySessionServiceStub Write GenerateLicenseRequest failed"); + return IPC_STUB_WRITE_PARCEL_ERR; + } + } + + DRM_INFO_LOG("KeySessionServiceStub MEDIA_KEY_SYSTEM_PROCESS_LICENSE_RESPONSE exit."); + return ret; + } + case MEDIA_KEY_SESSION_GENERATE_OFFLINE_RELEASE_REQUEST: { + DRM_INFO_LOG("KeySessionServiceStub MEDIA_KEY_SESSION_GENERATE_OFFLINE_RELEASE_REQUEST enter."); + std::vector keyId; + uint32_t keyIdSize = data.ReadInt32(); + for (int i = 0; i < keyIdSize; i++) { + keyId.push_back(data.ReadUint8()); + } + std::vector releaseRequest; + int32_t ret = GenerateOfflineReleaseRequest(keyId, releaseRequest); + if (!reply.WriteInt32(releaseRequest.size())) { + DRM_ERR_LOG("KeySessionServiceStub Write GenerateLicenseRequest failed"); + return IPC_STUB_WRITE_PARCEL_ERR; + } + + for (auto request : releaseRequest) { + if (!reply.WriteUint8(request)) { + DRM_ERR_LOG("KeySessionServiceStub Write GenerateLicenseRequest failed"); + return IPC_STUB_WRITE_PARCEL_ERR; + } + } + DRM_INFO_LOG("KeySessionServiceStub MEDIA_KEY_SESSION_GENERATE_OFFLINE_RELEASE_REQUEST exit."); + return ret; + } + case MEDIA_KEY_SESSION_PROCESS_OFFLINE_RELEASE_RESPONSE: { + DRM_INFO_LOG("KeySessionServiceStub MEDIA_KEY_SESSION_PROCESS_OFFLINE_RELEASE_RESPONSE enter."); + std::vector keyId; + uint32_t keyIdSize = data.ReadInt32(); + for (int i = 0; i < keyIdSize; i++) { + keyId.push_back(data.ReadUint8()); + } + std::vector response; + uint32_t responseSize = data.ReadInt32(); + for (int i = 0; i < responseSize; i++) { + response.push_back(data.ReadUint8()); + } + int32_t ret = ProcessOfflineReleaseResponse(keyId, response); + DRM_INFO_LOG("KeySessionServiceStub MEDIA_KEY_SESSION_PROCESS_OFFLINE_RELEASE_RESPONSE exit."); + return ret; + } + case MEDIA_KEY_SESSION_GENERATE_CHECK_LICENSE_STATUS: { + DRM_INFO_LOG("KeySessionServiceStub MEDIA_KEY_SESSION_GENERATE_CHECK_LICENSE_STATUS enter."); + std::vector infoMap; + int32_t ret = CheckLicenseStatus(infoMap); + if (ret != 0) { + DRM_ERR_LOG("CheckLicenseStatus faild."); + return ret; + } + reply.WriteInt32(infoMap.size()); + for (auto info : infoMap) { + reply.WriteString(info.name); + reply.WriteString(info.value); + } + DRM_INFO_LOG("KeySessionServiceStub MEDIA_KEY_SESSION_GENERATE_CHECK_LICENSE_STATUS exit."); + return ret; + } + case MEDIA_KEY_SESSION_RESTORE_OFFLINEKEYS: { + DRM_INFO_LOG("KeySessionServiceStub MEDIA_KEY_SESSION_RESTORE_OFFLINEKEYS enter."); + std::vector keyId; + uint32_t keyIdSize = data.ReadInt32(); + for (int i = 0; i < keyIdSize; i++) { + keyId.push_back(data.ReadUint8()); + } + int32_t ret = RestoreOfflineKeys(keyId); + DRM_INFO_LOG("KeySessionServiceStub MEDIA_KEY_SESSION_RESTORE_OFFLINEKEYS exit."); + return ret; + } + case MEDIA_KEY_SESSION_REMOVE_OFFLINEKEYS: { + DRM_INFO_LOG("KeySessionServiceStub MEDIA_KEY_SESSION_REMOVE_OFFLINEKEYS enter."); + std::vector keyId; + uint32_t keyIdSize = data.ReadInt32(); + for (int i = 0; i < keyIdSize; i++) { + keyId.push_back(data.ReadUint8()); + } + int32_t ret = RemoveOfflineKeys(keyId); + DRM_INFO_LOG("KeySessionServiceStub MEDIA_KEY_SESSION_REMOVE_OFFLINEKEYS exit."); + return ret; + } + case MEDIA_KEY_SESSION_GET_OFFLINEKEYIDS: { + DRM_INFO_LOG("KeySessionServiceStub MEDIA_KEY_SESSION_GET_OFFLINEKEYIDS enter."); + std::vector> keyIds; + int32_t ret = GetOfflineKeyIds(keyIds); + reply.WriteInt32(keyIds.size()); + for (auto keyId : keyIds) { + int32_t keyIdSize = keyId.size(); + reply.WriteInt32(keyIdSize); + if (!reply.WriteBuffer(keyId.data(), keyId.size())) { + DRM_ERR_LOG("KeySessionServiceProxy GenerateLicenseRequest Write keyIds failed"); + return IPC_STUB_WRITE_PARCEL_ERR; + } + } + + DRM_INFO_LOG("KeySessionServiceStub MEDIA_KEY_SESSION_GET_OFFLINEKEYIDS exit."); + return ret; + } + case MEDIA_KEY_SESSION_REMOVE_LICENSE: { + DRM_INFO_LOG("KeySessionServiceStub MEDIA_KEY_SESSION_REMOVE_LICENSE enter."); + int32_t ret = RemoveLicenses(); + DRM_INFO_LOG("KeySessionServiceStub MEDIA_KEY_SESSION_REMOVE_LICENSE exit."); + return ret; + } + case MEDIA_KEY_SESSION_GET_OFFLINEKEY_STATE: { + DRM_INFO_LOG("KeySessionServiceStub MEDIA_KEY_SESSION_GET_OFFLINEKEY_STATE enter."); + std::vector keyId; + uint32_t keyIdSize = data.ReadInt32(); + for (int i = 0; i < keyIdSize; i++) { + keyId.push_back(data.ReadUint8()); + } + IKeySessionService::OfflineKeyState state; + int32_t ret = GetOfflineKeyState(keyId, state); + if (ret != DRM_OK) { + DRM_ERR_LOG("MEDIA_KEY_SESSION_GET_OFFLINEKEY_STATE failed"); + return ret; + } + if (!reply.WriteInt32((int32_t)state)) { + DRM_ERR_LOG("KeySessionServiceStub Write state failed"); + return IPC_STUB_WRITE_PARCEL_ERR; + } + DRM_INFO_LOG("KeySessionServiceStub MEDIA_KEY_SESSION_GET_OFFLINEKEY_STATE exit."); + return ret; + } + case RELEASE_MEDIA_DECRYPT_MODULE: { + return errCode; + } + case KEY_SESSION_RELEASE: { + DRM_INFO_LOG("KeySessionServiceStub KEY_SESSION_RELEASE enter."); + errCode = Release(); + DRM_INFO_LOG("KeySessionServiceStub KEY_SESSION_RELEASE exit."); + return errCode; + } + case MEDIA_KEY_SESSION_SET_CALLBACK: { + DRM_INFO_LOG("KeySessionServiceStub MEDIA_KEY_SESSION_SET_CALLBACK enter."); + auto remoteObject = data.ReadRemoteObject(); + if (remoteObject == nullptr) { + DRM_ERR_LOG("KeySessionServiceStub SetCallback CameraServiceCallback is null"); + return IPC_STUB_INVALID_DATA_ERR; + } + auto callback = iface_cast(remoteObject); + if (callback == nullptr) { + DRM_ERR_LOG("KeySessionServiceStub SetCallback cast nullptr"); + return IPC_STUB_INVALID_DATA_ERR; + } + errCode = SetKeySessionServiceCallback(callback); + DRM_INFO_LOG("KeySessionServiceStub MEDIA_KEY_SESSION_SET_CALLBACK exit."); + return errCode; + } + default: { + DRM_INFO_LOG("KeySessionServiceStub request code %{public}u not handled", code); + return IPCObjectStub::OnRemoteRequest(code, data, reply, option); + } + } + + return errCode; +} + +} // DrmStandard +} // OHOS \ No newline at end of file diff --git a/services/drm_service/server/src/media_decrypt_module_service.cpp b/services/drm_service/server/src/media_decrypt_module_service.cpp new file mode 100644 index 00000000..173c9385 --- /dev/null +++ b/services/drm_service/server/src/media_decrypt_module_service.cpp @@ -0,0 +1,61 @@ +/* + * Copyright (c) 2023-2024 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "media_decrypt_module_service.h" +#include "ipc_skeleton.h" +#include "drm_napi_utils.h" +#include +#include +#include +#include "ashmem.h" + +namespace OHOS { +namespace DrmStandard { + +MediaDecryptModuleService::MediaDecryptModuleService() +{ + DRM_DEBUG_LOG("MediaDecryptModuleService::MediaDecryptModuleService"); +} + +MediaDecryptModuleService::~MediaDecryptModuleService() +{ + DRM_INFO_LOG("MediaDecryptModuleService::~MediaDecryptModuleService enter."); + DRM_INFO_LOG("MediaDecryptModuleService::~MediaDecryptModuleService exit."); +} + +int32_t MediaDecryptModuleService::Release() +{ + DRM_INFO_LOG("MediaDecryptModuleService::Release enter."); + DRM_INFO_LOG("MediaDecryptModuleService::Release exit."); + return DRM_OK; +} + +int32_t MediaDecryptModuleService::DecryptData(bool secureDecodrtState, IMediaDecryptModuleService::CryptInfo &cryptInfo, uint64_t srcBuffer, + uint64_t dstBuffer) +{ + DRM_INFO_LOG("MediaDecryptModuleService::DecryptData enter."); + DRM_INFO_LOG("MediaDecryptModuleService::DecryptData exit."); + return DRM_OK; +} + +int32_t MediaDecryptModuleService::RequireSecureDecoderModule(std::string &mimeType, bool *status) +{ + DRM_INFO_LOG("MediaDecryptModuleService::RequireSecureDecoderModule enter."); + DRM_INFO_LOG("MediaDecryptModuleService::RequireSecureDecoderModule exit."); + return DRM_OK; +} + +} // DrmStandard +} // OHOS \ No newline at end of file diff --git a/services/drm_service/server/src/media_decrypt_module_service_stub.cpp b/services/drm_service/server/src/media_decrypt_module_service_stub.cpp new file mode 100644 index 00000000..8d641f5d --- /dev/null +++ b/services/drm_service/server/src/media_decrypt_module_service_stub.cpp @@ -0,0 +1,105 @@ +/* + * Copyright (c) 2023-2024 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "media_decrypt_module_service_stub.h" +#include "drm_error_code.h" +#include "drm_log.h" +#include "drm_napi_utils.h" +#include "ipc_skeleton.h" +#include "xcollie/xcollie.h" +#include "xcollie/xcollie_define.h" +#include "remote_request_code.h" + +namespace OHOS { +namespace DrmStandard { + +MediaDecryptModuleServiceStub::MediaDecryptModuleServiceStub() +{ + DRM_DEBUG_LOG("0x%{public}06" PRIXPTR " Instances create", (POINTER_MASK & reinterpret_cast(this))); +} + +MediaDecryptModuleServiceStub::~MediaDecryptModuleServiceStub() +{ + DRM_DEBUG_LOG("0x%{public}06" PRIXPTR " Instances destroy", (POINTER_MASK & reinterpret_cast(this))); +} + +int32_t MediaDecryptModuleServiceStub::OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option) +{ + DRM_INFO_LOG("OnRemoteRequest, cmd = %{public}u", code); + if (data.ReadInterfaceToken() != GetDescriptor()) { + DRM_ERR_LOG("MediaDecryptModuleServiceStub: ReadInterfaceToken failed"); + return -1; + } + switch(code) { + case DECRYPT_MODULE_DECRYPT_DATA: { + DRM_INFO_LOG("MediaDecryptModuleServiceStub DECRYPT_MODULE_DECRYPT_DATA enter."); + IMediaDecryptModuleService::CryptInfo cryptInfo; + bool secureDecodrtState = data.ReadBool(); + cryptInfo.type = (OHOS::DrmStandard::IMediaDecryptModuleService::CryptAlgorithmType)data.ReadInt32(); + uint32_t keyIdSize = data.ReadInt32(); + for (uint32_t i = 0; i < keyIdSize; i++) { + cryptInfo.keyId.push_back(data.ReadUint8()); + } + uint32_t ivSize = data.ReadInt32(); + for (uint32_t i = 0; i < ivSize; i++) { + cryptInfo.iv.push_back(data.ReadUint8()); + } + cryptInfo.pattern.encryptBlocks = data.ReadInt32(); + cryptInfo.pattern.skipBlocks = data.ReadInt32(); + uint32_t subSampleNumber = data.ReadInt32(); + cryptInfo.subSample.resize(subSampleNumber); + for (int32_t i = 0; i < subSampleNumber; i++) { + cryptInfo.subSample[i].clearHeaderLen = data.ReadInt32(); + cryptInfo.subSample[i].payLoadLen = data.ReadInt32(); + } + uint64_t srcBuffer = data.ReadFileDescriptor(); + uint64_t dstBuffer = data.ReadFileDescriptor(); + int32_t ret = DecryptData(secureDecodrtState, cryptInfo, srcBuffer, dstBuffer); + if (ret != 0) { + DRM_ERR_LOG("DecryptData faild."); + return ret; + } + DRM_INFO_LOG("MediaDecryptModuleServiceStub DECRYPT_MODULE_DECRYPT_DATA exit."); + return ret; + } + case DECRYPT_MODULE_REQUIRE_SECURE_DECODER: { + DRM_INFO_LOG("MediaDecryptModuleServiceStub DECRYPT_MODULE_REQUIRE_SECURE_DECODER enter."); + std::string mimeType = data.ReadString(); + bool status; + int32_t ret = RequireSecureDecoderModule(mimeType, &status); + if (ret != 0) { + DRM_ERR_LOG("RequireSecureDecoderModule faild."); + return ret; + } + reply.WriteBool(status); + DRM_INFO_LOG("MediaDecryptModuleServiceStub DECRYPT_MODULE_REQUIRE_SECURE_DECODER exit."); + return ret; + } + case DECRYPT_MODULE_RELEASE: { + DRM_INFO_LOG("MediaDecryptModuleServiceStub DECRYPT_MODULE_RELEASE enter."); + int32_t ret = Release(); + DRM_INFO_LOG("MediaDecryptModuleServiceStub DECRYPT_MODULE_RELEASE exit."); + return ret; + } + default: { + DRM_ERR_LOG("default case, need check Decrypt Module"); + return IPCObjectStub::OnRemoteRequest(code, data, reply, option); + } + } + return -1; +} + +} // DrmStandard +} // OHOS \ No newline at end of file diff --git a/services/drm_service/server/src/mediakeysystem_service.cpp b/services/drm_service/server/src/mediakeysystem_service.cpp new file mode 100644 index 00000000..9e0252bc --- /dev/null +++ b/services/drm_service/server/src/mediakeysystem_service.cpp @@ -0,0 +1,111 @@ +/* + * Copyright (c) 2023-2024 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "mediakeysystem_service.h" +#include "key_session_service.h" +#include +#include + +#include "access_token.h" +#include "drm_napi_utils.h" +#include "iservice_registry.h" +#include "drm_log.h" +#include "system_ability_definition.h" +#include "ipc_skeleton.h" + +namespace OHOS{ +namespace DrmStandard{ + +MediaKeySystemService::MediaKeySystemService() +{ + DRM_DEBUG_LOG("~MediaKeySystemService"); +} + +MediaKeySystemService::~MediaKeySystemService() { + DRM_DEBUG_LOG("~MediaKeySystemService"); +} + +int32_t MediaKeySystemService::CloseMediaKeySystemServiceByCallback() +{ + DRM_INFO_LOG("MediaKeySystemService::CloseMediaKeySystemServiceByCallback enter."); + DRM_INFO_LOG("MediaKeySystemService::CloseMediaKeySystemServiceByCallback exit."); + return DRM_OK; +} + +int32_t MediaKeySystemService::Release() +{ + DRM_INFO_LOG("MediaKeySystemService::Release enter."); + DRM_INFO_LOG("MediaKeySystemService::Release exit."); + return DRM_OK; +} + +int32_t MediaKeySystemService::SetMediaKeySystemServiceOperatorsCallback(wptr callback) +{ + return DRM_OK; +} + +int32_t MediaKeySystemService::GenerateKeySystemRequest(IMediaKeySystemService::RequestType type, std::vector &request, std::string &defaultUrl) { + DRM_INFO_LOG("MediaKeySystemService::GenerateKeySystemRequest enter, type:%{public}d.", type); + DRM_INFO_LOG("MediaKeySystemService::GenerateKeySystemRequest exit."); + return DRM_OK; +} + +int32_t MediaKeySystemService::ProcessKeySystemResponse(IMediaKeySystemService::RequestType type, const std::vector &response) { + DRM_INFO_LOG("MediaKeySystemService::ProcessKeySystemResponse enter, type:%{public}d.", type); + DRM_INFO_LOG("MediaKeySystemService::ProcessKeySystemResponse exit."); + return DRM_OK; +} + +int32_t MediaKeySystemService::SetConfiguration(IMediaKeySystemService::ConfigType type, std::string &propertyName, std::string &value) { + DRM_INFO_LOG("MediaKeySystemService::SetConfiguration enter, configType:%{public}d, propertyName:%{public}s, value:%{public}s.", (int)type, propertyName.c_str(), value.c_str()); + DRM_INFO_LOG("MediaKeySystemService::SetConfiguration exit."); + return DRM_OK; +} + +int32_t MediaKeySystemService::GetConfiguration(IMediaKeySystemService::ConfigType type, std::string &propertyName, std::string &value) { + DRM_INFO_LOG("MediaKeySystemService::GetConfiguration enter, configType:%{public}d, propertyName:%{public}s.", (int)type, propertyName.c_str()); + DRM_INFO_LOG("MediaKeySystemService::GetConfiguration exit, value:%{public}s.", value.c_str()); + return DRM_OK; +} + +int32_t MediaKeySystemService::CreateKeySession(IKeySessionService::SecurityLevel securityLevel, sptr &keySessionProxy) +{ + DRM_INFO_LOG("MediaKeySystemService::CreateKeySession enter, securityLevel:%{public}d.", securityLevel); + DRM_INFO_LOG("MediaKeySystemService::CreateKeySession exit."); + return DRM_OK; +} + +int32_t MediaKeySystemService::CloseKeySessionService(sptr sessionService) +{ + DRM_INFO_LOG("MediaKeySystemService::CloseKeySessionService enter."); + DRM_INFO_LOG("MediaKeySystemService::CloseKeySessionService exit."); + return DRM_OK; +} + +int32_t MediaKeySystemService::GetMetric(std::vector &infoMap) +{ + DRM_INFO_LOG("MediaKeySystemService::GetMetric enter"); + DRM_INFO_LOG("MediaKeySystemService::GetMetric exit"); + return DRM_OK; +} + +int32_t MediaKeySystemService::GetSecurityLevel(IKeySessionService::SecurityLevel *securityLevel) { + DRM_INFO_LOG("MediaKeySystemService::GetSecurityLevel enter."); + DRM_INFO_LOG("MediaKeySystemService::GetSecurityLevel exit."); + return DRM_OK; +} + +} // DrmStandard +} // OHOS \ No newline at end of file diff --git a/services/drm_service/server/src/mediakeysystem_service_stub.cpp b/services/drm_service/server/src/mediakeysystem_service_stub.cpp new file mode 100644 index 00000000..f9938282 --- /dev/null +++ b/services/drm_service/server/src/mediakeysystem_service_stub.cpp @@ -0,0 +1,157 @@ +/* + * Copyright (c) 2023-2024 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "mediakeysystem_service_stub.h" +#include "drm_error_code.h" +#include "drm_log.h" +#include "drm_napi_utils.h" +#include "ipc_skeleton.h" +#include "xcollie/xcollie.h" +#include "xcollie/xcollie_define.h" + +namespace OHOS{ +namespace DrmStandard +{ +MediaKeySystemServiceStub::MediaKeySystemServiceStub() +{ + deathRecipientMap_.clear(); + DRM_DEBUG_LOG("0x%{public}06" PRIXPTR " Instances create", (POINTER_MASK & reinterpret_cast(this))); +} + +MediaKeySystemServiceStub::~MediaKeySystemServiceStub() +{ + DRM_DEBUG_LOG("0x%{public}06" PRIXPTR " Instances destroy", (POINTER_MASK & reinterpret_cast(this))); +} + +int MediaKeySystemServiceStub::OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option) +{ + DRM_INFO_LOG("OnRemoteRequest, cmd = %{public}u", code); + if (data.ReadInterfaceToken() != GetDescriptor()) { + DRM_ERR_LOG("MediaKeySystemServiceStub: ReadInterfaceToken failed"); + return -1; + } + + switch(code) { + case MEDIA_KEY_SYSTEM_CREATE_KEY_SESSION: { + DRM_INFO_LOG("MediaKeySystemServiceStub MEDIA_KEY_SYSTEM_CREATE_KEY_SESSION enter."); + sptr keySessionServiceProxy = nullptr; + const int32_t securityLevel = data.ReadInt32(); + int errCode = CreateKeySession((IKeySessionService::SecurityLevel)securityLevel, keySessionServiceProxy); + if (errCode != ERR_NONE) { + DRM_ERR_LOG("MediaKeySystemServiceStub CreateKeySession failed : %{public}d", errCode); + return errCode; + } + if (!reply.WriteRemoteObject(keySessionServiceProxy->AsObject())) { + DRM_ERR_LOG("MediaKeySystemServiceStub CreateKeySession Write KeySession obj failed"); + return IPC_STUB_WRITE_PARCEL_ERR; + } + DRM_INFO_LOG("MediaKeySystemServiceStub MEDIA_KEY_SYSTEM_CREATE_KEY_SESSION exit."); + return errCode; + } + case MEDIA_KEY_SYSTEM_GENERATE_KEYSYSTEM_REQUEST: { + DRM_INFO_LOG("MediaKeySystemServiceStub MEDIA_KEY_SYSTEM_GENERATE_KEYSYSTEM_REQUEST enter."); + std::vector request; + std::string defaultUrl; + int type = data.ReadInt32(); + int32_t ret = GenerateKeySystemRequest(IMediaKeySystemService::RequestType(type), request, defaultUrl); + if (!reply.WriteInt32(request.size())) { + DRM_ERR_LOG("KeySessionServiceStub Write request size failed"); + return IPC_STUB_WRITE_PARCEL_ERR; + } + for (auto req : request) { + if (!reply.WriteUint8(req)) { + DRM_ERR_LOG("MediaKeySystemServiceStub Write request failed"); + return IPC_STUB_WRITE_PARCEL_ERR; + } + } + if (!reply.WriteString(defaultUrl)) { + DRM_ERR_LOG("MediaKeySystemServiceStub Write GenerateKeySystemRequest failed"); + return IPC_STUB_WRITE_PARCEL_ERR; + } + DRM_INFO_LOG("MediaKeySystemServiceStub MEDIA_KEY_SYSTEM_GENERATE_KEYSYSTEM_REQUEST exit."); + return ret; + } + case MEDIA_KEY_SYSTEM_GENERATE_KEYSYSTEM_RESPONSE: { + DRM_INFO_LOG("MediaKeySystemServiceStub MEDIA_KEY_SYSTEM_GENERATE_KEYSYSTEM_RESPONSE enter."); + int type = data.ReadInt32(); + std::vector response; + uint32_t responseSize = data.ReadInt32(); + for (int i = 0; i < responseSize; i++) { + response.push_back(data.ReadUint8()); + } + int32_t ret = ProcessKeySystemResponse(IMediaKeySystemService::RequestType(type), response); + DRM_INFO_LOG("MediaKeySystemServiceStub MEDIA_KEY_SYSTEM_GENERATE_KEYSYSTEM_RESPONSE exit."); + return ret; + } + case MEDIA_KEY_SYSTEM_GETSECURITYLEVEL: { + DRM_INFO_LOG("KeySessionServiceStub MEDIA_KEY_SYSTEM_GETSECURITYLEVEL enter."); + IKeySessionService::SecurityLevel securityLevel = IKeySessionService::SECURITY_LEVEL_UNKNOWN; + int32_t ret = GetSecurityLevel(&securityLevel); + if (!reply.WriteInt32(securityLevel)) { + DRM_ERR_LOG("KeySessionServiceStub Write GetSecurityLevel failed"); + return IPC_STUB_WRITE_PARCEL_ERR; + } + DRM_INFO_LOG("KeySessionServiceStub MEDIA_KEY_SYSTEM_GETSECURITYLEVEL exit."); + return ret; + } + case MEDIA_KEY_SYSTEM_SETCONFIGURATION: { + DRM_INFO_LOG("bMediaKeySystemServiceStub MEDIA_KEY_SYSTEM_SETCONFIGURATION enter."); + int type = data.ReadInt32(); + std::string propertyName = data.ReadString(); + std::string value = data.ReadString(); + int32_t ret = SetConfiguration(IMediaKeySystemService::ConfigType(type), propertyName, value); + DRM_INFO_LOG("bMediaKeySystemServiceStub MEDIA_KEY_SYSTEM_SETCONFIGURATION exit."); + return ret; + } + case MEDIA_KEY_SYSTEM_GETCONFIGURATION: { + DRM_INFO_LOG("bMediaKeySystemServiceStub MEDIA_KEY_SYSTEM_GETCONFIGURATION enter."); + int type = data.ReadInt32(); + std::string propertyName = data.ReadString(); + std::string value; + int32_t ret = GetConfiguration(IMediaKeySystemService::ConfigType(type), propertyName, value); + if (!reply.WriteString(value)) { + DRM_ERR_LOG("bMediaKeySystemServiceStub Write GenerateKeySystemRequest failed"); + return IPC_STUB_WRITE_PARCEL_ERR; + } + DRM_INFO_LOG("bMediaKeySystemServiceStub MEDIA_KEY_SYSTEM_GETCONFIGURATION exit."); + return ret; + } + case MEDIA_KEY_SYSTEM_GETMETRIC: { + DRM_INFO_LOG("MediaKeySystemServiceStub MEDIA_KEY_SYSTEM_GETMETRIC enter."); + std::vector infoMap; + int32_t ret = GetMetric(infoMap); + reply.WriteInt32(infoMap.size()); + for (auto info : infoMap) { + reply.WriteString(info.name); + reply.WriteString(info.value); + } + DRM_INFO_LOG("MediaKeySystemServiceStub MEDIA_KEY_SYSTEM_GETMETRIC exit."); + return ret; + } + case MEDIA_KEY_SYSTEM_RELEASE: { + DRM_INFO_LOG("MediaKeySystemServiceStub KEY_SESSION_RELEASE enter."); + int32_t ret = Release(); + DRM_INFO_LOG("MediaKeySystemServiceStub KEY_SESSION_RELEASE exit."); + return ret; + } + default: { + DRM_ERR_LOG("default case, need check MediaKeySystemServiceStub"); + return IPCObjectStub::OnRemoteRequest(code, data, reply, option); + } + } +} + +}//namespace DrmStandard +}//namespace OHOS \ No newline at end of file diff --git a/services/drm_service/server/src/mediakeysystemfactory_service.cpp b/services/drm_service/server/src/mediakeysystemfactory_service.cpp new file mode 100644 index 00000000..efb8e667 --- /dev/null +++ b/services/drm_service/server/src/mediakeysystemfactory_service.cpp @@ -0,0 +1,89 @@ +/* + * Copyright (c) 2023-2024 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "mediakeysystemfactory_service.h" +#include "mediakeysystem_service.h" +#include +#include + +#include "access_token.h" +#include "drm_napi_utils.h" +#include "iservice_registry.h" +#include "drm_log.h" +#include "system_ability_definition.h" +#include "ipc_skeleton.h" + +namespace OHOS{ +namespace DrmStandard{ +REGISTER_SYSTEM_ABILITY_BY_ID(MediaKeySystemFactoryService, MEDIA_KEY_SYSTEM_SERVICE_ID, true) + +MediaKeySystemFactoryService::MediaKeySystemFactoryService(int32_t systemAbilityId, bool runOnCreate) + : SystemAbility(systemAbilityId, runOnCreate) {} + +MediaKeySystemFactoryService::~MediaKeySystemFactoryService() { + DRM_INFO_LOG("~MediaKeySystemFactoryService"); +} + +void MediaKeySystemFactoryService::OnStart() +{ + DRM_ERR_LOG("MediaKeySystemFactoryService"); +} + +void MediaKeySystemFactoryService::OnDump() +{ + DRM_INFO_LOG("MediaKeySystemFactoryService::OnDump called"); +} + +void MediaKeySystemFactoryService::OnStop() +{ + DRM_INFO_LOG("MediaKeySystemFactoryService::OnStop called"); +} + +int32_t MediaKeySystemFactoryService::CreateMediaKeySystem(std::string &uuid) +{ + DRM_INFO_LOG("MediaKeySystemFactoryService CreateMediaKeySystem enter."); + DRM_INFO_LOG("MediaKeySystemFactoryService CreateMediaKeySystem exit."); + return DRM_OK; +} + +int32_t MediaKeySystemFactoryService::CloseMediaKeySystemService(sptr mediaKeySystemService) +{ + DRM_INFO_LOG("MediaKeySystemFactoryService CloseMediaKeySystemService enter."); + DRM_INFO_LOG("MediaKeySystemFactoryService CloseMediaKeySystemService exit."); + return DRM_OK; +} + +int32_t MediaKeySystemFactoryService::IsMediaKeySystemSupported(std::string &uuid, bool *isSurpported) { + DRM_INFO_LOG("MediaKeySystemFactoryService::IsMediaKeySystemSupported one parameters enter"); + DRM_INFO_LOG("MediaKeySystemFactoryService::IsMediaKeySystemSupported one parameters exit"); + return DRM_OK; +} + +int32_t MediaKeySystemFactoryService::IsMediaKeySystemSupported(std::string &uuid, std::string &mimeType, bool *isSurpported) +{ + DRM_INFO_LOG("MediaKeySystemFactoryService::IsMediaKeySystemSupported two parameters enter"); + DRM_INFO_LOG("MediaKeySystemFactoryService::IsMediaKeySystemSupported two parameters exit"); + return DRM_OK; +} + +int32_t MediaKeySystemFactoryService::IsMediaKeySystemSupported(std::string &uuid, std::string &mimeType, int32_t securityLevel, bool *isSurpported) +{ + DRM_INFO_LOG("MediaKeySystemFactoryService::IsMediaKeySystemSupported three parameters enter"); + DRM_INFO_LOG("MediaKeySystemFactoryService enter IsMediaKeySystemSupported three parameters exit"); + return DRM_OK; +} + +} // DrmStandard +} // OHOS \ No newline at end of file diff --git a/services/drm_service/server/src/mediakeysystemfactory_service_stub.cpp b/services/drm_service/server/src/mediakeysystemfactory_service_stub.cpp new file mode 100644 index 00000000..38b8c352 --- /dev/null +++ b/services/drm_service/server/src/mediakeysystemfactory_service_stub.cpp @@ -0,0 +1,113 @@ +/* + * Copyright (c) 2023-2024 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "mediakeysystemfactory_service_stub.h" +#include "drm_error_code.h" +#include "drm_log.h" +#include "drm_napi_utils.h" +#include "ipc_skeleton.h" +#include "xcollie/xcollie.h" +#include "xcollie/xcollie_define.h" +#include +namespace OHOS{ +namespace DrmStandard +{ +MediaKeySystemFactoryServiceStub::MediaKeySystemFactoryServiceStub() +{ + deathRecipientMap_.clear(); + DRM_DEBUG_LOG("0x%{public}06" PRIXPTR " Instances create", (POINTER_MASK & reinterpret_cast(this))); +} + +MediaKeySystemFactoryServiceStub::~MediaKeySystemFactoryServiceStub() +{ + DRM_DEBUG_LOG("0x%{public}06" PRIXPTR " Instances destroy", (POINTER_MASK & reinterpret_cast(this))); +} + +int MediaKeySystemFactoryServiceStub::OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option) +{ + std::wstring_convert, char16_t> converter; + if (data.ReadInterfaceToken() != GetDescriptor()) { + DRM_DEBUG_LOG("MediaKeySystemFactoryServiceStub: ReadInterfaceToken failed"); + return -1; + } + + switch(code) { + case MEDIA_KEY_SYSTEM_FACTORY_IS_MEDIA_KEY_SYSTEM_SURPPORTED: { + DRM_INFO_LOG("MediaKeySystemFactoryServiceStub MEDIA_KEY_SYSTEM_FACTORY_IS_MEDIA_KEY_SYSTEM_SURPPORTED enter."); + int paramNum = data.ReadInt32(); + bool isSurpported = false; + if (paramNum > 3 || paramNum < 0) { + DRM_ERR_LOG("MediaKeySystemFactoryServiceStub paramNum is invalid"); + return IPC_STUB_WRITE_PARCEL_ERR; + } + DRM_ERR_LOG("MediaKeySystemFactoryServiceStub "); + + // get uuid + std::string uuid = data.ReadString(); + // search by uuid + if (paramNum == 1) { + DRM_ERR_LOG("MediaKeySystemFactoryServiceStub "); + int32_t ret = IsMediaKeySystemSupported(uuid, &isSurpported); + DRM_ERR_LOG("MediaKeySystemFactoryServiceStub "); + if (!reply.WriteBool(isSurpported)) { + DRM_ERR_LOG("MediaKeySystemFactoryServiceStub Write isSurpported failed"); + return IPC_STUB_WRITE_PARCEL_ERR; + } + DRM_ERR_LOG("MediaKeySystemFactoryServiceStub "); + return ret; + } + + // get mimeType + std::string mimeType = data.ReadString(); + // search by uuid and mimeType + if (paramNum == 2) { + int32_t ret = IsMediaKeySystemSupported(uuid, mimeType, &isSurpported); + if (!reply.WriteBool(isSurpported)) { + DRM_ERR_LOG("MediaKeySystemFactoryServiceStub Write isSurpported failed"); + return IPC_STUB_WRITE_PARCEL_ERR; + } + return ret; + } + + // get securityLevel + int32_t securityLevel = data.ReadInt32(); + // search by uuid, mineType and securityLevel + if (paramNum == 3) { + int32_t ret = IsMediaKeySystemSupported(uuid, mimeType, securityLevel, &isSurpported); + if (!reply.WriteBool(isSurpported)) { + DRM_ERR_LOG("MediaKeySystemFactoryServiceStub Write isSurpported failed"); + return IPC_STUB_WRITE_PARCEL_ERR; + } + return ret; + } + DRM_INFO_LOG("MediaKeySystemFactoryServiceStub MEDIA_KEY_SYSTEM_FACTORY_IS_MEDIA_KEY_SYSTEM_SURPPORTED exit."); + return -1; + } + case MEDIA_KEY_SYSTEM_FACTORY_CREATE_MEDIA_KEYSYSTEM: { + DRM_INFO_LOG("MediaKeySystemFactoryServiceStub MEDIA_KEY_SYSTEM_FACTORY_CREATE_MEDIA_KEYSYSTEM enter."); + std::string uuid = data.ReadString(); + int errCode = CreateMediaKeySystem(uuid); + if (errCode != ERR_NONE) { + DRM_ERR_LOG("MediaKeySystemFactoryServiceStub CreateMediaKeySystem failed : %{public}d", errCode); + return errCode; + } + DRM_INFO_LOG("MediaKeySystemFactoryServiceStub MEDIA_KEY_SYSTEM_FACTORY_CREATE_MEDIA_KEYSYSTEM exit."); + return errCode; + } + } + return DRM_OK; +} +}//namespace DrmStandard +}//namespace OHOS \ No newline at end of file diff --git a/services/etc/BUILD.gn b/services/etc/BUILD.gn new file mode 100644 index 00000000..cabc7e4a --- /dev/null +++ b/services/etc/BUILD.gn @@ -0,0 +1,8 @@ +import("//build/ohos.gni") + +ohos_prebuilt_etc("drm_service.rc") { + source = "drm_service.cfg" + relative_install_dir = "init" + part_name = "multimedia_drm_framework" + subsystem_name = "multimedia" +} diff --git a/services/etc/drm_service.cfg b/services/etc/drm_service.cfg new file mode 100644 index 00000000..31451aa1 --- /dev/null +++ b/services/etc/drm_service.cfg @@ -0,0 +1,13 @@ +{ + "services" : [{ + "name" : "drm_service", + "path" : ["/system/bin/sa_main", "/system/profile/drm_service.json"], + "uid" : "drmserver", + "gid" : ["system", "shell"], + "secon" : "u:r:drm_service:s0", + "permission" : ["ohos.permission.GET_SENSITIVE_PERMISSIONS", "ohos.permission.PERMISSION_USED_STATS"], + "permission_acls" : ["ohos.permission.GET_SENSITIVE_PERMISSIONS"] + } + ] +} + diff --git a/services/etc/drm_service.rc b/services/etc/drm_service.rc new file mode 100644 index 00000000..c583c9ee --- /dev/null +++ b/services/etc/drm_service.rc @@ -0,0 +1,22 @@ +# Copyright (c) 2023-2024 Huawei Device Co., Ltd. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +service drm_service /system/bin/sa_main /system/profile/drm_service.json + class z_core + user drmserver + group system shell + seclabel u:r:audiodistributedservice:s0 + +on boot + start drm_service + diff --git a/services/utils/include/drm_error_code.h b/services/utils/include/drm_error_code.h new file mode 100644 index 00000000..ef0fc344 --- /dev/null +++ b/services/utils/include/drm_error_code.h @@ -0,0 +1,42 @@ +/* + * Copyright (c) 2023-2024 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef DRM_ERROR_CODE_H +#define DRM_ERROR_CODE_H + +namespace OHOS{ +namespace DrmStandard{ +/** + * @brief drm remote request code for IPC. + * + * @since 1.0 + * @version 1.0 +*/ +enum DrmErrorCode{ + DRM_ERROR = -1, + DRM_SUCCESS = 0, + NO_SYSTEMP_APP_PERMISSION = 202, + INVALID_ARGUMENT = 7400101, + OPERATION_NOT_ALLOWED = 7400102, + SESSION_NOT_CONFIG = 7400103, + SESSION_NOT_RUNNING = 7400104, + SESSION_CONFIG_LOCKED = 7400105, + DEVICE_SETTING_LOCKED = 7400106, + DEVICE_DISABLED = 7400108, + SERVICE_FATAL_ERROR = 7400201 +}; +} // namespace DrmStandard +} // namespace OHOS +#endif // DRM_ERROR_CODE_H \ No newline at end of file diff --git a/services/utils/include/drm_log.h b/services/utils/include/drm_log.h new file mode 100644 index 00000000..d8418bf9 --- /dev/null +++ b/services/utils/include/drm_log.h @@ -0,0 +1,55 @@ +#ifndef OHOS_DRM_LOG_H +#define OHOS_DRM_LOG_H + +#include + +#include "hilog/log.h" +#include "hisysevent.h" +#include "hitrace_meter.h" +#include + +#undef LOG_DOMAIN +#undef LOG_TAG +#define LOG_DOMAIN 0xD002B00 +#define LOG_TAG "DRM" +#define MAX_STRING_SIZE 256 + +#define __FILENAME__ (__builtin_strrchr(__FILE__, '/') ? __builtin_strrchr(__FILE__, '/') + 1 : __FILE__) + +#define POINTER_MASK 0x00FFFFFF +#define FAKE_POINTER(addr) (POINTER_MASK & reinterpret_cast(addr)) + +#define DECORATOR_HILOG(op, fmt, args...) \ + do{ \ + op(LOG_CORE, "{%{public}s()-%{public}s:%{public}d}" fmt, __FUNCTION__, __FILENAME__, __LINE__, ##args); \ + } while(0) \ + +#define DRM_DEBUG_LOG(fmt, ...) DECORATOR_HILOG(HILOG_DEBUG, fmt, ##__VA_ARGS__) +#define DRM_ERR_LOG(fmt, ...) DECORATOR_HILOG(HILOG_ERROR, fmt, ##__VA_ARGS__) +#define DRM_WARNING_LOG(fmt, ...) DECORATOR_HILOG(HILOG_WARN, fmt, ##__VA_ARGS__) +#define DRM_INFO_LOG(fmt, ...) DECORATOR_HILOG(HILOG_INFO, fmt, ##__VA_ARGS__) +#define DRM_FATAL_LOG(fmt, ...) DECORATOR_HILOG(HILOG_FATAL, fmt, ##__VA_ARGS__) + +#ifndef CHECK_AND_RETURN_RET_LOG +#define CHECK_AND_RETURN_RET_LOG(cond, ret, fmt, ...) \ + do{ \ + if (!(cond)) { \ + DRM_ERR_LOG(fmt, ##__VA_ARGS__); \ + return ret; \ + } \ + } while(0) +#endif + +#ifndef CHECK_AND_RETURN_LOG +#define CHECK_AND_RETURN_LOG(cond, fmt, ...) \ + do{ \ + if (!(cond)) { \ + DRM_ERR_LOG(fmt, ##__VA_ARGS__); \ + return; \ + } \ + } while(0) +#endif + +#define POINTER_MASK 0x00FFFFFF + +#endif //OHOS_DRM_LOG_H \ No newline at end of file diff --git a/services/utils/include/drm_napi_utils.h b/services/utils/include/drm_napi_utils.h new file mode 100644 index 00000000..cc3a8bcd --- /dev/null +++ b/services/utils/include/drm_napi_utils.h @@ -0,0 +1,160 @@ +/* + * Copyright (c) 2023-2024 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef DRM_NAPI_UTILS_H_ +#define DRM_NAPI_UTILS_H_ + +#include +#include "drm_error_code.h" +#include "drm_log.h" +#include "napi/native_api.h" +#include "napi/native_node_api.h" + +#define DRM_NAPI_GET_JS_ARGS(env, info, argc, argv, thisVar) \ + do { \ + void* data; \ + napi_get_cb_info(env, info, &(argc), argv, &(thisVar), &data); \ + } while (0) + +#define DRM_NAPI_GET_JS_OBJ_WITH_ZERO_ARGS(env, info, status, thisVar) \ + do { \ + void* data; \ + status = napi_get_cb_info(env, info, nullptr, nullptr, &(thisVar), &data); \ + } while (0) + +#define DRM_NAPI_GET_JS_ASYNC_CB_REF(env, arg, count, cbRef) \ + do { \ + napi_valuetype valueType = napi_undefined; \ + napi_typeof(env, arg, &valueType); \ + if (valueType == napi_function) { \ + napi_create_reference(env, arg, count, &(cbRef)); \ + } else { \ + NAPI_ASSERT(env, false, "type mismatch"); \ + } \ + } while (0); + +#define DRM_NAPI_ASSERT_NULLPTR_CHECK(env, result) \ + do { \ + if ((result) == nullptr) { \ + napi_get_undefined(env, &(result)); \ + return result; \ + } \ + } while (0); + +#define DRM_NAPI_CREATE_PROMISE(env, callbackRef, deferred, result) \ + do { \ + if ((callbackRef) == nullptr) { \ + napi_create_promise(env, &(deferred), &(result)); \ + } \ + } while (0); + +#define DRM_NAPI_CREATE_RESOURCE_NAME(env, resource, resourceName) \ + do { \ + napi_create_string_utf8(env, resourceName, NAPI_AUTO_LENGTH, &(resource)); \ + } while (0); + +#define DRM_NAPI_CHECK_NULL_PTR_RETURN_UNDEFINED(env, ptr, ret, message) \ + do { \ + if ((ptr) == nullptr) { \ + HiLog::Error(LABEL, message); \ + napi_get_undefined(env, &(ret)); \ + return ret; \ + } \ + } while (0) + +#define DRM_NAPI_CHECK_NULL_PTR_RETURN_VOID(ptr, message) \ + do { \ + if ((ptr) == nullptr) { \ + HiLog::Error(LABEL, message); \ + return; \ + } \ + } while (0) + +#define DRM_NAPI_ASSERT_EQUAL(condition, errMsg) \ + do { \ + if (!(condition)) { \ + HiLog::Error(LABEL, errMsg); \ + return; \ + } \ + } while (0) + +#define DRM_NAPI_CHECK_AND_BREAK_LOG(cond, fmt, ...) \ + do { \ + if (!(cond)) { \ + DRM_ERR_LOG(fmt, ##__VA_ARGS__); \ + break; \ + } \ + } while (0) + +#define DRM_NAPI_CHECK_AND_RETURN_LOG(cond, fmt, ...) \ + do { \ + if (!(cond)) { \ + DRM_ERR_LOG(fmt, ##__VA_ARGS__); \ + return; \ + } \ + } while (0) + +namespace OHOS { +namespace DrmStandard { +/* Constants for array index */ +const int32_t PARAM0 = 0; +const int32_t PARAM1 = 1; +const int32_t PARAM2 = 2; + +/* Constants for array size */ +const int32_t ARGS_ZERO = 0; +const int32_t ARGS_ONE = 1; +const int32_t ARGS_TWO = 2; +const int32_t ARGS_THREE = 3; +const int32_t SIZE = 100; + +struct AsyncContext { + napi_env env; + napi_async_work work; + napi_deferred deferred; + napi_ref callbackRef; + bool status; + int32_t taskId; + int32_t errorCode; + std::string errorMsg; + std::string funcName; + bool isInvalidArgument; +}; + +enum DrmServiceError { + DRM_OK = 0, + DRM_ALLOC_ERROR, + DRM_INVALID_ARG, + DRM_UNSUPPORTED, + DRM_INVALID_SESSION_CFG, + DRM_INVALID_STATE, + DRM_UNKNOWN_ERROR, + DRM_OPERATION_NOT_ALLOWED, + DRM_HOST_ERROR, + DRM_SERVICE_ERROR, +}; + +// static bool CheckError(napi_env env, int32_t retCode) +// { +// if ((retCode != 0)) { +// std::string errorCode = std::to_string(retCode); +// napi_throw_error(env, errorCode.c_str(), ""); +// return false; +// } +// return true; +// } +} // namespace DrmStandard +} // namespace OHOS +#endif /* DRM_NAPI_UTILS_H_ */ diff --git a/services/utils/include/media_log.h b/services/utils/include/media_log.h new file mode 100644 index 00000000..e69de29b