1 /*
2 * Copyright (c) 2023 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15 #include "drm_log.h"
16 #include "drm_error_code.h"
17 #include "key_session_callback_napi.h"
18
19 namespace OHOS {
20 namespace DrmStandard {
MediaKeySessionCallbackNapi(napi_env env)21 MediaKeySessionCallbackNapi::MediaKeySessionCallbackNapi(napi_env env)
22 {
23 env_ = env;
24 }
25
~MediaKeySessionCallbackNapi()26 MediaKeySessionCallbackNapi::~MediaKeySessionCallbackNapi() {}
27
SetCallbackReference(const std::string eventType,std::shared_ptr<AutoRef> callbackPair)28 void MediaKeySessionCallbackNapi::SetCallbackReference(const std::string eventType,
29 std::shared_ptr<AutoRef> callbackPair)
30 {
31 DRM_INFO_LOG("MediaKeySessionCallbackNapi SetCallbackReference");
32 std::lock_guard<std::mutex> lock(mutex_);
33 callbackMap_[eventType] = callbackPair;
34 }
35
ClearCallbackReference(const std::string eventType)36 void MediaKeySessionCallbackNapi::ClearCallbackReference(const std::string eventType)
37 {
38 DRM_INFO_LOG("MediaKeySessionCallbackNapi ClearCallbackReference");
39 std::lock_guard<std::mutex> lock(mutex_);
40 callbackMap_.erase(eventType);
41 }
42
WorkCallbackInterruptDone(uv_work_t * work,int status)43 void MediaKeySessionCallbackNapi::WorkCallbackInterruptDone(uv_work_t *work, int status)
44 {
45 // Js Thread
46 std::shared_ptr<MediaKeySessionJsCallback> context(static_cast<MediaKeySessionJsCallback *>(work->data),
47 [work](MediaKeySessionJsCallback *ptr) {
48 delete ptr;
49 delete work;
50 });
51 DRM_NAPI_CHECK_AND_RETURN_VOID_LOG(work != nullptr, "work is nullptr");
52 MediaKeySessionJsCallback *event = reinterpret_cast<MediaKeySessionJsCallback *>(work->data);
53 DRM_NAPI_CHECK_AND_RETURN_VOID_LOG(event != nullptr, "event is nullptr");
54 std::string request = event->callbackName;
55
56 DRM_NAPI_CHECK_AND_RETURN_VOID_LOG(event->callback != nullptr, "event is nullptr");
57 napi_env env = event->callback->env_;
58 napi_ref callback = event->callback->cb_;
59
60 napi_handle_scope scope = nullptr;
61 napi_open_handle_scope(env, &scope);
62 DRM_NAPI_CHECK_AND_RETURN_VOID_LOG(scope != nullptr, "scope is nullptr");
63 DRM_DEBUG_LOG("JsCallBack %{public}s, uv_queue_work_with_qos start", request.c_str());
64 do {
65 DRM_NAPI_CHECK_AND_CLOSE_RETURN_VOID_LOG(status != UV_ECANCELED, "%{public}s cancelled", request.c_str());
66 napi_value jsCallback = nullptr;
67 napi_status nstatus = napi_get_reference_value(env, callback, &jsCallback);
68 DRM_NAPI_CHECK_AND_CLOSE_RETURN_VOID_LOG(nstatus == napi_ok && jsCallback != nullptr,
69 "%{public}s get reference value fail", request.c_str());
70
71 // Call back function
72 if (event->callbackName == "keysChange") {
73 napi_value statusTable;
74 napi_value hasNewGoodLicense;
75 nstatus = NapiParamUtils::SetDrmKeysChangeEventInfo(env, event->keysChangeParame,
76 statusTable, hasNewGoodLicense);
77 DRM_NAPI_CHECK_AND_CLOSE_RETURN_VOID_LOG(nstatus == napi_ok,
78 "%{public}s fail to create keysession keyschange callback", request.c_str());
79 napi_value args[ARGS_TWO] = { statusTable, hasNewGoodLicense };
80 napi_value result = nullptr;
81 nstatus = napi_call_function(env, nullptr, jsCallback, ARGS_TWO, args, &result);
82 DRM_NAPI_CHECK_AND_CLOSE_RETURN_VOID_LOG(nstatus == napi_ok, "%{public}s fail to call Interrupt callback",
83 request.c_str());
84 } else {
85 napi_value args[ARGS_ONE] = { nullptr };
86 nstatus = NapiParamUtils::SetDrmEventInfo(env, event->eventParame, args[PARAM0]);
87 DRM_NAPI_CHECK_AND_CLOSE_RETURN_VOID_LOG(nstatus == napi_ok && args[PARAM0] != nullptr,
88 "%{public}s fail to create keysession callback", request.c_str());
89 napi_value result = nullptr;
90 nstatus = napi_call_function(env, nullptr, jsCallback, ARGS_ONE, args, &result);
91 DRM_NAPI_CHECK_AND_CLOSE_RETURN_VOID_LOG(nstatus == napi_ok, "%{public}s fail to call Interrupt callback",
92 request.c_str());
93 }
94 } while (0);
95 napi_close_handle_scope(env, scope);
96 }
97
SendEvent(const std::string & event,int32_t extra,const std::vector<uint8_t> & data)98 void MediaKeySessionCallbackNapi::SendEvent(const std::string &event, int32_t extra, const std::vector<uint8_t> &data)
99 {
100 std::lock_guard<std::mutex> lock(mutex_);
101 std::unique_ptr<MediaKeySessionJsCallback> cb = std::make_unique<MediaKeySessionJsCallback>();
102 DRM_NAPI_CHECK_AND_RETURN_VOID_LOG(cb != nullptr, "No memory");
103 cb->callback = callbackMap_[event];
104 cb->callbackName = event;
105 cb->eventParame.extra = extra;
106 cb->eventParame.data.assign(data.begin(), data.end());
107 return OnJsCallbackInterrupt(cb);
108 }
109
OnJsCallbackInterrupt(std::unique_ptr<MediaKeySessionJsCallback> & jsCb)110 void MediaKeySessionCallbackNapi::OnJsCallbackInterrupt(std::unique_ptr<MediaKeySessionJsCallback> &jsCb)
111 {
112 uv_loop_s *loop = nullptr;
113 napi_get_uv_event_loop(env_, &loop);
114 DRM_NAPI_CHECK_AND_RETURN_VOID_LOG(loop != nullptr, "loop nullptr, No memory");
115
116 uv_work_t *work = new (std::nothrow) uv_work_t;
117 DRM_NAPI_CHECK_AND_RETURN_VOID_LOG(work != nullptr, "work nullptr, No memory");
118
119 if (jsCb.get() == nullptr) {
120 DRM_DEBUG_LOG("OnJsCallBackInterrupt: jsCb.get() is null");
121 delete work;
122 return;
123 }
124 work->data = reinterpret_cast<void *>(jsCb.get());
125
126 int ret = uv_queue_work_with_qos(
127 loop, work, [](uv_work_t *work) {}, WorkCallbackInterruptDone, uv_qos_default);
128 if (ret != 0) {
129 DRM_DEBUG_LOG("Failed to execute libuv work queue");
130 delete work;
131 } else {
132 jsCb.release();
133 }
134 }
135
SendEventKeyChanged(std::map<std::vector<uint8_t>,MediaKeySessionKeyStatus> statusTable,bool hasNewGoodLicense)136 void MediaKeySessionCallbackNapi::SendEventKeyChanged(
137 std::map<std::vector<uint8_t>, MediaKeySessionKeyStatus> statusTable, bool hasNewGoodLicense)
138 {
139 std::lock_guard<std::mutex> lock(mutex_);
140 std::unique_ptr<MediaKeySessionJsCallback> cb = std::make_unique<MediaKeySessionJsCallback>();
141 DRM_NAPI_CHECK_AND_RETURN_VOID_LOG(cb != nullptr, "No memory");
142 const std::string event = "keysChange";
143 cb->callback = callbackMap_[event];
144 cb->callbackName = event;
145 cb->keysChangeParame.hasNewGoodLicense = hasNewGoodLicense;
146 cb->keysChangeParame.statusTable = statusTable;
147 return OnJsCallbackInterrupt(cb);
148 }
149 } // DrmStandardr
150 } // OHOS