• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2024 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 "napi/native_api.h"
16 #include <cstdint>
17 #include <hilog/log.h>
18 #include "multimedia/drm_framework/native_drm_common.h"
19 #include "multimedia/drm_framework/native_drm_err.h"
20 #include "multimedia/drm_framework/native_mediakeysystem.h"
21 #include "multimedia/drm_framework/native_mediakeysession.h"
22 #include <ctime>
23 #include <cstring>
24 #include <cstdio>
25 #include <cstdlib>
26 #include <iostream>
27 #include <sys/socket.h>
28 #include <netinet/in.h>
29 #include <arpa/inet.h>
30 #include <unistd.h>
31 #include <netdb.h>
32 
33 // hilog必加的宏,不加无法打印
34 #undef LOG_DOMAIN
35 #undef LOG_TAG
36 #define LOG_DOMAIN 0x3200 // 全局domain宏,标识业务领域
37 #define LOG_TAG "MY_DRM"  // 全局tag宏,标识模块日志tag
38 // https请求宏定义
39 #define CURLEASYGETINFO 200
40 // drm module 宏定义
41 #define OPTIONS_NUM 3
42 #define DATA_BUFF_LEN 10000
43 #define DATA_BLUESPONSE_LEN 50
44 #define DATA_REQUESTINFO_LEN 139
45 #define ERROR_DATA_REQUESTINFO_LEN 139
46 #define PROVISION_DEFAULT_URL "http://default.com"
47 #define CLEAR_PLAY "com.clearplay.drm"
48 #define WISE_PLAY "com.wiseplay.drm"
49 #define LICENSE_ONLINE 1
50 #define LICENSE_OFFLINE 0
51 #define HTTP_TIMEOUT 10
52 #define URL_CERTIFICATE "https://wiseplay-prv.cloud.huawei.com/provision/v1/wiseplay"
53 #define URL_LICENSE "http://license.dev.trustdta.com:8080/drmproxy/v3/getLicense"
54 #define DRM_PLUGIN_COUNT 1
55 #define REQUESTINFODATA                                                                                        \
56     {                                                                                                          \
57         0x00, 0x00, 0x00, 0x8B, 0x70, 0x73, 0x73, 0x68, 0x00, 0x00, 0x00, 0x00, 0x3D, 0x5E, 0x6D, 0x35,        \
58         0x9B, 0x9A, 0x41, 0xE8, 0xB8, 0x43, 0xDD, 0x3C, 0x6E, 0x72, 0xC4, 0x2C, 0x00, 0x00, 0x00, 0x6B,        \
59         0x7B, 0x22, 0x76, 0x65, 0x72, 0x73, 0x69, 0x6F, 0x6E, 0x22, 0x3A, 0x22, 0x56, 0x31, 0x2E, 0x30,        \
60         0x22, 0x2C, 0x22, 0x63, 0x6F, 0x6E, 0x74, 0x65, 0x6E, 0x74, 0x49, 0x44, 0x22, 0x3A, 0x22, 0x64,        \
61         0x48, 0x4D, 0x74, 0x4D, 0x6A, 0x59, 0x30, 0x4C, 0x54, 0x45, 0x77, 0x4F, 0x44, 0x41, 0x74, 0x59,        \
62         0x57, 0x56, 0x7A, 0x22, 0x2C, 0x22, 0x6B, 0x69, 0x64, 0x73, 0x22, 0x3A, 0x5B, 0x22, 0x47, 0x2B,        \
63         0x45, 0x6B, 0x2F, 0x2B, 0x58, 0x6D, 0x55, 0x6B, 0x70, 0x42, 0x48, 0x51, 0x67, 0x58, 0x59, 0x57,        \
64         0x51, 0x51, 0x49, 0x67, 0x3D, 0x3D, 0x22, 0x5D, 0x2C, 0x22, 0x65, 0x6E, 0x73, 0x63, 0x68, 0x65,        \
65         0x6D, 0x61, 0x22, 0x3A, 0x22, 0x63, 0x62, 0x63, 0x31, 0x22, 0x7D                                       \
66     }
67 #define ERRORREQUESTINFODATA                                                                                        \
68     {                                                                                                          \
69         0x00, 0x00, 0x00, 0x8B, 0x70, 0x73, 0x73, 0x68, 0x00, 0x00, 0x00, 0x00, 0x3D, 0x5E, 0x6D, 0x35,        \
70         0x9B, 0x9A, 0x41, 0xE8, 0xB8, 0x43, 0xDD                                                               \
71     }
72 #define OFFLINERESPONSE                                                                                        \
73     {                                                                                                          \
74         0x31, 0x64, 0x6E, 0x5A, 0x32, 0x4E, 0x57, 0x74, 0x76, 0x4D, 0x47, 0x34, 0x34, 0x4E, 0x6A, 0x42,        \
75         0x30, 0x4D, 0x32, 0x77, 0x33, 0x4E, 0x67, 0x3D, 0x3D, 0x3A, 0x59, 0x7A, 0x56, 0x78, 0x63, 0x48,        \
76         0x64, 0x70, 0x61, 0x6D, 0x30, 0x34, 0x59, 0x57, 0x45, 0x34, 0x5A, 0x48, 0x6B, 0x79, 0x4D, 0x67,        \
77         0x3D, 0x3D                                                                                             \
78     }
79 #define ONLINERESPONSE                                                                                         \
80     {                                                                                                          \
81         0x30, 0x64, 0x6E, 0x5A, 0x32, 0x4E, 0x57, 0x74, 0x76, 0x4D, 0x47, 0x34, 0x34, 0x4E, 0x6A, 0x42,        \
82         0x30, 0x4D, 0x32, 0x77, 0x33, 0x4E, 0x67, 0x3D, 0x3D, 0x3A, 0x59, 0x7A, 0x56, 0x78, 0x63, 0x48,        \
83         0x64, 0x70, 0x61, 0x6D, 0x30, 0x34, 0x59, 0x57, 0x45, 0x34, 0x5A, 0x48, 0x6B, 0x79, 0x4D, 0x67,        \
84         0x3D, 0x3D                                                                                             \
85     }
86 
87 #define CHECK_RETURN(cond, ret)                                                                             \
88     do {                                                                                                    \
89         if ((cond)) {                                                                                       \
90             OH_LOG_ERROR(LOG_APP, "testCL_failed ,ret = %{public}d",ret);                                   \
91             return ret;                                                                                     \
92         }                                                                                                   \
93     } while (0)
94 #define CHECK_GOTO(expressions, label)                                                                     \
95     do {                                                                                                   \
96         if (expressions) {                                                                                 \
97             OH_LOG_ERROR(LOG_APP, "testCL_goto");                                              \
98             goto label;                                                                                    \
99         }                                                                                                 \
100     } while (0)
101 #define CHECK_PRINTF(cond, ret)                                                                                        \
102     do {                                                                                                               \
103         if (cond) {                                                                                                  \
104             OH_LOG_ERROR(LOG_APP, "testCL_check failed ,ret = %{public}d", ret);                                             \
105         }                                                                                                              \
106     } while (0)
107 
splitStringToIntArray(char * str,uint8_t * temp)108 void splitStringToIntArray(char *str,uint8_t *temp) {
109   int i = 0;
110   char *p = str;
111   char *q;
112   while ((q = strchr(p, ',')) != NULL) {
113     temp[i++] = atoi(p);
114     p = q + 1;
115   }
116   temp[i] = atoi(p);
117 }
judge_uuid(void)118 static const char* judge_uuid(void) {
119     bool isSupport = OH_MediaKeySystem_IsSupported(CLEAR_PLAY);
120     if(isSupport == 1) {
121             return CLEAR_PLAY;
122     } else {
123             return WISE_PLAY;
124     }
125 }
126 
getHttp(const std::string & url,unsigned char * request,uint32_t requestLen,unsigned char * response,int32_t * responseLen,uint32_t timeout)127 int getHttp(const std::string &url, unsigned char *request, uint32_t requestLen, unsigned char *response,
128             int32_t *responseLen, uint32_t timeout) {
129     int ret = -1;
130     struct addrinfo hints, *res, *p;
131     int sockfd;
132     struct sockaddr_in server_addr;
133     char host[1024], port[10];
134     std::string path;
135 
136     // Parse URL
137     std::string::size_type pos1 = url.find("://");
138     std::string::size_type pos2 = url.find('/', pos1 + 3);
139 
140     if (pos1 == std::string::npos) {
141             pos1 = 0;
142     } else {
143             pos1 += 3;
144     }
145 
146     std::string::size_type pos3 = url.find(':', pos1);
147     if (pos3 != std::string::npos && pos3 < pos2) {
148             strncpy(host, url.substr(pos1, pos3 - pos1).c_str(), sizeof(host));
149             strncpy(port, url.substr(pos3 + 1, pos2 - pos3 - 1).c_str(), sizeof(port));
150     } else {
151             strncpy(host, url.substr(pos1, pos2 - pos1).c_str(), sizeof(host));
152             strncpy(port, "80", sizeof(port)); // default HTTP port
153     }
154 
155     if (pos2 == std::string::npos) {
156             path = "/";
157     } else {
158             path = url.substr(pos2);
159     }
160 
161     // Prepare HTTP request
162     std::string requestStr = "POST " + path +
163                              " HTTP/1.1\r\n"
164                              "Host: " +
165                              host +
166                              "\r\n"
167                              "Content-Type: application/json\r\n"
168                              "Content-Length: " +
169                              std::to_string(requestLen) +
170                              "\r\n"
171                              "Connection: close\r\n\r\n";
172     std::string requestData(reinterpret_cast<char *>(request), requestLen);
173     requestStr += requestData;
174 
175     // Set up hints for getaddrinfo
176     memset(&hints, 0, sizeof hints);
177     hints.ai_family = AF_UNSPEC;
178     hints.ai_socktype = SOCK_STREAM;
179 
180     // Get address info
181     if (getaddrinfo(host, port, &hints, &res) != 0) {
182             std::cerr << "getaddrinfo failed" << std::endl;
183             return -1;
184     }
185 
186     // Create socket
187     for (p = res; p != nullptr; p = p->ai_next) {
188             sockfd = socket(p->ai_family, p->ai_socktype, p->ai_protocol);
189             if (sockfd == -1) {
190                 continue;
191             }
192             if (connect(sockfd, p->ai_addr, p->ai_addrlen) != -1) {
193                 break;
194             }
195             close(sockfd);
196     }
197 
198     if (p == nullptr) {
199             std::cerr << "failed to connect" << std::endl;
200             freeaddrinfo(res);
201             return -1;
202     }
203 
204     freeaddrinfo(res);
205 
206     // Send HTTP request
207     if (send(sockfd, requestStr.c_str(), requestStr.length(), 0) == -1) {
208             std::cerr << "send failed" << std::endl;
209             close(sockfd);
210             return -1;
211     }
212 
213     // Receive HTTP response
214     std::string responseStr;
215     char buffer[4096];
216     ssize_t bytesReceived;
217     while ((bytesReceived = recv(sockfd, buffer, sizeof(buffer) - 1, 0)) > 0) {
218             buffer[bytesReceived] = '\0';
219             responseStr += buffer;
220     }
221 
222     if (bytesReceived == -1) {
223             std::cerr << "recv failed" << std::endl;
224             close(sockfd);
225             return -1;
226     }
227 
228     close(sockfd);
229 
230     // Parse response
231     std::string::size_type bodyStart = responseStr.find("\r\n\r\n");
232     if (bodyStart == std::string::npos) {
233             std::cerr << "Invalid response format" << std::endl;
234             return -1;
235     }
236 
237     std::string responseBody = responseStr.substr(bodyStart + 4);
238     *responseLen = responseBody.size();
239     memcpy(response, responseBody.c_str(), *responseLen);
240 
241     return 0;
242 }
243 
getCertificate(MediaKeySystem * drmKeySystem)244 static int getCertificate(MediaKeySystem *drmKeySystem){
245     uint8_t requestData[DATA_BUFF_LEN] = {0};
246     int32_t requestLen = DATA_BUFF_LEN;
247     char defaultURLData[DATA_BUFF_LEN] = {0};
248     int32_t defaultURLDataLen = DATA_BUFF_LEN;
249     uint8_t responseData[DATA_BUFF_LEN] = {0};
250     int32_t responseDataLen_out = DATA_BUFF_LEN;
251     Drm_ErrCode ret2 = OH_MediaKeySystem_GenerateKeySystemRequest(drmKeySystem, requestData,&requestLen, defaultURLData, defaultURLDataLen);
252     CHECK_RETURN((ret2 != DRM_ERR_OK), -2);
253     if (strcmp(judge_uuid(), CLEAR_PLAY) != 0) {
254        int getRet = getHttp(defaultURLData, requestData, (uint32_t)requestLen, responseData, &responseDataLen_out,
255                             HTTP_TIMEOUT); // 设备证书申请
256        CHECK_RETURN((getRet != 0), getRet);
257     } else{
258         memset(responseData, 0x55, DATA_BLUESPONSE_LEN);
259         responseDataLen_out = DATA_BLUESPONSE_LEN;
260     }
261     Drm_ErrCode ret3 = OH_MediaKeySystem_ProcessKeySystemResponse(drmKeySystem, responseData, responseDataLen_out);
262     CHECK_RETURN((ret3 != DRM_ERR_OK), -3);
263     return 0;
264 }
getLicense(MediaKeySession * mediaKeySession,char isOnline,uint8_t * keydata,int32_t * keylen)265 static int  getLicense(MediaKeySession *mediaKeySession, char isOnline, uint8_t *keydata , int32_t *keylen){
266     DRM_MediaKeyRequestInfo requestInfo;
267     memset(&requestInfo, 0, sizeof(DRM_MediaKeyRequestInfo));
268     if(isOnline){
269         requestInfo.type = MEDIA_KEY_TYPE_ONLINE;
270     } else{
271         requestInfo.type = MEDIA_KEY_TYPE_OFFLINE;
272     }
273     unsigned char passhData[DATA_REQUESTINFO_LEN] = REQUESTINFODATA;
274     memcpy(requestInfo.initData, passhData, DATA_REQUESTINFO_LEN);
275     requestInfo.initDataLen = DATA_REQUESTINFO_LEN;
276 
277     requestInfo.optionsCount = 1;
278     memcpy(requestInfo.mimeType, (char *)"video/mp4", sizeof("video/mp4"));
279     memcpy(requestInfo.optionName[0], (char *)"optionalDataName", sizeof("optionalDataName"));
280     memcpy(requestInfo.optionData[0], (char *)"optionalDataValue", sizeof("optionalDataValue"));
281     DRM_MediaKeyRequest requestData;
282     memset(&requestData, 0, sizeof(DRM_MediaKeyRequest));
283     Drm_ErrCode ret1 = OH_MediaKeySession_GenerateMediaKeyRequest(mediaKeySession,&requestInfo, &requestData);
284     CHECK_RETURN((ret1!=DRM_ERR_OK),-1);
285 
286     uint8_t keyId[DATA_BUFF_LEN] = {0};
287     int32_t keyIdLen = DATA_BUFF_LEN;
288     uint8_t responseData[DATA_BUFF_LEN]={0};
289     int32_t responseLen = DATA_BUFF_LEN;
290     if(strcmp(judge_uuid(), CLEAR_PLAY) == 0){
291         if(isOnline){
292             uint8_t blue_onData[DATA_BLUESPONSE_LEN] = ONLINERESPONSE;
293             memcpy(responseData, blue_onData, sizeof(blue_onData));
294         }else{
295             uint8_t blue_offData[DATA_BLUESPONSE_LEN] = OFFLINERESPONSE;
296             memcpy(responseData, blue_offData, sizeof(blue_offData));
297         }
298         responseLen = DATA_BLUESPONSE_LEN;
299     }else{
300         int ret3 = getHttp(URL_LICENSE, requestData.data, (uint32_t)requestData.dataLen, responseData, &responseLen,
301                            HTTP_TIMEOUT);
302         CHECK_RETURN(ret3 != 0, ret3);
303     }
304     Drm_ErrCode ret2 = OH_MediaKeySession_ProcessMediaKeyResponse(mediaKeySession,responseData, responseLen, keyId, &keyIdLen);
305     CHECK_RETURN((ret2!=DRM_ERR_OK),-2);
306     memcpy(keydata, keyId, keyIdLen);
307     memcpy(keylen, &keyIdLen, sizeof(int32_t));
308     return 0;
309 }
getOnLicResponseData(MediaKeySession * mediaKeySession,uint8_t * response,int32_t * responselen)310 static void getOnLicResponseData(MediaKeySession *mediaKeySession, uint8_t *response , int32_t *responselen){
311     DRM_MediaKeyRequestInfo requestInfo;
312     memset(&requestInfo, 0, sizeof(DRM_MediaKeyRequestInfo));
313     unsigned char testData[DATA_REQUESTINFO_LEN] = REQUESTINFODATA;
314     requestInfo.type = MEDIA_KEY_TYPE_ONLINE;
315     requestInfo.initDataLen = sizeof(testData);
316     requestInfo.optionsCount = 1;
317     memcpy(requestInfo.mimeType, (char *)"video/mp4", sizeof("video/mp4"));
318     memcpy(requestInfo.initData, testData, sizeof(testData));
319     memcpy(requestInfo.optionName[0], (char *)"optionalDataName", sizeof("optionalDataName"));
320     memcpy(requestInfo.optionData[0], (char *)"optionalDataValue", sizeof("optionalDataValue"));
321     DRM_MediaKeyRequest requestData;
322     memset(&requestData, 0, sizeof(DRM_MediaKeyRequest));
323     Drm_ErrCode ret6 = OH_MediaKeySession_GenerateMediaKeyRequest(mediaKeySession,&requestInfo, &requestData);
324     CHECK_PRINTF((ret6 != DRM_ERR_OK), ret6);
325     uint8_t responseData[DATA_BUFF_LEN]={0};
326     int32_t responseLenOut = DATA_BUFF_LEN;
327     int ret3 = getHttp(URL_LICENSE, requestData.data, (uint32_t)requestData.dataLen,
328               responseData, &responseLenOut, HTTP_TIMEOUT);
329     CHECK_PRINTF((ret3 != 0), ret3);
330     memcpy(response, responseData, responseLenOut);
331     memcpy(responselen, &responseLenOut, sizeof(int32_t));
332     return;
333 }
GetOfflineMediaKeyIds(napi_env env,napi_callback_info info)334 static napi_value GetOfflineMediaKeyIds(napi_env env, napi_callback_info info) {
335     napi_value result;
336     const char *backString = "GetOfflineMediaKeyIds failed";
337     MediaKeySystem *drmKeySystem = nullptr;
338     Drm_ErrCode ret1 = OH_MediaKeySystem_Create(judge_uuid(), &drmKeySystem);
339     if(ret1 != DRM_ERR_OK){
340         OH_LOG_ERROR(LOG_APP, "testCL_GetOfflineMediaKeyIds failed!");
341         napi_create_string_utf8(env, backString, strlen(backString),&result);
342         return result;
343     }
344     DRM_OfflineMediakeyIdArray mediaKeyIds;
345     memset(&mediaKeyIds, 0x00, sizeof(DRM_OfflineMediakeyIdArray));
346     Drm_ErrCode ret2 = OH_MediaKeySystem_GetOfflineMediaKeyIds(drmKeySystem, &mediaKeyIds);
347     if ((ret2 == DRM_ERR_OK) && (mediaKeyIds.idsCount !=0)){
348         Drm_ErrCode ret5 = OH_MediaKeySystem_Destroy(drmKeySystem);
349         if(ret5 != DRM_ERR_OK){
350             OH_LOG_ERROR(LOG_APP, "testCL_GetOfflineMediaKeyIds failed!");
351             napi_create_string_utf8(env, backString, strlen(backString),&result);
352             return result;
353         }
354         napi_create_string_utf8(env, (const char*)&mediaKeyIds.ids[0][0], (size_t)mediaKeyIds.idsLen[0], &result);
355         return result;
356     } else{
357         OH_LOG_DEBUG(LOG_APP, "testCL_generate offline key!");
358         DRM_ContentProtectionLevel level = CONTENT_PROTECTION_LEVEL_SW_CRYPTO;
359         MediaKeySession *drmKeySession = nullptr;
360         Drm_ErrCode ret3 = OH_MediaKeySystem_CreateMediaKeySession(drmKeySystem, &level, &drmKeySession);
361         if(ret3 != DRM_ERR_OK){
362             OH_MediaKeySystem_Destroy(drmKeySystem);
363             OH_LOG_ERROR(LOG_APP, "testCL_GetOfflineMediaKeyIds failed!");
364             napi_create_string_utf8(env, backString, strlen(backString),&result);
365             return result;
366         }
367         uint8_t keyData[DATA_BUFF_LEN] = {0};
368 	    int32_t keyDataLen = DATA_BUFF_LEN;
369         int licRet = getLicense(drmKeySession, LICENSE_OFFLINE, keyData , &keyDataLen);
370         Drm_ErrCode ret4 = OH_MediaKeySession_Destroy(drmKeySession);
371         Drm_ErrCode ret5 = OH_MediaKeySystem_Destroy(drmKeySystem);
372         if((ret5 != DRM_ERR_OK) || (ret4 != DRM_ERR_OK) || (licRet != 0)){
373             OH_LOG_ERROR(LOG_APP, "testCL_GetOfflineMediaKeyIds failed!");
374             napi_create_string_utf8(env, backString, strlen(backString),&result);
375             return result;
376         }
377         napi_create_string_utf8(env, (const char*)keyData, (size_t)keyDataLen, &result);
378         return result;
379     }
380 }
GetCertificateStatus(napi_env env,napi_callback_info info)381 static napi_value GetCertificateStatus(napi_env env, napi_callback_info info) {
382     napi_value result;
383     size_t argc = 2;
384     napi_value args[2] = {nullptr};
385 
386     Drm_ErrCode ret = DRM_ERR_INVALID_VAL;
387     MediaKeySystem *drmKeySystem = nullptr;
388     ret = OH_MediaKeySystem_Create(judge_uuid(), &drmKeySystem);
389     if(ret != DRM_ERR_OK){
390         napi_create_int32(env, DRM_ERR_INVALID_VAL, &result);
391         return result;
392     }
393     DRM_CertificateStatus status = CERT_STATUS_UNAVAILABLE;
394     ret = OH_MediaKeySystem_GetCertificateStatus(drmKeySystem, &status);
395     if(ret != DRM_ERR_OK){
396         napi_create_int32(env, DRM_ERR_INVALID_VAL, &result);
397         return result;
398     }
399     ret = OH_MediaKeySystem_Destroy(drmKeySystem);
400     if(ret != DRM_ERR_OK){
401         napi_create_int32(env, DRM_ERR_INVALID_VAL, &result);
402         return result;
403     }
404     napi_create_int32(env, status, &result);
405     return result;
406 }
Test_KeySystemSupported(napi_env env,napi_callback_info info)407 static napi_value Test_KeySystemSupported(napi_env env, napi_callback_info info) {
408     bool isSupport;
409     napi_value res;
410     size_t argc = 2;
411     napi_value args[2] = {nullptr};
412     size_t uuidLen = 0;
413     char *uuid = nullptr;
414     napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
415     napi_get_value_string_utf8(env, args[0], nullptr, 0, &uuidLen);
416     uuid = new char[uuidLen + 1];
417     napi_get_value_string_utf8(env, args[0], uuid, uuidLen + 1, &uuidLen);
418 
419     char *uuid_ptr = nullptr;
420     if(strcmp(uuid," ") != 0){
421         uuid_ptr = uuid;
422     }
423     isSupport = OH_MediaKeySystem_IsSupported(uuid_ptr);
424     napi_create_int32(env, isSupport, &res);
425     return res;
426 }
Test_KeySystemSupported2(napi_env env,napi_callback_info info)427 static napi_value Test_KeySystemSupported2(napi_env env, napi_callback_info info) {
428     bool isSupport;
429     napi_value res;
430     size_t argc = 2;
431     napi_value args[2] = {nullptr};
432     size_t uuidLen = 0;
433     char *uuid = nullptr;
434     size_t mineTypeLen = 0;
435     char *mineType = nullptr;
436 
437     napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
438     napi_get_value_string_utf8(env, args[0], nullptr, 0, &uuidLen);
439     uuid = new char[uuidLen + 1];
440     napi_get_value_string_utf8(env, args[0], uuid, uuidLen + 1, &uuidLen);
441 
442     napi_get_value_string_utf8(env, args[1], nullptr, 0, &mineTypeLen);
443     mineType = new char[mineTypeLen + 1];
444     napi_get_value_string_utf8(env, args[1], mineType, mineTypeLen + 1, &mineTypeLen);
445     char *uuid_ptr = nullptr;
446     if(strcmp(uuid," ") != 0){
447         uuid_ptr = uuid;
448     }
449     char *mineType_ptr = nullptr;
450     if(strcmp(mineType," ") != 0){
451         mineType_ptr = mineType;
452     }
453     isSupport = OH_MediaKeySystem_IsSupported2(uuid_ptr, mineType_ptr);
454     napi_create_int32(env, isSupport, &res);
455     return res;
456 }
Test_KeySystemSupported3(napi_env env,napi_callback_info info)457 static napi_value Test_KeySystemSupported3(napi_env env, napi_callback_info info) {
458     bool isSupport;
459     napi_value res;
460     size_t argc = 3;
461     napi_value args[3] = {nullptr};
462     size_t uuidLen = 0;
463     char *uuid = nullptr;
464     size_t mineTypeLen = 0;
465     char *mineType = nullptr;
466     int32_t securityLevel;
467 
468     napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
469     napi_get_value_string_utf8(env, args[0], nullptr, 0, &uuidLen);
470     uuid = new char[uuidLen + 1];
471     napi_get_value_string_utf8(env, args[0], uuid, uuidLen + 1, &uuidLen);
472 
473     napi_get_value_string_utf8(env, args[1], nullptr, 0, &mineTypeLen);
474     mineType = new char[mineTypeLen + 1];
475     napi_get_value_string_utf8(env, args[1], mineType, mineTypeLen + 1, &mineTypeLen);
476 
477     napi_get_value_int32(env, args[2], &securityLevel);
478    char *uuid_ptr = nullptr;
479     if(strcmp(uuid," ") != 0){
480         uuid_ptr = uuid;
481     }
482     char *mineType_ptr = nullptr;
483     if(strcmp(mineType," ") != 0){
484         mineType_ptr = mineType;
485     }
486     isSupport = OH_MediaKeySystem_IsSupported3(uuid_ptr, mineType_ptr,(DRM_ContentProtectionLevel)securityLevel);
487     napi_create_int32(env, isSupport, &res);
488     return res;
489 }
Test_MediaKeySystem_Create(napi_env env,napi_callback_info info)490 static napi_value Test_MediaKeySystem_Create(napi_env env, napi_callback_info info) {
491     napi_value result;
492     Drm_ErrCode ret = DRM_ERR_INVALID_VAL;
493     size_t argc = 2;
494     napi_value args[2] = {nullptr};
495     size_t uuidLen = 0;
496     char *uuid = nullptr;
497     size_t inStringLen = 0;
498     char *inString = nullptr;
499 
500     napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
501     napi_get_value_string_utf8(env, args[0], nullptr, 0, &uuidLen);
502     uuid = new char[uuidLen + 1];
503     napi_get_value_string_utf8(env, args[0], uuid, uuidLen + 1, &uuidLen);
504 
505     napi_get_value_string_utf8(env, args[1], nullptr, 0, &inStringLen);
506     inString = new char[inStringLen + 1];
507     napi_get_value_string_utf8(env, args[1], inString, inStringLen + 1, &inStringLen);
508     const char *uuid_ptr = nullptr;
509     MediaKeySystem *drmKeySystem = nullptr;
510     Drm_ErrCode ret1 = DRM_ERR_INVALID_VAL;
511     if(strcmp(uuid, " ") != 0){
512         uuid_ptr = uuid;
513     }
514     if(strcmp(inString, " ") != 0){
515         ret1 = OH_MediaKeySystem_Create(uuid_ptr, &drmKeySystem);
516     }else{
517         ret1 = OH_MediaKeySystem_Create(uuid_ptr, nullptr);
518     }
519     if(ret1 == DRM_ERR_OK){
520         Drm_ErrCode ret2 = OH_MediaKeySystem_Destroy(drmKeySystem);
521         ret = (ret2 == DRM_ERR_OK) ? DRM_ERR_OK:DRM_ERR_INVALID_VAL;
522     }
523     napi_create_int32(env, ret, &result);
524     return result;
525 }
Test_MediaKeySystem_SetConfigurationString(napi_env env,napi_callback_info info)526 static napi_value Test_MediaKeySystem_SetConfigurationString(napi_env env, napi_callback_info info) {
527     napi_value result;
528     Drm_ErrCode ret = DRM_ERR_INVALID_VAL;
529     size_t argc = 3;
530     napi_value args[3] = {nullptr};
531     size_t stringLen = 0;
532     char *inString = nullptr;
533     size_t valueLen = 0;
534     char *inValue = nullptr;
535     MediaKeySystem *drmKeySystem = nullptr;
536     size_t sysLen = 0;
537     char *inSys = nullptr;
538     napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
539     napi_get_value_string_utf8(env, args[0], nullptr, 0, &sysLen);
540     inSys = new char[sysLen + 1];
541     napi_get_value_string_utf8(env, args[0], inSys, sysLen + 1, &sysLen);
542     napi_get_value_string_utf8(env, args[1], nullptr, 0, &stringLen);
543     inString = new char[stringLen + 1];
544     napi_get_value_string_utf8(env, args[1], inString, stringLen + 1, &stringLen);
545     napi_get_value_string_utf8(env, args[2], nullptr, 0, &valueLen);
546     inValue = new char[valueLen + 1];
547     napi_get_value_string_utf8(env, args[2], inValue, valueLen + 1, &valueLen);
548 
549     Drm_ErrCode ret1 = OH_MediaKeySystem_Create(judge_uuid(), &drmKeySystem);
550     MediaKeySystem *system_ptr = nullptr;
551     if (strcmp(inSys, " ") != 0) {
552        system_ptr = drmKeySystem;
553     }
554     char *name_ptr = nullptr;
555     if(strcmp(inString, " ")){
556         name_ptr = inString;
557     }
558     char *value_ptr = nullptr;
559     if(strcmp(inValue, " ")){
560         value_ptr = inValue;
561     }
562     Drm_ErrCode ret2 = OH_MediaKeySystem_SetConfigurationString(system_ptr, name_ptr, value_ptr);
563     char outBuff[DATA_BUFF_LEN]={0};
564     int32_t outBuffLen = DATA_BUFF_LEN;
565 
566     if (ret2 == DRM_ERR_OK) {
567         Drm_ErrCode ret4 = OH_MediaKeySystem_GetConfigurationString(drmKeySystem, inString, outBuff, outBuffLen);
568         if((ret2 == DRM_ERR_OK) && (strncmp(outBuff, inValue,outBuffLen) == 0)){
569             ret2 = DRM_ERR_OK;
570         }else{
571             ret2 = DRM_ERR_INVALID_VAL;
572         }
573     }
574     Drm_ErrCode ret3 = OH_MediaKeySystem_Destroy(drmKeySystem);
575     if (ret1 == DRM_ERR_OK && ret2 == DRM_ERR_OK && ret3 == DRM_ERR_OK) {
576         ret = DRM_ERR_OK;
577     } else {
578         ret = DRM_ERR_INVALID_VAL;
579     }
580     napi_create_int32(env, ret, &result);
581     return result;
582 }
Test_MediaKeySystem_GetConfigurationString(napi_env env,napi_callback_info info)583 static napi_value Test_MediaKeySystem_GetConfigurationString(napi_env env, napi_callback_info info) {
584     napi_value result;
585     Drm_ErrCode ret = DRM_ERR_INVALID_VAL;
586     size_t argc = 4;
587     napi_value args[4] = {nullptr};
588     size_t sysLen = 0;
589     char *inSys = nullptr;
590     size_t configNameLen = 0;
591     char *configName = nullptr;
592     size_t valueLen = 0;
593     char *inValue = nullptr;
594     int32_t outValueLen = 0;
595 
596     napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
597     napi_get_value_string_utf8(env, args[0], nullptr, 0, &sysLen);
598     inSys = new char[sysLen + 1];
599     napi_get_value_string_utf8(env, args[0], inSys, sysLen + 1, &sysLen);
600     napi_get_value_string_utf8(env, args[1], nullptr, 0, &configNameLen);
601     configName = new char[configNameLen + 1];
602     napi_get_value_string_utf8(env, args[1], configName, configNameLen + 1, &configNameLen);
603     napi_get_value_string_utf8(env, args[2], nullptr, 0, &valueLen);
604     inValue = new char[valueLen + 1];
605     napi_get_value_string_utf8(env, args[2], inValue, valueLen + 1, &valueLen);
606     napi_get_value_int32(env,args[3], &outValueLen);
607 
608     MediaKeySystem *drmKeySystem = nullptr;
609     Drm_ErrCode ret1 = OH_MediaKeySystem_Create(judge_uuid(), &drmKeySystem);
610 
611     const char *set_val = "test";
612     Drm_ErrCode ret2 = DRM_ERR_OK;
613     if ((0 == strcmp(judge_uuid(), CLEAR_PLAY)) && (0 != strcmp(configName, " "))) {
614         ret2 = OH_MediaKeySystem_SetConfigurationString(drmKeySystem,configName, set_val);
615     }
616     MediaKeySystem *system_ptr = nullptr;
617     if (strcmp(inSys, " ") != 0) {
618         system_ptr = drmKeySystem;
619     }
620     char *name_ptr = nullptr;
621     if (0 != strcmp(configName, " ")) {
622         name_ptr = configName;
623     }
624     char *outValue = nullptr;
625     char outBuff[DATA_BUFF_LEN];
626     if (0 != strncmp(inValue, " ",valueLen)) {
627         outValue = outBuff;
628     }
629     Drm_ErrCode ret3 = OH_MediaKeySystem_GetConfigurationString(system_ptr, name_ptr, outValue, outValueLen);
630 
631     if(ret3 == DRM_ERR_OK){
632         if(strcmp(judge_uuid(), CLEAR_PLAY)==0){
633             ret3 = (strncmp(outValue, set_val, strlen(set_val)) == 0) ? DRM_ERR_OK : DRM_ERR_INVALID_VAL;
634         }
635     }
636     Drm_ErrCode ret4 = OH_MediaKeySystem_Destroy(drmKeySystem);
637     if (ret1 == DRM_ERR_OK && ret2 == DRM_ERR_OK && ret3 == DRM_ERR_OK && ret4 == DRM_ERR_OK) { //
638         ret = DRM_ERR_OK;
639     } else {
640         ret = DRM_ERR_INVALID_VAL;
641     }
642     napi_create_int32(env, ret, &result);
643     return result;
644 }
Test_MediaKeySystem_SetConfigurationByteArray(napi_env env,napi_callback_info info)645 static napi_value Test_MediaKeySystem_SetConfigurationByteArray(napi_env env, napi_callback_info info) {
646     napi_value result;
647     Drm_ErrCode ret = DRM_ERR_INVALID_VAL;
648     size_t argc = 4;
649     napi_value args[4] = {nullptr};
650     size_t stringLen = 0;
651     char *inString = nullptr;
652     size_t valueLen = 0;
653     char *inValue = nullptr;
654     size_t sysLen = 0;
655     char *inSys = nullptr;
656     MediaKeySystem *drmKeySystem = nullptr;
657 
658     napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
659     napi_get_value_string_utf8(env, args[0], nullptr, 0, &sysLen);
660     inSys = new char[sysLen + 1];
661     napi_get_value_string_utf8(env, args[0], inSys, sysLen + 1, &sysLen);
662     napi_get_value_string_utf8(env, args[1], nullptr, 0, &stringLen);
663     inString = new char[stringLen + 1];
664     napi_get_value_string_utf8(env, args[1], inString, stringLen + 1, &stringLen);
665     napi_get_value_string_utf8(env, args[2], nullptr, 0, &valueLen);
666     inValue = new char[valueLen + 1];
667     napi_get_value_string_utf8(env, args[2], inValue, valueLen + 1, &valueLen);
668     int32_t valueDataLen = 0;
669     napi_get_value_int32(env,args[3], &valueDataLen);
670 
671     Drm_ErrCode ret1 = OH_MediaKeySystem_Create(judge_uuid(), &drmKeySystem);
672     char *name_ptr = nullptr;
673     if (0 != strncmp(inString, " ",stringLen)) {
674          name_ptr = inString;
675     }
676     uint8_t *data_ptr = nullptr;
677     if (0 != strncmp(inValue, " ",valueLen)) {
678         data_ptr = (uint8_t *)inValue;
679     }
680     MediaKeySystem *system_ptr =nullptr;
681     if (0 != strncmp(inSys, " ",sysLen)){
682         system_ptr = drmKeySystem;
683     }
684     uint8_t outValue[DATA_BUFF_LEN] = {0};
685     int32_t outValueLen = DATA_BUFF_LEN;
686     Drm_ErrCode ret2 = OH_MediaKeySystem_SetConfigurationByteArray(system_ptr, name_ptr, data_ptr, valueDataLen);
687 
688     if (ret2 == DRM_ERR_OK) {
689         Drm_ErrCode ret4 = OH_MediaKeySystem_GetConfigurationByteArray(drmKeySystem, name_ptr, outValue, &outValueLen);
690         if(ret4 == DRM_ERR_OK){
691             ret2 = ( 0 == memcmp(outValue, data_ptr,valueDataLen)) ? DRM_ERR_OK:DRM_ERR_INVALID_VAL;
692         }
693     }
694     Drm_ErrCode ret3 = OH_MediaKeySystem_Destroy(drmKeySystem);
695     if (ret1 == DRM_ERR_OK && ret2 == DRM_ERR_OK && ret3 == DRM_ERR_OK) {
696         ret = DRM_ERR_OK;
697     } else {
698         ret = DRM_ERR_INVALID_VAL;
699     }
700     napi_create_int32(env, ret, &result);
701     return result;
702 }
Test_MediaKeySystem_GetConfigurationByteArray(napi_env env,napi_callback_info info)703 static napi_value Test_MediaKeySystem_GetConfigurationByteArray(napi_env env, napi_callback_info info) {
704     napi_value result;
705     Drm_ErrCode ret = DRM_ERR_INVALID_VAL;
706     size_t argc = 5;
707     napi_value args[5] = {nullptr};
708     size_t sysLen = 0;
709     char *inSys = nullptr;
710     size_t configNameLen = 0;
711     char *configName = nullptr;
712     char *value = nullptr;
713     size_t valueLen = 0;
714     size_t valueLenStringLen = 0;
715     char *valueLenString = nullptr;
716     MediaKeySystem *drmKeySystem = nullptr;
717     napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
718     napi_get_value_string_utf8(env, args[0], nullptr, 0, &sysLen);
719     inSys = new char[sysLen + 1];
720     napi_get_value_string_utf8(env, args[0], inSys, sysLen + 1, &sysLen);
721 
722     napi_get_value_string_utf8(env, args[1], nullptr, 0, &configNameLen);
723     configName = new char[configNameLen + 1];
724     napi_get_value_string_utf8(env, args[1], configName, configNameLen + 1, &configNameLen);
725 
726     napi_get_value_string_utf8(env, args[2], nullptr, 0, &valueLen);
727     value = new char[valueLen + 1];
728     napi_get_value_string_utf8(env, args[2], value, valueLen + 1, &valueLen);
729 
730     napi_get_value_string_utf8(env, args[3], nullptr, 0, &valueLenStringLen);
731     valueLenString = new char[valueLenStringLen + 1];
732     napi_get_value_string_utf8(env, args[3], valueLenString, valueLenStringLen + 1, &valueLenStringLen);
733 
734     Drm_ErrCode ret2 = DRM_ERR_OK;
735     Drm_ErrCode ret1 = OH_MediaKeySystem_Create(judge_uuid(), &drmKeySystem);
736     if (0==strcmp(judge_uuid(), CLEAR_PLAY)){
737         uint8_t valueData[10];
738         memset(valueData, 0x55, 10);
739         ret2 = OH_MediaKeySystem_SetConfigurationByteArray(drmKeySystem,configName, valueData, 10); // 蓝区没有默认值,需要先set;
740     }
741     MediaKeySystem *system_ptr =  nullptr;
742     if( 0!=strcmp(inSys, " ")){
743         system_ptr = drmKeySystem;
744     }
745     const char *configName_ptr =  nullptr;
746     if( 0!=strcmp(configName, " ")){
747         configName_ptr = configName;
748     }
749     uint8_t *value_ptr = nullptr;
750     uint8_t valueData[DATA_BUFF_LEN]={0};
751     if(0!= strcmp(value, " ")){
752         value_ptr = valueData;
753     }
754     int32_t *valueLen_ptr = nullptr;
755     int32_t valueLenData = DATA_BUFF_LEN;
756     if (0!=strcmp(valueLenString, " ")) {
757         valueLen_ptr = &valueLenData;
758     }
759     Drm_ErrCode ret3 = OH_MediaKeySystem_GetConfigurationByteArray(system_ptr, configName_ptr, value_ptr, valueLen_ptr);
760     Drm_ErrCode ret4 = OH_MediaKeySystem_Destroy(drmKeySystem);
761     if (ret1 == DRM_ERR_OK && ret2 == DRM_ERR_OK && ret3 == DRM_ERR_OK && ret4 == DRM_ERR_OK) {
762         ret = DRM_ERR_OK;
763     } else {
764         ret = DRM_ERR_INVALID_VAL;
765     }
766     napi_create_int32(env, ret, &result);
767     return result;
768 }
Test_MediaKeySystem_GetStatistics(napi_env env,napi_callback_info info)769 static napi_value Test_MediaKeySystem_GetStatistics(napi_env env, napi_callback_info info) {
770     napi_value result;
771     size_t argc = 2;
772     napi_value args[2] = {nullptr};
773     size_t sysLen = 0;
774     char *inSys = nullptr;
775     size_t statisticsLen = 0;
776     char *statistics = nullptr;
777     Drm_ErrCode ret = DRM_ERR_INVALID_VAL;
778 
779     napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
780     napi_get_value_string_utf8(env, args[0], nullptr, 0, &sysLen);
781     inSys = new char[sysLen + 1];
782     napi_get_value_string_utf8(env, args[0], inSys, sysLen + 1, &sysLen);
783     napi_get_value_string_utf8(env, args[1], nullptr, 0, &statisticsLen);
784     statistics = new char[statisticsLen + 1];
785     napi_get_value_string_utf8(env, args[1], statistics, statisticsLen + 1, &statisticsLen);
786 
787     MediaKeySystem *drmKeySystem = nullptr;
788     Drm_ErrCode ret1 = OH_MediaKeySystem_Create(judge_uuid(), &drmKeySystem);
789 
790     MediaKeySystem *system_ptr = nullptr;
791     if(strcmp(inSys, " ") !=0){
792        system_ptr = drmKeySystem;
793     }
794     DRM_Statistics *statisticsData_ptr = nullptr;
795     DRM_Statistics statisticsData;
796     memset(&statisticsData, 0x00, sizeof(DRM_Statistics));
797     if (strcmp(statistics, " ")) {
798         statisticsData_ptr = &statisticsData;
799     }
800     Drm_ErrCode ret2 = OH_MediaKeySystem_GetStatistics(system_ptr, statisticsData_ptr);
801     OH_LOG_DEBUG(LOG_APP, "testCL_statisticsCount = %{public}d",statisticsData.statisticsCount);
802     for(int i=0; i<statisticsData.statisticsCount; i++){
803         OH_LOG_DEBUG(LOG_APP, "testCL_statisticsName[%{public}d]: %{public}s", i, statisticsData.statisticsName[i]);
804         OH_LOG_DEBUG(LOG_APP, "testCL_Description[%{public}d]: %{public}s", i, statisticsData.statisticsDescription[i]);
805     }
806 
807     Drm_ErrCode ret3 = OH_MediaKeySystem_Destroy(drmKeySystem);
808     if (ret1 == DRM_ERR_OK && ret2 == DRM_ERR_OK && ret3 == DRM_ERR_OK) {
809         ret = DRM_ERR_OK;
810     } else {
811         ret = DRM_ERR_INVALID_VAL;
812     }
813     napi_create_int32(env, ret, &result);
814     return result;
815 }
Test_MediaKeySystem_GetMaxContentProtectionLevel(napi_env env,napi_callback_info info)816 static napi_value Test_MediaKeySystem_GetMaxContentProtectionLevel(napi_env env, napi_callback_info info) {
817     napi_value result;
818     size_t argc = 3;
819     napi_value args[3] = {nullptr};
820     size_t sysLen = 0;
821     char *inSys = nullptr;
822     size_t levelLen = 0;
823     char *inLevel = nullptr;
824     Drm_ErrCode ret = DRM_ERR_INVALID_VAL;
825     napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
826     napi_get_value_string_utf8(env, args[0], nullptr, 0, &sysLen);
827     inSys = new char[sysLen + 1];
828     napi_get_value_string_utf8(env, args[0], inSys, sysLen + 1, &sysLen);
829     napi_get_value_string_utf8(env, args[1], nullptr, 0, &levelLen);
830     inLevel = new char[levelLen + 1];
831     napi_get_value_string_utf8(env, args[1], inLevel, levelLen + 1, &levelLen);
832 
833     MediaKeySystem *drmKeySystem = nullptr;
834     Drm_ErrCode ret1 = OH_MediaKeySystem_Create(judge_uuid(), &drmKeySystem);
835     MediaKeySystem *system_ptr = nullptr;
836     if (strcmp(inSys, " ") != 0) {
837         system_ptr = drmKeySystem;
838     }
839     DRM_ContentProtectionLevel securityLevel = CONTENT_PROTECTION_LEVEL_UNKNOWN;
840     DRM_ContentProtectionLevel *level_ptr = nullptr;
841     if (strcmp(inLevel, " ") != 0) {
842         level_ptr = &securityLevel;
843     }
844     Drm_ErrCode ret2 = OH_MediaKeySystem_GetMaxContentProtectionLevel(system_ptr, level_ptr);
845     OH_LOG_DEBUG(LOG_APP, "testCL_MaxContentProtectionLevel is %{public}d", securityLevel);
846     Drm_ErrCode ret3 = OH_MediaKeySystem_Destroy(drmKeySystem);
847     if (ret1 == DRM_ERR_OK && ret2 == DRM_ERR_OK && ret3 == DRM_ERR_OK) {
848         ret = DRM_ERR_OK;
849     } else {
850         ret = DRM_ERR_INVALID_VAL;
851     }
852     napi_create_int32(env, ret, &result);
853     return result;
854 }
Test_CreateMediaKeySession(napi_env env,napi_callback_info info)855 static napi_value Test_CreateMediaKeySession(napi_env env, napi_callback_info info) {
856     napi_value result;
857     size_t argc = 3;
858     napi_value args[3] = {nullptr};
859     size_t sysLen = 0;
860     char *inSys = nullptr;
861     int32_t level = 0;
862     size_t sessionLen = 0;
863     char *inSession = nullptr;
864     Drm_ErrCode ret = DRM_ERR_INVALID_VAL;
865     napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
866     napi_get_value_string_utf8(env, args[0], nullptr, 0, &sysLen);
867     inSys = new char[sysLen + 1];
868     napi_get_value_string_utf8(env, args[0], inSys, sysLen + 1, &sysLen);
869     napi_get_value_int32(env, args[1], &level);
870     napi_get_value_string_utf8(env, args[2], nullptr, 0, &sessionLen);
871     inSession = new char[sessionLen + 1];
872     napi_get_value_string_utf8(env, args[2], inSession, sessionLen + 1, &sessionLen);
873 
874     MediaKeySystem *drmKeySystem = nullptr;
875     Drm_ErrCode ret1 = OH_MediaKeySystem_Create(judge_uuid(), &drmKeySystem);
876     MediaKeySystem *system_ptr = nullptr;
877     if((0 !=strcmp(inSys, " "))){
878         system_ptr = drmKeySystem;
879     }
880     DRM_ContentProtectionLevel *level_ptr = nullptr;
881     if(level != -1){
882         level_ptr = (DRM_ContentProtectionLevel *)&level;
883     }
884     MediaKeySession *drmKeySession = nullptr;
885     MediaKeySession **session_ptr = nullptr;
886     if (0 != strcmp(inSession, " ")) {
887         session_ptr = &drmKeySession;
888     }
889     Drm_ErrCode ret2 = OH_MediaKeySystem_CreateMediaKeySession(system_ptr, level_ptr, session_ptr);
890     Drm_ErrCode ret3 = DRM_ERR_OK;
891     if(ret2 == DRM_ERR_OK && (drmKeySession != nullptr)){
892         Drm_ErrCode ret3 = OH_MediaKeySession_Destroy(drmKeySession);
893     }
894     Drm_ErrCode ret4 = OH_MediaKeySystem_Destroy(drmKeySystem);
895     if (ret1 == DRM_ERR_OK && ret2 == DRM_ERR_OK && ret3 == DRM_ERR_OK && ret4 == DRM_ERR_OK) {
896         ret = DRM_ERR_OK;
897     } else {
898         ret = DRM_ERR_INVALID_VAL;
899     }
900     napi_create_int32(env, ret, &result);
901     return result;
902 }
Test_MediaKeySystem_GenerateKeySystemRequest(napi_env env,napi_callback_info info)903 static napi_value Test_MediaKeySystem_GenerateKeySystemRequest(napi_env env, napi_callback_info info) {
904     napi_value result;
905     size_t argc = 5;
906     napi_value args[5] = {nullptr};
907     size_t sysLen = 0;
908     char *inSys = nullptr;
909     size_t requestStringLen = 0;
910     char *inRequestString = nullptr;
911     size_t requestLenStingLen = 0;
912     char *requestLenSting = nullptr;
913     size_t defaultUrlStingLen = 0;
914     char *defaultUrlSting = nullptr;
915     int32_t defaultUrlLen= 0;
916     Drm_ErrCode ret = DRM_ERR_INVALID_VAL;
917 
918     napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
919     napi_get_value_string_utf8(env, args[0], nullptr, 0, &sysLen);
920     inSys = new char[sysLen + 1];
921     napi_get_value_string_utf8(env, args[0], inSys, sysLen + 1, &sysLen);
922 
923     napi_get_value_string_utf8(env, args[1], nullptr, 0, &requestStringLen);
924     inRequestString = new char[requestStringLen + 1];
925     napi_get_value_string_utf8(env, args[1], inRequestString, requestStringLen + 1, &requestStringLen);
926 
927     napi_get_value_string_utf8(env, args[2], nullptr, 0, &requestLenStingLen);
928     requestLenSting = new char[requestLenStingLen + 1];
929     napi_get_value_string_utf8(env, args[2], requestLenSting, requestLenStingLen + 1, &requestLenStingLen);
930 
931     napi_get_value_string_utf8(env, args[3], nullptr, 0, &defaultUrlStingLen);
932     defaultUrlSting = new char[defaultUrlStingLen + 1];
933     napi_get_value_string_utf8(env, args[3], defaultUrlSting, defaultUrlStingLen + 1, &defaultUrlStingLen);
934 
935     napi_get_value_int32(env, args[4], &defaultUrlLen);
936 
937     MediaKeySystem *drmKeySystem = nullptr;
938     Drm_ErrCode ret1 = OH_MediaKeySystem_Create(judge_uuid(), &drmKeySystem);
939     MediaKeySystem *system_ptr = nullptr;
940     if ((0 != strcmp(inSys, " "))) {
941         system_ptr= drmKeySystem;
942     }
943     uint8_t *request_ptr = nullptr;
944     uint8_t requestData[DATA_BUFF_LEN]={0};
945     if((0 != strcmp(inRequestString, " "))){
946         request_ptr = requestData;
947     }
948     int32_t *requestLen_ptr= nullptr;
949     int32_t outRequestLen = DATA_BUFF_LEN;
950     if((0 != strcmp(requestLenSting, " "))){
951         requestLen_ptr  = &outRequestLen;
952     }
953     char *urlData_ptr =  nullptr;
954     char urlData[DATA_BUFF_LEN] ={0};
955     int32_t UrlLen = DATA_BUFF_LEN;
956     if ((0 != strcmp(defaultUrlSting, " "))) {
957         urlData_ptr = urlData;
958     }
959     if((defaultUrlLen !=0) && (defaultUrlLen !=-1)){
960         defaultUrlLen = UrlLen;
961     }
962     Drm_ErrCode ret2 = OH_MediaKeySystem_GenerateKeySystemRequest(system_ptr,request_ptr, requestLen_ptr, urlData_ptr, defaultUrlLen);
963     Drm_ErrCode ret3 = OH_MediaKeySystem_Destroy(drmKeySystem);
964     if (ret1 == DRM_ERR_OK && ret2 == DRM_ERR_OK && ret3 == DRM_ERR_OK ) {
965         ret = DRM_ERR_OK;
966     } else {
967         ret = DRM_ERR_INVALID_VAL;
968     }
969     napi_create_int32(env, ret, &result);
970     return result;
971 }
Test_MediaKeySystem_ProcessKeySystemResponse(napi_env env,napi_callback_info info)972 static napi_value Test_MediaKeySystem_ProcessKeySystemResponse(napi_env env, napi_callback_info info) {
973     napi_value result;
974     size_t argc = 3;
975     napi_value args[3] = {nullptr};
976     size_t inSys_len = 0;
977     char *inSys = nullptr;
978     size_t response_len = 0;
979     char *response = nullptr;
980     Drm_ErrCode ret = DRM_ERR_INVALID_VAL;
981     int32_t responseDataLen = 0;
982     napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
983     napi_get_value_string_utf8(env, args[0], nullptr, 0, &inSys_len);
984     inSys = new char[inSys_len + 1];
985     napi_get_value_string_utf8(env, args[0], inSys, inSys_len + 1, &inSys_len);
986     napi_get_value_string_utf8(env, args[1], nullptr, 0, &response_len);
987     response = new char[response_len + 1];
988     napi_get_value_string_utf8(env, args[1], response, response_len + 1, &response_len);
989     napi_get_value_int32(env, args[2], &responseDataLen);
990 
991     MediaKeySystem *drmKeySystem = nullptr;
992     Drm_ErrCode ret1 = OH_MediaKeySystem_Create(judge_uuid(), &drmKeySystem);
993     MediaKeySystem *system_ptr =  nullptr;
994     if ((0 != strcmp(inSys, " "))) {
995         system_ptr = drmKeySystem;
996     }
997     uint8_t *responseData_ptr = nullptr;
998     uint8_t requestData[DATA_BUFF_LEN] = {0};
999     int32_t requestLen = DATA_BUFF_LEN;
1000     char defaultURLData[DATA_BUFF_LEN] = {0};
1001     int32_t defaultURLDataLen = DATA_BUFF_LEN;
1002     uint8_t responseData[DATA_BUFF_LEN] = {0};
1003     int32_t responseDataLen_out = DATA_BUFF_LEN;
1004 
1005     if (strncmp(response, "genResponse",response_len) == 0) {
1006         Drm_ErrCode ret3 = OH_MediaKeySystem_GenerateKeySystemRequest(drmKeySystem, requestData,&requestLen, defaultURLData, defaultURLDataLen);
1007         if(ret3 == DRM_ERR_OK){
1008             int getRet = getHttp(defaultURLData, requestData, (uint32_t)requestLen,
1009                                      responseData, &responseDataLen_out, HTTP_TIMEOUT);  // 设备证书申请
1010             if(getRet != 0){
1011                 OH_LOG_ERROR(LOG_APP, "testCL_getHttp failed ,Ret = %{public}d",getRet);
1012             }
1013             responseData_ptr = responseData;
1014             if(responseDataLen > 0){
1015                  responseDataLen = responseDataLen_out;
1016             }
1017         }
1018     }else if(0 != strncmp(response, " ",response_len)){
1019         responseData_ptr = (uint8_t *)response;
1020         OH_LOG_DEBUG(LOG_APP, "testCL_input responseData");
1021     } else {
1022         responseData_ptr = nullptr;
1023         OH_LOG_DEBUG(LOG_APP, "testCL_responseData is nullptr");
1024     }
1025     Drm_ErrCode ret4 = OH_MediaKeySystem_ProcessKeySystemResponse(system_ptr, responseData_ptr, responseDataLen);
1026     Drm_ErrCode ret5 = OH_MediaKeySystem_Destroy(drmKeySystem);
1027     if (ret1 == DRM_ERR_OK && ret4 == DRM_ERR_OK && ret5 == DRM_ERR_OK) {
1028         ret = DRM_ERR_OK;
1029     } else {
1030         ret = DRM_ERR_INVALID_VAL;
1031     }
1032     napi_create_int32(env, ret, &result);
1033     return result;
1034 }
Test_MediaKeySystem_GetOfflineMediaKeyIds(napi_env env,napi_callback_info info)1035 static napi_value Test_MediaKeySystem_GetOfflineMediaKeyIds(napi_env env, napi_callback_info info) {
1036     napi_value result;
1037     size_t argc = 3;
1038     napi_value args[3] = {nullptr};
1039     size_t sysLen = 0;
1040     char *inSys = nullptr;
1041     size_t keyIdsLen = 0;
1042     char *keyIds = nullptr;
1043     size_t keySourceLen = 0;
1044     char *keySource= nullptr;
1045     Drm_ErrCode ret = DRM_ERR_INVALID_VAL;
1046     Drm_ErrCode ret3 = DRM_ERR_INVALID_VAL;
1047     MediaKeySystem *drmKeySystem = nullptr;
1048     MediaKeySession *drmKeySession = nullptr;
1049     DRM_ContentProtectionLevel contentProtectionLevel = CONTENT_PROTECTION_LEVEL_SW_CRYPTO;
1050 
1051     napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
1052     napi_get_value_string_utf8(env, args[0], nullptr, 0, &sysLen);
1053     inSys = new char[sysLen + 1];
1054     napi_get_value_string_utf8(env, args[0], inSys, sysLen + 1, &sysLen);
1055     napi_get_value_string_utf8(env, args[1], nullptr, 0, &keyIdsLen);
1056     keyIds = new char[keyIdsLen + 1];
1057     napi_get_value_string_utf8(env, args[1], keyIds, keyIdsLen + 1, &keyIdsLen);
1058     napi_get_value_string_utf8(env, args[2], nullptr, 0, &keySourceLen);
1059     keySource = new char[keySourceLen + 1];
1060     napi_get_value_string_utf8(env, args[2], keySource, keySourceLen + 1, &keySourceLen);
1061 
1062     Drm_ErrCode ret1 = OH_MediaKeySystem_Create(judge_uuid(), &drmKeySystem);
1063     Drm_ErrCode ret2 = OH_MediaKeySystem_CreateMediaKeySession(drmKeySystem, &contentProtectionLevel, &drmKeySession);
1064     uint8_t keyData[DATA_BUFF_LEN] = {0};
1065     int32_t keyDataLen = DATA_BUFF_LEN;
1066     int reslicense = getLicense(drmKeySession, LICENSE_OFFLINE,keyData,&keyDataLen); // 确保生成离线license
1067     if(reslicense != 0){
1068         OH_LOG_ERROR(LOG_APP, "testCL_get offline license failed!");
1069     }
1070     MediaKeySystem *system_ptr = nullptr;
1071     if((0 != strcmp(inSys, " "))){
1072       system_ptr = drmKeySystem;
1073     }
1074     DRM_OfflineMediakeyIdArray *offlineMediaKeyIds_ptr = nullptr;
1075     DRM_OfflineMediakeyIdArray mediaKeyIds;
1076     memset(&mediaKeyIds, 0, sizeof(DRM_OfflineMediakeyIdArray));
1077     if((0 != strcmp(keyIds, " "))){
1078       offlineMediaKeyIds_ptr = &mediaKeyIds;
1079     }
1080     ret3 = OH_MediaKeySystem_GetOfflineMediaKeyIds(system_ptr, offlineMediaKeyIds_ptr);
1081     if (ret3 == DRM_ERR_OK){
1082         ret3 = (mediaKeyIds.idsCount != 0)?DRM_ERR_OK:DRM_ERR_INVALID_VAL;
1083 	    OH_LOG_DEBUG(LOG_APP, "testCL_offline mediaKeyIds count is %{public}d",mediaKeyIds.idsCount);
1084         for(int i=0; i<mediaKeyIds.idsCount; i++){ // 与./data/vendor/mediakeysystem/chinadrm/license.dat中keyid(十六进制)一致
1085             OH_LOG_DEBUG(LOG_APP, "testCL_offline mediaKeyIds[%{public}d] is %{public}s", i ,mediaKeyIds.ids[i]);
1086         }
1087     }
1088     Drm_ErrCode ret4 = OH_MediaKeySession_Destroy(drmKeySession);
1089     Drm_ErrCode ret5 = OH_MediaKeySystem_Destroy(drmKeySystem);
1090 
1091     if (ret1 == DRM_ERR_OK && ret2 == DRM_ERR_OK && ret3 == DRM_ERR_OK && ret4 == DRM_ERR_OK && ret5 == DRM_ERR_OK) {
1092         ret = DRM_ERR_OK;
1093     } else {
1094         ret = DRM_ERR_INVALID_VAL;
1095     }
1096     napi_create_int32(env, ret, &result);
1097     return result;
1098 }
Test_MediaKeySystem_GetOfflineMediaKeyStatus(napi_env env,napi_callback_info info)1099 static napi_value Test_MediaKeySystem_GetOfflineMediaKeyStatus(napi_env env, napi_callback_info info) {
1100     napi_value result;
1101     size_t argc = 5;
1102     napi_value args[5] = {nullptr};
1103     size_t sysLen = 0;
1104     char *inSys = nullptr;
1105     size_t keyIdStringLen = 0;
1106     char *keyIdString = nullptr;
1107     int32_t offlineMediaKeyIdLen = 0;
1108     size_t statusStringLen = 0;
1109     char *statusString = nullptr;
1110     int32_t expectStatus;
1111     Drm_ErrCode ret = DRM_ERR_INVALID_VAL;
1112 
1113     napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
1114     napi_get_value_string_utf8(env, args[0], nullptr, 0, &sysLen);
1115     inSys = new char[sysLen + 1];
1116     napi_get_value_string_utf8(env, args[0], inSys, sysLen + 1, &sysLen);
1117     napi_get_value_string_utf8(env, args[1], nullptr, 0, &keyIdStringLen);
1118     keyIdString = new char[keyIdStringLen + 1];
1119     napi_get_value_string_utf8(env, args[1], keyIdString, keyIdStringLen + 1, &keyIdStringLen);
1120     napi_get_value_int32(env, args[2], &offlineMediaKeyIdLen);
1121     napi_get_value_string_utf8(env, args[3], nullptr, 0, &statusStringLen);
1122     statusString = new char[statusStringLen + 1];
1123     napi_get_value_string_utf8(env, args[3], statusString, statusStringLen + 1, &statusStringLen);
1124     napi_get_value_int32(env, args[4], &expectStatus);
1125 
1126     MediaKeySystem *drmKeySystem = nullptr;
1127     Drm_ErrCode ret1 = OH_MediaKeySystem_Create(judge_uuid(), &drmKeySystem);
1128     DRM_ContentProtectionLevel level = CONTENT_PROTECTION_LEVEL_SW_CRYPTO;
1129     MediaKeySession *drmKeySession = nullptr;
1130     Drm_ErrCode ret2 = OH_MediaKeySystem_CreateMediaKeySession(drmKeySystem, &level, &drmKeySession);
1131     uint8_t *keyId_ptr = nullptr;
1132 	uint8_t keyData[DATA_BUFF_LEN] = {0};
1133 	int32_t keyDataLen = DATA_BUFF_LEN;
1134     if(strncmp(keyIdString, "genKey",keyIdStringLen) == 0){
1135         getLicense(drmKeySession, LICENSE_OFFLINE, keyData, &keyDataLen); // 确保生成离线license
1136         keyId_ptr =  keyData;
1137         if(offlineMediaKeyIdLen > 0){
1138             offlineMediaKeyIdLen = keyDataLen;
1139         }
1140      }else if(strncmp(keyIdString, " ",keyIdStringLen) != 0){
1141         keyId_ptr = (uint8_t *)keyIdString;
1142      }else {
1143         keyId_ptr = nullptr;
1144      }
1145     MediaKeySystem *system_ptr = nullptr;
1146     if((0 != strncmp(inSys, " ",sysLen))){
1147         system_ptr = drmKeySystem;
1148     }
1149     DRM_OfflineMediaKeyStatus status = OFFLINE_MEDIA_KEY_STATUS_UNKNOWN;
1150     DRM_OfflineMediaKeyStatus *status_ptr = nullptr;
1151     if((0 != strncmp(statusString, " ",statusStringLen))){
1152         status_ptr = &status;
1153     }
1154     Drm_ErrCode ret4 = OH_MediaKeySystem_GetOfflineMediaKeyStatus(system_ptr, keyId_ptr, offlineMediaKeyIdLen, status_ptr);
1155     Drm_ErrCode ret5 = DRM_ERR_INVALID_VAL;
1156     if (ret4 == DRM_ERR_OK){// 获取到license
1157         ret5 = ((int32_t)status == expectStatus) ? DRM_ERR_OK:DRM_ERR_INVALID_VAL;
1158     }
1159     Drm_ErrCode ret6 = OH_MediaKeySession_Destroy(drmKeySession);
1160     Drm_ErrCode ret7 = OH_MediaKeySystem_Destroy(drmKeySystem);
1161     if (ret1 == DRM_ERR_OK && ret2 == DRM_ERR_OK && ret4 == DRM_ERR_OK  && ret6 == DRM_ERR_OK && ret7 == DRM_ERR_OK ) {
1162         ret = DRM_ERR_OK;
1163     } else {
1164         ret = DRM_ERR_INVALID_VAL;
1165     }
1166     napi_create_int32(env, ret, &result);
1167     return result;
1168 }
Test_MediaKeySystem_ClearOfflineMediaKeys(napi_env env,napi_callback_info info)1169 static napi_value Test_MediaKeySystem_ClearOfflineMediaKeys(napi_env env, napi_callback_info info) {
1170     napi_value result;
1171     size_t argc = 3;
1172     napi_value args[3] = {nullptr};
1173     char *inSys = nullptr;
1174     size_t sysLen = 0;
1175     char *keyIdString = nullptr;
1176     size_t keyIdStringLen= 0;
1177     int32_t offlineMediaKeyIdLen = 0;
1178     Drm_ErrCode ret = DRM_ERR_INVALID_VAL;
1179     MediaKeySystem *drmKeySystem= nullptr;
1180 
1181     napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
1182     napi_get_value_string_utf8(env, args[0], nullptr, 0, &sysLen);
1183     inSys = new char[sysLen + 1];
1184     napi_get_value_string_utf8(env, args[0], inSys, sysLen + 1, &sysLen);
1185     napi_get_value_string_utf8(env, args[1], nullptr, 0, &keyIdStringLen);
1186     keyIdString = new char[keyIdStringLen + 1];
1187     napi_get_value_string_utf8(env, args[1], keyIdString, keyIdStringLen + 1, &keyIdStringLen);
1188     napi_get_value_int32(env, args[2], &offlineMediaKeyIdLen);
1189 
1190     DRM_ContentProtectionLevel level = CONTENT_PROTECTION_LEVEL_SW_CRYPTO;
1191     MediaKeySession *mediaKeySession = nullptr;
1192     Drm_ErrCode ret1 = OH_MediaKeySystem_Create(judge_uuid(), &drmKeySystem);
1193     Drm_ErrCode ret2 = OH_MediaKeySystem_CreateMediaKeySession(drmKeySystem,&level, &mediaKeySession);
1194 
1195     MediaKeySystem *system_ptr = nullptr;
1196     if ((0 != strcmp(inSys, " "))) {
1197         system_ptr = drmKeySystem;
1198     }
1199     uint8_t *keyId_ptr = nullptr;
1200 	uint8_t keyData[DATA_BUFF_LEN] = {0};
1201 	int32_t keyDataLen = DATA_BUFF_LEN;
1202     if(strncmp(keyIdString, "genKey",keyIdStringLen) == 0){
1203         getLicense(mediaKeySession, LICENSE_OFFLINE, keyData, &keyDataLen); // 确保生成离线license
1204         keyId_ptr =  keyData;
1205         if(offlineMediaKeyIdLen > 0){
1206             offlineMediaKeyIdLen = keyDataLen;
1207         }
1208      }else if ((0 != strcmp(keyIdString, " "))) {
1209         keyId_ptr = (uint8_t *)keyIdString;
1210     }else{
1211         keyId_ptr =  nullptr;
1212     }
1213     Drm_ErrCode ret3 = OH_MediaKeySystem_ClearOfflineMediaKeys(system_ptr, keyId_ptr, offlineMediaKeyIdLen);
1214     DRM_OfflineMediaKeyStatus status = OFFLINE_MEDIA_KEY_STATUS_USABLE;
1215     if(ret3 == DRM_ERR_OK){
1216         Drm_ErrCode ret6 = OH_MediaKeySystem_GetOfflineMediaKeyStatus(system_ptr, keyId_ptr, offlineMediaKeyIdLen, &status);
1217         if(ret6 == DRM_ERR_OK){
1218             ret3 = (status != OFFLINE_MEDIA_KEY_STATUS_USABLE)?DRM_ERR_OK:DRM_ERR_INVALID_VAL;
1219         }
1220     }
1221     Drm_ErrCode ret4 = OH_MediaKeySession_Destroy(mediaKeySession);
1222     Drm_ErrCode ret5 = OH_MediaKeySystem_Destroy(drmKeySystem);
1223     if (ret1 == DRM_ERR_OK && ret2 == DRM_ERR_OK && ret3 == DRM_ERR_OK && ret4 == DRM_ERR_OK && ret5 == DRM_ERR_OK ) {
1224         ret = DRM_ERR_OK;
1225     } else {
1226         ret = DRM_ERR_INVALID_VAL;
1227     }
1228     napi_create_int32(env, ret, &result);
1229     return result;
1230 }
Test_MediaKeySystem_GetCertificateStatus(napi_env env,napi_callback_info info)1231 static napi_value Test_MediaKeySystem_GetCertificateStatus(napi_env env, napi_callback_info info) {
1232     napi_value result;
1233     size_t argc = 2;
1234     napi_value args[2] = {nullptr};
1235     size_t sysLen = 0;
1236     char *inSys = nullptr;
1237     size_t StatusLen = 0;
1238     char *Status = nullptr;
1239     Drm_ErrCode ret = DRM_ERR_INVALID_VAL;
1240     MediaKeySystem *drmKeySystem = nullptr;
1241 
1242     napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
1243     napi_get_value_string_utf8(env, args[0], nullptr, 0, &sysLen);
1244     inSys = new char[sysLen + 1];
1245     napi_get_value_string_utf8(env, args[0], inSys, sysLen + 1, &sysLen);
1246     napi_get_value_string_utf8(env, args[1], nullptr, 0, &StatusLen);
1247     Status = new char[StatusLen + 1];
1248     napi_get_value_string_utf8(env, args[1], Status, StatusLen + 1, &StatusLen);
1249 
1250     Drm_ErrCode ret1 = OH_MediaKeySystem_Create(judge_uuid(), &drmKeySystem);
1251     MediaKeySystem *system_ptr = nullptr;
1252     if ((0 != strcmp(inSys, " "))) {
1253         system_ptr = drmKeySystem;
1254     }
1255     DRM_CertificateStatus status = CERT_STATUS_UNAVAILABLE;
1256     DRM_CertificateStatus *cerStatus_ptr = nullptr;
1257     if((0 != strcmp(Status, " "))){
1258         cerStatus_ptr = &status;
1259     }
1260     Drm_ErrCode ret2 = OH_MediaKeySystem_GetCertificateStatus(system_ptr, cerStatus_ptr);
1261     if(ret2 == DRM_ERR_OK){
1262         OH_LOG_DEBUG(LOG_APP, "testCL_CertificateStatus is %{public}d",status);
1263     }
1264     Drm_ErrCode ret3 = OH_MediaKeySystem_Destroy(drmKeySystem);
1265     if (ret1 == DRM_ERR_OK && ret2 == DRM_ERR_OK && ret3 == DRM_ERR_OK) {
1266         ret = DRM_ERR_OK;
1267     } else {
1268         ret = DRM_ERR_INVALID_VAL;
1269     }
1270     napi_create_int32(env, ret, &result);
1271     return result;
1272 }
Test_MediaKeySystem_Destroy(napi_env env,napi_callback_info info)1273 static napi_value Test_MediaKeySystem_Destroy(napi_env env, napi_callback_info info) {
1274     napi_value result;
1275     Drm_ErrCode ret = DRM_ERR_INVALID_VAL;
1276     size_t argc = 2;
1277     napi_value args[2] = {nullptr};
1278     size_t inSysLen = 0;
1279     char *inSys = nullptr;
1280     int32_t count = 0;
1281     napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
1282     napi_get_value_string_utf8(env, args[0], nullptr, 0, &inSysLen);
1283     inSys = new char[inSysLen + 1];
1284     napi_get_value_string_utf8(env, args[0], inSys, inSysLen + 1, &inSysLen);
1285     napi_get_value_int32(env, args[1], &count);
1286 
1287     MediaKeySystem *drmKeySystem = nullptr;
1288     Drm_ErrCode ret1 = OH_MediaKeySystem_Create(judge_uuid(), &drmKeySystem);
1289     MediaKeySystem *system_ptr = nullptr;
1290     if (strcmp(inSys, " ") != 0) {
1291         system_ptr = drmKeySystem;
1292     }
1293     Drm_ErrCode ret2 = DRM_ERR_INVALID_VAL;
1294     for(int i=0; i<count; i++){
1295         ret2 = OH_MediaKeySystem_Destroy(system_ptr);
1296     }
1297     if (ret1 == DRM_ERR_OK && ret2 == DRM_ERR_OK) {
1298         ret = DRM_ERR_OK;
1299     }
1300     napi_create_int32(env, ret, &result);
1301     return result;
1302 }
1303 uint8_t Flag_SysCallBack = 0;
TestSystemCallBack(DRM_EventType eventType,uint8_t * eventInfo,int32_t infoLen,char * extra)1304 Drm_ErrCode TestSystemCallBack(DRM_EventType eventType, uint8_t *eventInfo,int32_t infoLen, char *extra) {
1305     OH_LOG_DEBUG(LOG_APP, "testCL_Enter systemCallBack func!");
1306     Flag_SysCallBack =1;
1307     return DRM_ERR_OK;
1308 }
Test_MediaKeySystem_SetMediaKeySystemCallback(napi_env env,napi_callback_info info)1309 static napi_value Test_MediaKeySystem_SetMediaKeySystemCallback(napi_env env, napi_callback_info info) {
1310     napi_value result;
1311     size_t argc = 2;
1312     napi_value args[2] = {nullptr};
1313     size_t inSysLen = 0;
1314     char *inSys = nullptr;
1315     Drm_ErrCode ret = DRM_ERR_INVALID_VAL;
1316 
1317     napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
1318     napi_get_value_string_utf8(env, args[0], nullptr, 0, &inSysLen);
1319     inSys = new char[inSysLen + 1];
1320     napi_get_value_string_utf8(env, args[0], inSys, inSysLen + 1, &inSysLen);
1321 
1322     MediaKeySystem *drmKeySystem = nullptr;
1323     Drm_ErrCode ret1 = OH_MediaKeySystem_Create(judge_uuid(), &drmKeySystem);
1324     MediaKeySystem *system_ptr = nullptr;
1325     if ((0 != strcmp(inSys, " "))) {
1326         system_ptr = drmKeySystem;
1327         OH_LOG_DEBUG(LOG_APP, "testCL_SetMediaKeySystemCallback system is not nullptr!");
1328     }
1329     Drm_ErrCode ret2 = OH_MediaKeySystem_SetMediaKeySystemCallback(system_ptr, TestSystemCallBack);
1330     MediaKeySession *drmKeySession = nullptr;
1331     DRM_ContentProtectionLevel level = CONTENT_PROTECTION_LEVEL_SW_CRYPTO;
1332     // Trigger callback
1333     if(strcmp(judge_uuid(), CLEAR_PLAY) == 0){ // clearpaly直接申请设备证书,触发回调
1334         int retCert = getCertificate(drmKeySystem);
1335         CHECK_PRINTF((retCert != 0), retCert);
1336         Drm_ErrCode ret4 = OH_MediaKeySystem_CreateMediaKeySession(drmKeySystem, &level, &drmKeySession);
1337         if (ret4 == DRM_ERR_OK) {
1338             Drm_ErrCode ret5 = OH_MediaKeySession_Destroy(drmKeySession);
1339             CHECK_PRINTF((ret5 != DRM_ERR_OK), ret5);
1340         }
1341         if((ret2 == DRM_ERR_OK)){
1342             ret2 = (Flag_SysCallBack == 1) ? DRM_ERR_OK : DRM_ERR_INVALID_VAL;
1343         }
1344     } else{ // wiseplay手动删除证书,创建session失败,触发回调
1345         DRM_CertificateStatus status = CERT_STATUS_UNAVAILABLE;
1346         Drm_ErrCode ret3 = OH_MediaKeySystem_GetCertificateStatus(drmKeySystem, &status);
1347         if((ret3 == DRM_ERR_OK)&&(status != CERT_STATUS_PROVISIONED)) {
1348             Drm_ErrCode ret4 = OH_MediaKeySystem_CreateMediaKeySession(drmKeySystem, &level, &drmKeySession);
1349             if (ret4 == DRM_ERR_OK) {
1350                  Drm_ErrCode ret5 = OH_MediaKeySession_Destroy(drmKeySession);
1351                  CHECK_PRINTF((ret5 != DRM_ERR_OK), ret5);
1352             }
1353             if((ret2 == DRM_ERR_OK)){
1354                 ret2 = (Flag_SysCallBack == 1) ? DRM_ERR_OK : DRM_ERR_INVALID_VAL;
1355             }
1356             int retCert = getCertificate(drmKeySystem);
1357             CHECK_PRINTF((retCert != 0), retCert);
1358         }
1359     }
1360     Drm_ErrCode ret6 = OH_MediaKeySystem_Destroy(drmKeySystem);
1361     Flag_SysCallBack = 0; // 清标志
1362     if (ret1 == DRM_ERR_OK && ret2 == DRM_ERR_OK && ret6 == DRM_ERR_OK) {
1363         ret = DRM_ERR_OK;
1364     } else {
1365         ret = DRM_ERR_INVALID_VAL;
1366     }
1367     napi_create_int32(env, ret, &result);
1368     return result;
1369 }
1370 // session
Test_MediaKeySession_GenerateMediaKeyRequest(napi_env env,napi_callback_info info)1371 static napi_value Test_MediaKeySession_GenerateMediaKeyRequest(napi_env env, napi_callback_info info) {
1372     napi_value result;
1373     size_t argc = 3;
1374     napi_value args[3] = {nullptr};
1375     size_t inSessionLen = 0;
1376     char *inSession = nullptr;
1377     size_t infoLen = 0;
1378     char *infoString = nullptr;
1379     size_t RequestLen = 0;
1380     char *request = nullptr;
1381     Drm_ErrCode ret = DRM_ERR_INVALID_VAL;
1382     MediaKeySystem *drmKeySystem = nullptr;
1383     MediaKeySession *drmKeySession = nullptr;
1384     DRM_ContentProtectionLevel sessionLevel = CONTENT_PROTECTION_LEVEL_SW_CRYPTO;
1385 
1386     napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
1387     napi_get_value_string_utf8(env, args[0], nullptr, 0, &inSessionLen);
1388     inSession = new char[inSessionLen + 1];
1389     napi_get_value_string_utf8(env, args[0], inSession, inSessionLen + 1, &inSessionLen);
1390 
1391     napi_get_value_string_utf8(env, args[1], nullptr, 0, &infoLen);
1392     infoString = new char[infoLen + 1];
1393     napi_get_value_string_utf8(env, args[1], infoString, infoLen + 1, &infoLen);
1394 
1395     napi_get_value_string_utf8(env, args[2], nullptr, 0, &RequestLen);
1396     request = new char[RequestLen + 1];
1397     napi_get_value_string_utf8(env, args[2], request, RequestLen + 1, &RequestLen);
1398 
1399     unsigned char testData[DATA_REQUESTINFO_LEN] = REQUESTINFODATA;
1400     unsigned char errorTestData[ERROR_DATA_REQUESTINFO_LEN] = ERRORREQUESTINFODATA;
1401     DRM_MediaKeyRequestInfo requestInfo;
1402     requestInfo.type = MEDIA_KEY_TYPE_OFFLINE;
1403     if ((0 == strcmp(infoString, "err_dataLen"))) {
1404         requestInfo.initDataLen = -1;
1405     } else {
1406         requestInfo.initDataLen = sizeof(testData);
1407     }
1408     if ((0 == strcmp(infoString, "err_data"))) {
1409         memcpy(requestInfo.initData, errorTestData, sizeof(errorTestData));
1410     }else{
1411         memcpy(requestInfo.initData, testData, sizeof(testData));
1412     }
1413     if ((0 == strcmp(infoString, "err_mineType"))) {
1414         memcpy(requestInfo.mimeType, "111/222222", sizeof("111/222222"));
1415     } else {
1416         memcpy(requestInfo.mimeType, "video/mp4", sizeof("video/mp4"));
1417     }
1418     if ((0 == strcmp(infoString, "err_optCount"))) {
1419         requestInfo.optionsCount = MAX_MEDIA_KEY_REQUEST_OPTION_COUNT << 1;
1420         for (int i = 0; i < MAX_MEDIA_KEY_REQUEST_OPTION_COUNT; i++) {
1421             strncpy(requestInfo.optionName[i], "optionalDataName", strlen("optionalDataName"));
1422             strncpy(requestInfo.optionData[i], "optionalDataValue", strlen("optionalDataValue"));
1423         }
1424     } else {
1425         requestInfo.optionsCount = OPTIONS_NUM;
1426         for (int i = 0; i < requestInfo.optionsCount; i++) {
1427             strncpy(requestInfo.optionName[i], "optionalDataName", strlen("optionalDataName"));
1428             strncpy(requestInfo.optionData[i], "optionalDataValue", strlen("optionalDataValue"));
1429         }
1430     }
1431     DRM_MediaKeyRequestInfo *requestInfo_ptr = nullptr;
1432    if ((0 != strcmp(infoString, " "))) {
1433         requestInfo_ptr = &requestInfo;
1434     }
1435     DRM_MediaKeyRequest *mediaKeyRequest_ptr = nullptr;
1436     DRM_MediaKeyRequest mediaKeyRequest;
1437     if ((0 != strcmp(request, " "))) {
1438         mediaKeyRequest_ptr = &mediaKeyRequest;
1439     }
1440     Drm_ErrCode ret1 = OH_MediaKeySystem_Create(judge_uuid(), &drmKeySystem);
1441     Drm_ErrCode ret2 = OH_MediaKeySystem_CreateMediaKeySession(drmKeySystem,&sessionLevel , &drmKeySession);
1442     MediaKeySession *session_ptr = nullptr;
1443     if ((0 != strcmp(inSession, " "))) {
1444         session_ptr = drmKeySession;
1445     }
1446     Drm_ErrCode ret3 = OH_MediaKeySession_GenerateMediaKeyRequest(session_ptr, requestInfo_ptr, mediaKeyRequest_ptr);
1447     Drm_ErrCode ret4 = OH_MediaKeySession_Destroy(drmKeySession);
1448     Drm_ErrCode ret5 = OH_MediaKeySystem_Destroy(drmKeySystem);
1449     if (ret1 == DRM_ERR_OK && ret2 == DRM_ERR_OK && ret3 == DRM_ERR_OK && ret4 == DRM_ERR_OK && ret5 == DRM_ERR_OK) {
1450         ret = DRM_ERR_OK;
1451     } else {
1452         ret = DRM_ERR_INVALID_VAL;
1453     }
1454     napi_create_int32(env, ret, &result);
1455     return result;
1456 }
Test_MediaKeySession_ProcessMediaKeyResponse(napi_env env,napi_callback_info info)1457 static napi_value Test_MediaKeySession_ProcessMediaKeyResponse(napi_env env, napi_callback_info info) {
1458     napi_value result;
1459     size_t argc = 5;
1460     napi_value args[5] = {nullptr};
1461     size_t inSessionLen = 0;
1462     char *inSession = nullptr;
1463     size_t inResponseStringLen = 0;
1464     char *inResponseString = nullptr;
1465     int32_t responseLen = 0;
1466     size_t KeyIdStringLen = 0;
1467     char *KeyIdString = nullptr;
1468     size_t KeyIdLenStringLen = 0;
1469     char *KeyIdLenString = nullptr;
1470     Drm_ErrCode ret = DRM_ERR_INVALID_VAL;
1471     DRM_ContentProtectionLevel sessionLevel = CONTENT_PROTECTION_LEVEL_SW_CRYPTO;
1472     MediaKeySystem *drmKeySystem = nullptr;
1473     MediaKeySession *drmKeySession = nullptr;
1474 
1475     napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
1476     napi_get_value_string_utf8(env, args[0], nullptr, 0, &inSessionLen);
1477     inSession = new char[inSessionLen + 1];
1478     napi_get_value_string_utf8(env, args[0], inSession, inSessionLen + 1, &inSessionLen);
1479 
1480     napi_get_value_string_utf8(env, args[1], nullptr, 0, &inResponseStringLen);
1481     inResponseString = new char[inResponseStringLen + 1];
1482     napi_get_value_string_utf8(env, args[1], inResponseString, inResponseStringLen + 1, &inResponseStringLen);
1483 
1484     napi_get_value_int32(env, args[2], &responseLen);
1485 
1486     napi_get_value_string_utf8(env, args[3], nullptr, 0, &KeyIdStringLen);
1487     KeyIdString = new char[KeyIdStringLen + 1];
1488     napi_get_value_string_utf8(env, args[3], KeyIdString, KeyIdStringLen + 1, &KeyIdStringLen);
1489 
1490     napi_get_value_string_utf8(env, args[4], nullptr, 0, &KeyIdLenStringLen);
1491     KeyIdLenString = new char[KeyIdLenStringLen + 1];
1492     napi_get_value_string_utf8(env, args[4], KeyIdLenString, KeyIdLenStringLen + 1, &KeyIdLenStringLen);
1493 
1494     Drm_ErrCode ret1 = OH_MediaKeySystem_Create(judge_uuid(), &drmKeySystem);
1495     Drm_ErrCode ret2 = OH_MediaKeySystem_CreateMediaKeySession(drmKeySystem, &sessionLevel, &drmKeySession);
1496 
1497     MediaKeySession *session_ptr = nullptr;
1498     if((0 != strcmp(inSession, " "))){
1499         session_ptr = drmKeySession;
1500     }
1501     uint8_t *response_ptr = nullptr;
1502     uint8_t responseData[DATA_BUFF_LEN]={0};
1503     int32_t responseLenOut = DATA_BUFF_LEN;
1504     if(strcmp(inResponseString, "genResponse") == 0){
1505         getOnLicResponseData(drmKeySession, responseData, &responseLenOut);
1506         OH_LOG_DEBUG(LOG_APP, "testCL_responseData is %{public}s , len =  %{public}d",responseData, responseLenOut);
1507         response_ptr = responseData;
1508         if(responseLen >0){
1509             responseLen = responseLenOut;
1510         }
1511     }else if (strcmp(inResponseString, " ") != 0) {
1512         response_ptr = (uint8_t *)malloc(inResponseStringLen);
1513         splitStringToIntArray(inResponseString,response_ptr);
1514     }
1515     uint8_t *keyIds_ptr = nullptr;
1516     uint8_t keyIdsBuff[DATA_BUFF_LEN];
1517     if ((0 != strcmp(KeyIdString, " "))) {
1518         keyIds_ptr = keyIdsBuff;
1519     }
1520     int32_t *keyIdLen_ptr = nullptr;
1521     int32_t keyIdLen = DATA_BUFF_LEN;
1522     if ((0 != strcmp(KeyIdLenString, " "))) {
1523         keyIdLen_ptr = &keyIdLen;
1524     }
1525     Drm_ErrCode ret3 = OH_MediaKeySession_ProcessMediaKeyResponse(session_ptr, response_ptr, responseLen, keyIds_ptr, keyIdLen_ptr);
1526     Drm_ErrCode ret4 = OH_MediaKeySession_Destroy(drmKeySession);
1527     Drm_ErrCode ret5 = OH_MediaKeySystem_Destroy(drmKeySystem);
1528     if((response_ptr != nullptr) && (strcmp(inResponseString, "genResponse") != 0)){
1529         free(response_ptr);
1530     }
1531     if (ret1 == DRM_ERR_OK && ret2 == DRM_ERR_OK && ret3 == DRM_ERR_OK && ret4 == DRM_ERR_OK && ret5 == DRM_ERR_OK) {
1532         ret = DRM_ERR_OK;
1533     } else {
1534         ret = DRM_ERR_INVALID_VAL;
1535     }
1536     napi_create_int32(env, ret, &result);
1537     return result;
1538 }
Test_MediaKeySession_CheckMediaKeyStatus(napi_env env,napi_callback_info info)1539 static napi_value Test_MediaKeySession_CheckMediaKeyStatus(napi_env env, napi_callback_info info) {
1540     napi_value result;
1541     size_t argc = 3;
1542     napi_value args[3] = {nullptr};
1543     size_t inSessionLen = 0;
1544     char *inSession = nullptr;
1545     size_t inStatusLen = 0;
1546     char *inStatus = nullptr;
1547     int32_t licenseNum = 0;
1548     Drm_ErrCode ret = DRM_ERR_INVALID_VAL;
1549     MediaKeySystem *drmKeySystem = nullptr;
1550     MediaKeySession *drmKeySession = nullptr;
1551     DRM_ContentProtectionLevel sessionLevel = CONTENT_PROTECTION_LEVEL_SW_CRYPTO;
1552 
1553     napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
1554     napi_get_value_string_utf8(env, args[0], nullptr, 0, &inSessionLen);
1555     inSession = new char[inSessionLen + 1];
1556     napi_get_value_string_utf8(env, args[0], inSession, inSessionLen + 1, &inSessionLen);
1557     napi_get_value_string_utf8(env, args[1], nullptr, 0, &inStatusLen);
1558     inStatus = new char[inStatusLen + 1];
1559     napi_get_value_string_utf8(env, args[1], inStatus, inStatusLen + 1, &inStatusLen);
1560     napi_get_value_int32(env, args[2], &licenseNum);
1561 
1562     Drm_ErrCode ret1 = OH_MediaKeySystem_Create(judge_uuid(), &drmKeySystem);
1563     Drm_ErrCode ret2 = OH_MediaKeySystem_CreateMediaKeySession(drmKeySystem, &sessionLevel, &drmKeySession);
1564 
1565     uint8_t keyData[10][DATA_BUFF_LEN]={0};
1566     int32_t keyDataLen[10] = {DATA_BUFF_LEN};
1567     for(int i=0; i<licenseNum;i++){
1568         int res = getLicense(drmKeySession, LICENSE_ONLINE,keyData[i],&keyDataLen[i]);
1569         CHECK_PRINTF((res != 0), res);
1570     }
1571     MediaKeySession *sessoin_ptr  = nullptr;
1572     DRM_MediaKeyStatus mediaKeyStatus;
1573     memset(&mediaKeyStatus, 0, sizeof(DRM_MediaKeyStatus));
1574     DRM_MediaKeyStatus *mediaKeyStatus_ptr = nullptr;
1575     if(0 !=strcmp(inSession, " ")){
1576         sessoin_ptr = drmKeySession;
1577     }
1578     if (0 != strcmp(inStatus, " ")) {
1579         mediaKeyStatus_ptr = &mediaKeyStatus;
1580     }
1581     Drm_ErrCode ret3 = OH_MediaKeySession_CheckMediaKeyStatus(sessoin_ptr, mediaKeyStatus_ptr);
1582     if(ret3 == DRM_ERR_OK){
1583         OH_LOG_DEBUG(LOG_APP, "testCL_CheckMediaKeyStatus, count = %{public}d",
1584                      mediaKeyStatus.statusCount);
1585         for (int i = 0; i < mediaKeyStatus.statusCount; i++) {
1586             OH_LOG_DEBUG(LOG_APP, "testCL_CheckMediaKeyStatus statusName[%{public}d] is %{public}s", i,
1587                          mediaKeyStatus.statusName[i]);
1588             OH_LOG_DEBUG(LOG_APP, "testCL_CheckMediaKeyStatus statusValue[%{public}d] is %{public}s", i,
1589                          mediaKeyStatus.statusValue[i]);
1590         }
1591     }else{
1592         OH_LOG_ERROR(LOG_APP, "testCL_CheckMediaKeyStatus failed,ret = %{public}d",ret3);
1593     }
1594     Drm_ErrCode ret4 = OH_MediaKeySession_Destroy(drmKeySession);
1595     Drm_ErrCode ret5 = OH_MediaKeySystem_Destroy(drmKeySystem);
1596     if (ret1 == DRM_ERR_OK && ret2 == DRM_ERR_OK && ret3 == DRM_ERR_OK && ret4 == DRM_ERR_OK && ret5 == DRM_ERR_OK) {
1597         ret = DRM_ERR_OK;
1598     } else {
1599         ret = DRM_ERR_INVALID_VAL;
1600     }
1601     napi_create_int32(env, ret, &result);
1602     return result;
1603 }
Test_MediaKeySession_ClearMediaKeys(napi_env env,napi_callback_info info)1604 static napi_value Test_MediaKeySession_ClearMediaKeys(napi_env env, napi_callback_info info) {
1605     napi_value result;
1606     size_t argc = 2;
1607     napi_value args[2] = {nullptr};
1608     size_t inSessionLen = 0;
1609     char *inSession = nullptr;
1610     Drm_ErrCode ret = DRM_ERR_INVALID_VAL;
1611     DRM_ContentProtectionLevel sessionLevel = CONTENT_PROTECTION_LEVEL_SW_CRYPTO;
1612     MediaKeySystem *drmKeySystem = nullptr;
1613     MediaKeySession *drmKeySession = nullptr;
1614 
1615     napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
1616     napi_get_value_string_utf8(env, args[0], nullptr, 0, &inSessionLen);
1617     inSession = new char[inSessionLen + 1];
1618     napi_get_value_string_utf8(env, args[0], inSession, inSessionLen + 1, &inSessionLen);
1619 
1620     Drm_ErrCode ret1 = OH_MediaKeySystem_Create(judge_uuid(), &drmKeySystem);
1621     Drm_ErrCode ret2 = OH_MediaKeySystem_CreateMediaKeySession(drmKeySystem, &sessionLevel, &drmKeySession);
1622     uint8_t keyData[DATA_BUFF_LEN] = {0};
1623     int32_t keyDataLen = DATA_BUFF_LEN;
1624     getLicense(drmKeySession, LICENSE_ONLINE,keyData,&keyDataLen);
1625     DRM_MediaKeyStatus status_before;
1626     memset(&status_before, 0, sizeof(DRM_MediaKeyStatus));
1627     Drm_ErrCode ret6 = OH_MediaKeySession_CheckMediaKeyStatus(drmKeySession, &status_before);
1628     if(ret6 == DRM_ERR_OK){
1629         for(int i=0; i<status_before.statusCount; i++){
1630             OH_LOG_DEBUG(LOG_APP, "testCL_before clear, statusName[%{public}d] is %{public}s", i, status_before.statusName[i]);
1631             OH_LOG_DEBUG(LOG_APP, "testCL_before clear, statusValue[%{public}d] is %{public}s", i, status_before.statusValue[i]);
1632         }
1633     }else{
1634         OH_LOG_ERROR(LOG_APP, "testCL_before ClearMediaKeys check status failed ret = %{public}d", ret6);
1635     }
1636     MediaKeySession *session_ptr = nullptr;
1637     if (0 != strcmp(inSession, " ")) {
1638         session_ptr = drmKeySession;
1639     }
1640     Drm_ErrCode ret3 = OH_MediaKeySession_ClearMediaKeys(session_ptr);
1641     DRM_MediaKeyStatus status_after;
1642     memset(&status_after, 0, sizeof(DRM_MediaKeyStatus));
1643     Drm_ErrCode ret7 = OH_MediaKeySession_CheckMediaKeyStatus(drmKeySession, &status_after);
1644     if(ret7 == DRM_ERR_OK){
1645         for(int i=0; i<status_after.statusCount; i++){
1646             OH_LOG_DEBUG(LOG_APP, "testCL_after clear, statusName[%{public}d] is %{public}s", i, status_after.statusName[i]);
1647             OH_LOG_DEBUG(LOG_APP, "testCL_after clear, statusValue[%{public}d] is %{public}s", i, status_after.statusValue[i]);
1648         }
1649     }else{
1650         OH_LOG_ERROR(LOG_APP, "testCL_after ClearMediaKeys check status failed,ret = %{public}d",ret7);
1651     }
1652     Drm_ErrCode ret4 = OH_MediaKeySession_Destroy(drmKeySession);
1653     Drm_ErrCode ret5 = OH_MediaKeySystem_Destroy(drmKeySystem);
1654     if (ret1 == DRM_ERR_OK && ret2 == DRM_ERR_OK && ret3 == DRM_ERR_OK && ret4 == DRM_ERR_OK && ret5 == DRM_ERR_OK) {
1655         ret = DRM_ERR_OK;
1656     } else {
1657         ret = DRM_ERR_INVALID_VAL;
1658     }
1659     napi_create_int32(env, ret ,&result);
1660     return result;
1661 }
Test_MediaKeySession_GetContentProtectionLevel(napi_env env,napi_callback_info info)1662 static napi_value Test_MediaKeySession_GetContentProtectionLevel(napi_env env, napi_callback_info info) {
1663     napi_value result;
1664     size_t argc = 2;
1665     napi_value args[2] = {nullptr};
1666     size_t inSessionLen = 0;
1667     char *inSession = nullptr;
1668     size_t inLevelLen = 0;
1669     char *inLevelSting = nullptr;
1670     Drm_ErrCode ret = DRM_ERR_INVALID_VAL;
1671     MediaKeySystem *drmKeySystem = nullptr;
1672     MediaKeySession *drmKeySession = nullptr;
1673     DRM_ContentProtectionLevel sessionLevel = CONTENT_PROTECTION_LEVEL_SW_CRYPTO;
1674 
1675     napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
1676     napi_get_value_string_utf8(env, args[0], nullptr, 0, &inSessionLen);
1677     inSession = new char[inSessionLen + 1];
1678     napi_get_value_string_utf8(env, args[0], inSession, inSessionLen + 1, &inSessionLen);
1679 
1680     napi_get_value_string_utf8(env, args[1], nullptr, 0, &inLevelLen);
1681     inLevelSting = new char[inLevelLen + 1];
1682     napi_get_value_string_utf8(env, args[1], inLevelSting, inLevelLen + 1, &inLevelLen);
1683 
1684     Drm_ErrCode ret1 = OH_MediaKeySystem_Create(judge_uuid(), &drmKeySystem);
1685     Drm_ErrCode ret2 = OH_MediaKeySystem_CreateMediaKeySession(drmKeySystem, &sessionLevel, &drmKeySession);
1686     MediaKeySession *session = nullptr;
1687     if (0 != strcmp(inSession, " ")) {
1688         session = drmKeySession;
1689     }
1690     DRM_ContentProtectionLevel *contentProtectionLevel = nullptr;
1691     DRM_ContentProtectionLevel outLevel = CONTENT_PROTECTION_LEVEL_UNKNOWN;
1692     if (0 != strcmp(inLevelSting, " ")) {
1693         contentProtectionLevel = &outLevel;
1694     }
1695     Drm_ErrCode ret3 = OH_MediaKeySession_GetContentProtectionLevel(session, contentProtectionLevel);
1696     if(ret3 == DRM_ERR_OK){
1697         ret3 = (outLevel == sessionLevel) ? DRM_ERR_OK : DRM_ERR_INVALID_VAL;
1698     }
1699     Drm_ErrCode ret4 = OH_MediaKeySession_Destroy(drmKeySession);
1700     Drm_ErrCode ret5 = OH_MediaKeySystem_Destroy(drmKeySystem);
1701     if (ret1 == DRM_ERR_OK && ret2 == DRM_ERR_OK && ret3 == DRM_ERR_OK && ret4 == DRM_ERR_OK && ret5 == DRM_ERR_OK) {
1702         ret = DRM_ERR_OK;
1703     } else {
1704         ret = DRM_ERR_INVALID_VAL;
1705     }
1706     napi_create_int32(env, ret, &result);
1707     return result;
1708 }
Test_MediaKeySession_RequireSecureDecoderModule(napi_env env,napi_callback_info info)1709 static napi_value Test_MediaKeySession_RequireSecureDecoderModule(napi_env env, napi_callback_info info) {
1710     napi_value result;
1711     size_t argc = 4;
1712     napi_value args[4] = {nullptr};
1713     size_t inSessionLen = 0;
1714     char *inSession = nullptr;
1715     size_t mimeTypeLen = 0;
1716     char *mimeType = nullptr;
1717     size_t statusLen = 0;
1718     char *statusString = nullptr;
1719     bool expectStatus = 0;
1720     Drm_ErrCode ret = DRM_ERR_INVALID_VAL;
1721     MediaKeySystem *drmKeySystem = nullptr;
1722     MediaKeySession *drmKeySession = nullptr;
1723 
1724     napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
1725     napi_get_value_string_utf8(env, args[0], nullptr, 0, &inSessionLen);
1726     inSession = new char[inSessionLen + 1];
1727     napi_get_value_string_utf8(env, args[0], inSession, inSessionLen + 1, &inSessionLen);
1728 
1729     napi_get_value_string_utf8(env, args[1], nullptr, 0, &mimeTypeLen);
1730     mimeType = new char[mimeTypeLen + 1];
1731     napi_get_value_string_utf8(env, args[1], mimeType, mimeTypeLen + 1, &mimeTypeLen);
1732 
1733     napi_get_value_string_utf8(env, args[2], nullptr, 0, &statusLen);
1734     statusString = new char[statusLen + 1];
1735     napi_get_value_string_utf8(env, args[2], statusString, statusLen + 1, &statusLen);
1736 
1737     napi_get_value_bool(env, args[3], &expectStatus);
1738 
1739     Drm_ErrCode ret1 = OH_MediaKeySystem_Create(judge_uuid(), &drmKeySystem);
1740     DRM_ContentProtectionLevel sessionLevel = CONTENT_PROTECTION_LEVEL_UNKNOWN;
1741     MediaKeySession *session_ptr = nullptr;
1742      if (0 == strcmp(inSession, "hw")) {
1743         sessionLevel = CONTENT_PROTECTION_LEVEL_HW_CRYPTO;
1744     } else if(0 == strcmp(inSession, "sw")) {
1745         sessionLevel = CONTENT_PROTECTION_LEVEL_SW_CRYPTO;
1746     } else if(0 == strcmp(inSession, "enhanced_hw")){
1747         sessionLevel = CONTENT_PROTECTION_LEVEL_ENHANCED_HW_CRYPTO;
1748     } else if(0 == strcmp(inSession, "max")){
1749         sessionLevel = CONTENT_PROTECTION_LEVEL_MAX;
1750     } else{
1751         sessionLevel = CONTENT_PROTECTION_LEVEL_UNKNOWN;
1752     }
1753     OH_LOG_DEBUG(LOG_APP, "testCL_RequireSecureDecoderModule session level = %{public}d", sessionLevel);
1754     Drm_ErrCode ret2 = OH_MediaKeySystem_CreateMediaKeySession(drmKeySystem,&sessionLevel,&drmKeySession);
1755     if (0 != strcmp(inSession, " ")) {
1756         session_ptr = drmKeySession;
1757     }
1758     char *mineType_ptr = nullptr;
1759     if (0 != strcmp(mimeType, " ")) {
1760         mineType_ptr = mimeType;
1761     }
1762     bool *status_ptr= nullptr;
1763     bool outStatus = 0;
1764     if (0 != strcmp(statusString, " ")) {
1765         status_ptr = &outStatus;
1766         OH_LOG_DEBUG(LOG_APP, "testCL_RequireSecureDecoderModule, outStatus = %{public}d", outStatus);
1767         OH_LOG_DEBUG(LOG_APP, "testCL_RequireSecureDecoderModule, expectStatus = %{public}d", expectStatus);
1768     }
1769 
1770     Drm_ErrCode ret3 = OH_MediaKeySession_RequireSecureDecoderModule(session_ptr, mineType_ptr, status_ptr);
1771     if(ret3 == DRM_ERR_OK){
1772         ret3 = (outStatus == expectStatus)?DRM_ERR_OK:DRM_ERR_INVALID_VAL;
1773     }
1774     Drm_ErrCode ret4 = OH_MediaKeySession_Destroy(drmKeySession);
1775     Drm_ErrCode ret5 = OH_MediaKeySystem_Destroy(drmKeySystem);
1776     if (ret1 == DRM_ERR_OK && ret2 == DRM_ERR_OK && ret3 == DRM_ERR_OK && ret4 == DRM_ERR_OK && ret5 == DRM_ERR_OK) {
1777         ret = DRM_ERR_OK;
1778     } else {
1779         ret = DRM_ERR_INVALID_VAL;
1780     }
1781     napi_create_int32(env, ret, &result);
1782     return result;
1783 }
1784 uint8_t Flag_EnterEventCallBack = 0;
1785 uint8_t Flag_EnterChangeCallBack = 0;
TestSessionEventCallBack(DRM_EventType eventType,uint8_t * info,int32_t infoLen,char * extra)1786 Drm_ErrCode TestSessionEventCallBack(DRM_EventType eventType, uint8_t *info, int32_t infoLen, char *extra) {
1787     OH_LOG_DEBUG(LOG_APP, "testCL_Enter SessionEventCallBack");
1788     Flag_EnterEventCallBack = 1;
1789     return DRM_ERR_OK;
1790 }
TestSessionKeyChangeCallback(DRM_KeysInfo * keysInfo,bool newKeysAvailable)1791 Drm_ErrCode TestSessionKeyChangeCallback(DRM_KeysInfo *keysInfo, bool newKeysAvailable){
1792     OH_LOG_DEBUG(LOG_APP, "testCL_Enter SessionKeyChangeCallback");
1793     Flag_EnterChangeCallBack = 1;
1794     return DRM_ERR_OK;
1795 }
Test_MediaKeySession_SetMediaKeySessionCallback(napi_env env,napi_callback_info info)1796 static napi_value Test_MediaKeySession_SetMediaKeySessionCallback(napi_env env, napi_callback_info info) {
1797     napi_value result;
1798     size_t argc = 2;
1799     napi_value args[2] = {nullptr};
1800     size_t inSessionLen = 0;
1801     char *inSession = nullptr;
1802     size_t callBackLen = 0;
1803     char *callBack = nullptr;
1804     Drm_ErrCode ret = DRM_ERR_INVALID_VAL;
1805     MediaKeySystem *drmKeySystem = nullptr;
1806     MediaKeySession *drmKeySession = nullptr;
1807     napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
1808     napi_get_value_string_utf8(env, args[0], nullptr, 0, &inSessionLen);
1809     inSession = new char[inSessionLen + 1];
1810     napi_get_value_string_utf8(env, args[0], inSession, inSessionLen + 1, &inSessionLen);
1811     napi_get_value_string_utf8(env, args[1], nullptr, 0, &callBackLen);
1812     callBack = new char[callBackLen + 1];
1813     napi_get_value_string_utf8(env, args[1], callBack, callBackLen + 1, &callBackLen);
1814     Drm_ErrCode ret1 = OH_MediaKeySystem_Create(judge_uuid(), &drmKeySystem);
1815     DRM_ContentProtectionLevel sessionLevel = CONTENT_PROTECTION_LEVEL_SW_CRYPTO;
1816     Drm_ErrCode ret2 = OH_MediaKeySystem_CreateMediaKeySession(drmKeySystem,&sessionLevel,&drmKeySession);
1817     MediaKeySession_Callback SessionCallBack;
1818     SessionCallBack.eventCallback = TestSessionEventCallBack;
1819     SessionCallBack.keyChangeCallback = TestSessionKeyChangeCallback;
1820     MediaKeySession *session_ptr = nullptr;
1821     MediaKeySession_Callback *callback_ptr = nullptr;
1822     if (0 != strcmp(inSession, " ")) {
1823         session_ptr = drmKeySession;
1824     }
1825     if (0 != strcmp(callBack, " ")) {
1826         callback_ptr = &SessionCallBack;
1827     }
1828     Drm_ErrCode ret3 = OH_MediaKeySession_SetMediaKeySessionCallback(session_ptr, callback_ptr);
1829     uint8_t keyData[DATA_BUFF_LEN] = {0};
1830     int32_t keyDataLen = DATA_BUFF_LEN;
1831     int resLice = getLicense(drmKeySession, LICENSE_ONLINE,keyData,&keyDataLen); // 申请license,触发回调
1832     if((ret3 == DRM_ERR_OK) && (resLice == 0)){
1833         ret3 = ((Flag_EnterEventCallBack == 1)||(Flag_EnterChangeCallBack == 1))?DRM_ERR_OK:DRM_ERR_INVALID_VAL;
1834     }
1835     Drm_ErrCode ret4 = OH_MediaKeySession_Destroy(drmKeySession);
1836     Drm_ErrCode ret5 = OH_MediaKeySystem_Destroy(drmKeySystem);
1837     Flag_EnterEventCallBack = 0;
1838     Flag_EnterChangeCallBack = 0;
1839     if (ret1 == DRM_ERR_OK && ret3 == DRM_ERR_OK && ret2 == DRM_ERR_OK && ret4 == DRM_ERR_OK && ret5 == DRM_ERR_OK) {
1840         ret = DRM_ERR_OK;
1841     } else {
1842         ret = DRM_ERR_INVALID_VAL;
1843     }
1844     napi_create_int32(env, ret, &result);
1845     return result;
1846 }
Test_MediaKeySession_GenerateOfflineReleaseRequest(napi_env env,napi_callback_info info)1847 static napi_value Test_MediaKeySession_GenerateOfflineReleaseRequest(napi_env env, napi_callback_info info) {
1848     napi_value result;
1849     size_t argc = 5;
1850     napi_value args[5] = {nullptr};
1851     size_t inSessionLen = 0;
1852     char *inSession = nullptr;
1853     size_t KeyIdStringLen = 0;
1854     char *KeyIdString = nullptr;
1855     int32_t offlineKeyIdLen = 0;
1856     size_t requestStringLen = 0;
1857     char *requestString = nullptr;
1858     size_t requestLenStringLen = 0;
1859     char *requestLenString = nullptr;
1860     Drm_ErrCode ret = DRM_ERR_INVALID_VAL;
1861     DRM_ContentProtectionLevel sessionLevel = CONTENT_PROTECTION_LEVEL_SW_CRYPTO;
1862     MediaKeySystem *drmKeySystem = nullptr;
1863     MediaKeySession *drmKeySession = nullptr;
1864 
1865     napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
1866     napi_get_value_string_utf8(env, args[0], nullptr, 0, &inSessionLen);
1867     inSession = new char[inSessionLen + 1];
1868     napi_get_value_string_utf8(env, args[0], inSession, inSessionLen + 1, &inSessionLen);
1869 
1870     napi_get_value_string_utf8(env, args[1], nullptr, 0, &KeyIdStringLen);
1871     KeyIdString = new char[KeyIdStringLen + 1];
1872     napi_get_value_string_utf8(env, args[1], KeyIdString, KeyIdStringLen + 1, &KeyIdStringLen);
1873 
1874     napi_get_value_int32(env, args[2], &offlineKeyIdLen);
1875 
1876     napi_get_value_string_utf8(env, args[3], nullptr, 0, &requestStringLen);
1877     requestString = new char[requestStringLen + 1];
1878     napi_get_value_string_utf8(env, args[3], requestString, requestStringLen + 1, &requestStringLen);
1879 
1880     napi_get_value_string_utf8(env, args[4], nullptr, 0, &requestLenStringLen);
1881     requestLenString = new char[requestLenStringLen + 1];
1882     napi_get_value_string_utf8(env, args[4], requestLenString, requestLenStringLen + 1, &requestLenStringLen);
1883 
1884     Drm_ErrCode ret1 = OH_MediaKeySystem_Create(judge_uuid(), &drmKeySystem);
1885     Drm_ErrCode ret2 = OH_MediaKeySystem_CreateMediaKeySession(drmKeySystem, &sessionLevel, &drmKeySession);
1886     MediaKeySession *session_ptr = nullptr;
1887     if (0 != strcmp(inSession, " ")) {
1888         session_ptr = drmKeySession;
1889     }
1890     uint8_t *KeyId_ptr = nullptr;
1891 	uint8_t keyData[DATA_BUFF_LEN] = {0};
1892 	int32_t keyDataLen = DATA_BUFF_LEN;
1893     if(strcmp(KeyIdString, "genKey") == 0){
1894         getLicense(drmKeySession, LICENSE_OFFLINE,keyData,&keyDataLen); // 确保生成离线license
1895         KeyId_ptr = keyData;
1896         if(offlineKeyIdLen > 0){
1897             offlineKeyIdLen = keyDataLen;
1898         }
1899     }else if (0 != strcmp(KeyIdString, " ")) {
1900         KeyId_ptr = (uint8_t *)malloc(KeyIdStringLen);
1901         splitStringToIntArray(KeyIdString,KeyId_ptr);
1902     }
1903     uint8_t data[DATA_BUFF_LEN]={0};
1904     uint8_t *releaseRequest_ptr = nullptr;
1905     if (0 != strcmp(requestString, " ")){
1906         releaseRequest_ptr = data;
1907     }
1908     int32_t releaseRequestLen = DATA_BUFF_LEN;
1909     int32_t *releaseRequestLen_ptr = nullptr;
1910     if (0 != strcmp(requestLenString, " ")){
1911         releaseRequestLen_ptr = &releaseRequestLen;
1912     }
1913     Drm_ErrCode ret3 = OH_MediaKeySession_GenerateOfflineReleaseRequest(session_ptr, KeyId_ptr, offlineKeyIdLen,releaseRequest_ptr, releaseRequestLen_ptr);
1914     Drm_ErrCode ret4 = OH_MediaKeySession_Destroy(drmKeySession);
1915     Drm_ErrCode ret5 = OH_MediaKeySystem_Destroy(drmKeySystem);
1916     if (ret1 == DRM_ERR_OK && ret2 == DRM_ERR_OK && ret3 == DRM_ERR_OK && ret4 == DRM_ERR_OK && ret5 == DRM_ERR_OK ){
1917         ret = DRM_ERR_OK;
1918     } else {
1919         ret = DRM_ERR_INVALID_VAL;
1920     }
1921     if((KeyId_ptr != nullptr) && (strcmp(KeyIdString, "genKey") != 0)){
1922         free(KeyId_ptr);
1923     }
1924     napi_create_int32(env,ret, &result);
1925     return result;
1926 }
Test_MediaKeySession_ProcessOfflineReleaseResponse(napi_env env,napi_callback_info info)1927 static napi_value Test_MediaKeySession_ProcessOfflineReleaseResponse(napi_env env, napi_callback_info info) {
1928     napi_value result;
1929     size_t argc = 5;
1930     napi_value args[5] = {nullptr};
1931     size_t inSessionLen = 0;
1932     char *inSession = nullptr;
1933     size_t KeyIdLen = 0;
1934     char *KeyId = nullptr;
1935     int32_t offlineKeyIdLen = 0;
1936     size_t releaseResponseLen = 0;
1937     char *releaseResponse = nullptr;
1938     int32_t responseLen = 0;
1939     Drm_ErrCode ret = DRM_ERR_INVALID_VAL;
1940     DRM_ContentProtectionLevel sessionLevel = CONTENT_PROTECTION_LEVEL_SW_CRYPTO;
1941     MediaKeySystem *drmKeySystem = nullptr;
1942     MediaKeySession *drmKeySession = nullptr;
1943 
1944     napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
1945     napi_get_value_string_utf8(env, args[0], nullptr, 0, &inSessionLen);
1946     inSession = new char[inSessionLen + 1];
1947     napi_get_value_string_utf8(env, args[0], inSession, inSessionLen + 1, &inSessionLen);
1948     napi_get_value_string_utf8(env, args[1], nullptr, 0, &KeyIdLen);
1949     KeyId = new char[KeyIdLen + 1];
1950     napi_get_value_string_utf8(env, args[1], KeyId, KeyIdLen + 1, &KeyIdLen);
1951     napi_get_value_int32(env, args[2], &offlineKeyIdLen);
1952     napi_get_value_string_utf8(env, args[3], nullptr, 0, &releaseResponseLen);
1953     releaseResponse = new char[releaseResponseLen + 1];
1954     napi_get_value_string_utf8(env, args[3], releaseResponse, releaseResponseLen + 1, &releaseResponseLen);
1955     napi_get_value_int32(env, args[4], &responseLen);
1956     Drm_ErrCode ret1 = OH_MediaKeySystem_Create(judge_uuid(), &drmKeySystem);
1957     Drm_ErrCode ret2 = OH_MediaKeySystem_CreateMediaKeySession(drmKeySystem, &sessionLevel, &drmKeySession);
1958     MediaKeySession *session_ptr = nullptr;
1959     if (0 != strcmp(inSession, " ")) {
1960         session_ptr = drmKeySession;
1961     }
1962     uint8_t *keyId_ptr = nullptr;
1963 	uint8_t keyData[DATA_BUFF_LEN] = {0};
1964 	int32_t keyDataLen = DATA_BUFF_LEN;
1965     if(strcmp(KeyId, "genKey") == 0){
1966         getLicense(drmKeySession, LICENSE_OFFLINE,keyData,&keyDataLen); // 确保生成离线license
1967         keyId_ptr = keyData;
1968         if(offlineKeyIdLen > 0){
1969             offlineKeyIdLen = keyDataLen;
1970         }
1971     }else if (0 != strcmp(KeyId, " ")) {
1972          keyId_ptr = (uint8_t *)malloc(KeyIdLen);
1973          splitStringToIntArray(KeyId,keyId_ptr);
1974      }
1975      uint8_t *releaseResponse_ptr = nullptr;
1976      if (0 != strcmp(releaseResponse, " ")) {
1977          releaseResponse_ptr = (uint8_t *)malloc(releaseResponseLen);
1978          splitStringToIntArray(releaseResponse, releaseResponse_ptr);
1979      }
1980     Drm_ErrCode ret3 = OH_MediaKeySession_ProcessOfflineReleaseResponse(session_ptr, keyId_ptr, offlineKeyIdLen,releaseResponse_ptr,responseLen);
1981     Drm_ErrCode ret4 = OH_MediaKeySession_Destroy(drmKeySession);
1982     Drm_ErrCode ret5 = OH_MediaKeySystem_Destroy(drmKeySystem);
1983     if (ret1 == DRM_ERR_OK && ret2 == DRM_ERR_OK && ret3 == DRM_ERR_OK && ret4 == DRM_ERR_OK && ret5 == DRM_ERR_OK ) {
1984         ret = DRM_ERR_OK;
1985     } else {
1986         ret = DRM_ERR_INVALID_VAL;
1987     }
1988     if(keyId_ptr != nullptr && strcmp(KeyId, "genKey") != 0){
1989         free(keyId_ptr);
1990     }
1991     if(releaseResponse_ptr != nullptr){
1992         free(releaseResponse_ptr);
1993     }
1994     napi_create_int32(env, ret, &result);
1995     return result;
1996 }
Test_MediaKeySession_RestoreOfflineMediaKeys(napi_env env,napi_callback_info info)1997 static napi_value Test_MediaKeySession_RestoreOfflineMediaKeys(napi_env env, napi_callback_info info) {
1998     napi_value result;
1999     size_t argc = 3;
2000     napi_value args[3] = {nullptr};
2001     size_t inSessionLen = 0;
2002     char *inSession = nullptr;
2003     size_t KeyIdLen = 0;
2004     char *KeyId = nullptr;
2005     int32_t offlineKeyIdLen = 0;
2006     Drm_ErrCode ret = DRM_ERR_INVALID_VAL;
2007     DRM_ContentProtectionLevel sessionLevel = CONTENT_PROTECTION_LEVEL_SW_CRYPTO;
2008     MediaKeySystem *drmKeySystem = nullptr;
2009     MediaKeySession *drmKeySession = nullptr;
2010 
2011     napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
2012     napi_get_value_string_utf8(env, args[0], nullptr, 0, &inSessionLen);
2013     inSession = new char[inSessionLen + 1];
2014     napi_get_value_string_utf8(env, args[0], inSession, inSessionLen + 1, &inSessionLen);
2015     napi_get_value_string_utf8(env, args[1], nullptr, 0, &KeyIdLen);
2016     KeyId = new char[KeyIdLen + 1];
2017     napi_get_value_string_utf8(env, args[1], KeyId, KeyIdLen + 1, &KeyIdLen);
2018     napi_get_value_int32(env, args[2], &offlineKeyIdLen);
2019     Drm_ErrCode ret1 = OH_MediaKeySystem_Create(judge_uuid(), &drmKeySystem);
2020     Drm_ErrCode ret2 = OH_MediaKeySystem_CreateMediaKeySession(drmKeySystem, &sessionLevel, &drmKeySession);
2021     uint8_t *keyId_ptr = nullptr;
2022 	uint8_t keyData[DATA_BUFF_LEN] = {0};
2023 	int32_t keyDataLen = DATA_BUFF_LEN;
2024     if(strcmp(KeyId, "getKey") == 0){
2025         getLicense(drmKeySession, LICENSE_OFFLINE,keyData,&keyDataLen); // 确保生成离线license
2026         keyId_ptr = keyData;
2027         if (offlineKeyIdLen > 0) {
2028             offlineKeyIdLen = keyDataLen;
2029         }
2030    }else if (0 != strcmp(KeyId, " ")) {
2031         keyId_ptr = (uint8_t *)malloc(KeyIdLen);
2032         splitStringToIntArray(KeyId,keyId_ptr);
2033     }else;
2034     MediaKeySession *session_ptr = nullptr;
2035     if (0 != strcmp(inSession, " ")) {
2036         session_ptr = drmKeySession;
2037     }
2038     Drm_ErrCode ret3 = OH_MediaKeySession_RestoreOfflineMediaKeys(session_ptr, keyId_ptr, offlineKeyIdLen);
2039     Drm_ErrCode ret4 = OH_MediaKeySession_Destroy(drmKeySession);
2040     Drm_ErrCode ret5 = OH_MediaKeySystem_Destroy(drmKeySystem);
2041     if (ret1 == DRM_ERR_OK && ret2 == DRM_ERR_OK && ret3 == DRM_ERR_OK && ret4 == DRM_ERR_OK && ret5 == DRM_ERR_OK) {
2042         ret = DRM_ERR_OK;
2043     } else {
2044         ret = DRM_ERR_INVALID_VAL;
2045     }
2046     if(keyId_ptr != nullptr && strcmp(KeyId, "getKey") != 0){
2047         free(keyId_ptr);
2048     }
2049     napi_create_int32(env, ret, &result);
2050     return result;
2051 }
2052 // add cases
Test_CreateMediaKeySession_multi(napi_env env,napi_callback_info info)2053 static napi_value Test_CreateMediaKeySession_multi(napi_env env, napi_callback_info info) {
2054     napi_value result;
2055     size_t argc = 2;
2056     napi_value args[2] = {nullptr};
2057     int32_t sessionNum = 0;
2058     Drm_ErrCode ret = DRM_ERR_INVALID_VAL;
2059     Drm_ErrCode ret2 = DRM_ERR_INVALID_VAL;
2060     Drm_ErrCode ret4 = DRM_ERR_INVALID_VAL;
2061     MediaKeySystem *drmKeySystem = nullptr;
2062     DRM_ContentProtectionLevel level = CONTENT_PROTECTION_LEVEL_SW_CRYPTO;
2063     napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
2064     napi_get_value_int32(env, args[0], &sessionNum);
2065     MediaKeySession *drmKeySession[100] = {nullptr};
2066     Drm_ErrCode ret1 = OH_MediaKeySystem_Create(judge_uuid(), &drmKeySystem);
2067     uint32_t count_create = 0;
2068     for (int i = 0; i < sessionNum; i++) {
2069         ret2 = OH_MediaKeySystem_CreateMediaKeySession(drmKeySystem, &level, &drmKeySession[i]);
2070         CHECK_GOTO(ret2 != DRM_ERR_OK, EXIT1);
2071         count_create++;
2072     }
2073 EXIT1:
2074     for (int i = 0; i < (count_create); i++) {
2075         ret4 = OH_MediaKeySession_Destroy(drmKeySession[i]);
2076     }
2077     Drm_ErrCode ret3 = OH_MediaKeySystem_Destroy(drmKeySystem);
2078     if (ret1 == DRM_ERR_OK && (count_create == sessionNum) && ret3 == DRM_ERR_OK && ret4 == DRM_ERR_OK) {
2079         ret = DRM_ERR_OK;
2080     } else {
2081         ret = DRM_ERR_INVALID_VAL;
2082     }
2083     napi_create_int32(env, ret, &result);
2084     return result;
2085 }
Test_MediaKeySession_ProcessOfflineReleaseResponse_multi(napi_env env,napi_callback_info info)2086 static napi_value Test_MediaKeySession_ProcessOfflineReleaseResponse_multi(napi_env env, napi_callback_info info) {
2087     napi_value result;
2088     size_t argc = 5;
2089     napi_value args[5] = {nullptr};
2090     int32_t releaseNum = 0;
2091 
2092     Drm_ErrCode ret = DRM_ERR_INVALID_VAL;
2093     DRM_ContentProtectionLevel sessionLevel = CONTENT_PROTECTION_LEVEL_SW_CRYPTO;
2094     MediaKeySystem *drmKeySystem = nullptr;
2095     MediaKeySession *drmKeySession = nullptr;
2096 
2097     napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
2098     napi_get_value_int32(env, args[0], &releaseNum);
2099 
2100     Drm_ErrCode ret1 = OH_MediaKeySystem_Create(judge_uuid(), &drmKeySystem);
2101     Drm_ErrCode ret2 = OH_MediaKeySystem_CreateMediaKeySession(drmKeySystem, &sessionLevel, &drmKeySession);
2102     uint8_t keyData[DATA_BUFF_LEN] = {0};
2103     int32_t keyDataLen = DATA_BUFF_LEN;
2104     getLicense(drmKeySession, LICENSE_OFFLINE, keyData, &keyDataLen); // 确保生成离线license
2105     Drm_ErrCode ret3 = DRM_ERR_INVALID_VAL;
2106 
2107     if (strcmp(judge_uuid(), CLEAR_PLAY) == 0) {
2108         uint8_t releaseResponseData[50] = OFFLINERESPONSE;
2109         int32_t responseLen = 50;
2110         for (int i = 0; i < releaseNum; i++) {
2111             ret3 = OH_MediaKeySession_ProcessOfflineReleaseResponse(drmKeySession, keyData, keyDataLen,
2112                                                                     releaseResponseData, responseLen);
2113         }
2114     }else{
2115         ret3 = DRM_ERR_OK;
2116     }
2117     Drm_ErrCode ret5 = OH_MediaKeySession_Destroy(drmKeySession);
2118     Drm_ErrCode ret6 = OH_MediaKeySystem_Destroy(drmKeySystem);
2119     if (ret1 == DRM_ERR_OK && ret2 == DRM_ERR_OK && ret3 == DRM_ERR_OK && ret5 == DRM_ERR_OK && ret6 == DRM_ERR_OK) {
2120         ret = DRM_ERR_OK;
2121     } else {
2122         ret = DRM_ERR_INVALID_VAL;
2123     }
2124     napi_create_int32(env, ret, &result);
2125     return result;
2126 }
Test_MediaKeySystem_SetConfigurationString_multi(napi_env env,napi_callback_info info)2127 static napi_value Test_MediaKeySystem_SetConfigurationString_multi(napi_env env, napi_callback_info info) {
2128     napi_value result;
2129     Drm_ErrCode ret = DRM_ERR_INVALID_VAL;
2130     MediaKeySystem *drmKeySystem = nullptr;
2131     Drm_ErrCode ret1 = OH_MediaKeySystem_Create(judge_uuid(), &drmKeySystem);
2132     Drm_ErrCode ret2 = OH_MediaKeySystem_SetConfigurationString(drmKeySystem, "version", "4500000");
2133     Drm_ErrCode ret3 = OH_MediaKeySystem_SetConfigurationString(drmKeySystem, "version", "2.2");
2134 
2135     char outBuff[20] = {0};
2136     int32_t outBuffLen = 20;
2137     Drm_ErrCode ret4 = DRM_ERR_INVALID_VAL;
2138     if (ret2 == DRM_ERR_OK && ret3 == DRM_ERR_OK) {
2139         ret4 = OH_MediaKeySystem_GetConfigurationString(drmKeySystem, "version", outBuff, outBuffLen);
2140         if (strncmp(outBuff, "2.2", outBuffLen) == 0) {
2141             ret4 = DRM_ERR_OK;
2142         } else {
2143             ret4 = DRM_ERR_INVALID_VAL;
2144         }
2145     }
2146     Drm_ErrCode ret5 = OH_MediaKeySystem_Destroy(drmKeySystem);
2147     if (ret1 == DRM_ERR_OK && ret2 == DRM_ERR_OK && ret5 == DRM_ERR_OK && ret4 == DRM_ERR_OK) {
2148         ret = DRM_ERR_OK;
2149     } else {
2150         ret = DRM_ERR_INVALID_VAL;
2151     }
2152     napi_create_int32(env, ret3, &result);
2153     return result;
2154 }
2155 
2156 // add api
2157 uint8_t Flag_SetCallback =0;
testMediaSystemSetCallBack(MediaKeySystem * mediaKeySystem,DRM_EventType eventType,uint8_t * drmInfo,int32_t infoLen,char * extra)2158 Drm_ErrCode testMediaSystemSetCallBack(MediaKeySystem *mediaKeySystem, DRM_EventType eventType,
2159         uint8_t *drmInfo, int32_t infoLen, char *extra){
2160     OH_LOG_DEBUG(LOG_APP, "testCL_testMediaSystemSetCallBack ok");
2161     Flag_SetCallback = 1;
2162     return DRM_ERR_OK;
2163 }
Test_MediaKeySystem_SetCallback(napi_env env,napi_callback_info info)2164 static napi_value Test_MediaKeySystem_SetCallback(napi_env env, napi_callback_info info) {
2165     napi_value result;
2166     size_t argc = 2;
2167     napi_value args[2] = {nullptr};
2168     size_t inSystemLen = 0;
2169     char *inSystem = nullptr;
2170     size_t callBackLen = 0;
2171     char *callBack = nullptr;
2172     Drm_ErrCode ret = DRM_ERR_INVALID_VAL;
2173     MediaKeySystem *drmKeySystem = nullptr;
2174     MediaKeySession *drmKeySession = nullptr;
2175     DRM_ContentProtectionLevel level = CONTENT_PROTECTION_LEVEL_SW_CRYPTO;
2176 
2177     napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
2178     napi_get_value_string_utf8(env, args[0], nullptr, 0, &inSystemLen);
2179     inSystem = new char[inSystemLen + 1];
2180     napi_get_value_string_utf8(env, args[0], inSystem, inSystemLen + 1, &inSystemLen);
2181     napi_get_value_string_utf8(env, args[1], nullptr, 0, &callBackLen);
2182     callBack = new char[callBackLen + 1];
2183     napi_get_value_string_utf8(env, args[1], callBack, callBackLen + 1, &callBackLen);
2184 
2185     Drm_ErrCode ret1 = OH_MediaKeySystem_Create(judge_uuid(), &drmKeySystem);
2186     MediaKeySystem *sys_ptr = nullptr;
2187     OH_MediaKeySystem_Callback callback_ptr = nullptr;
2188     if (0 != strcmp(inSystem, " ")) {
2189         sys_ptr = drmKeySystem;
2190     }
2191     if (0 != strcmp(callBack, " ")) {
2192         callback_ptr = &testMediaSystemSetCallBack;
2193     }
2194     Drm_ErrCode ret2 = OH_MediaKeySystem_SetCallback(sys_ptr, callback_ptr);  // DRM_ERR_OK; //  SDK12
2195     CHECK_PRINTF((ret2 != DRM_ERR_OK), ret2);
2196     // Trigger callback
2197     if(strcmp(judge_uuid(), CLEAR_PLAY) == 0){ // clearpaly直接申请设备证书,触发回调
2198         int retCert = getCertificate(drmKeySystem);
2199         CHECK_PRINTF((retCert != 0), retCert);
2200 
2201         Drm_ErrCode ret4 = OH_MediaKeySystem_CreateMediaKeySession(drmKeySystem, &level, &drmKeySession);
2202         if (ret4 == DRM_ERR_OK) {
2203             Drm_ErrCode ret5 = OH_MediaKeySession_Destroy(drmKeySession);
2204             CHECK_PRINTF((ret5 != DRM_ERR_OK), ret5);
2205         }
2206         if((ret2 == DRM_ERR_OK) && (callback_ptr !=nullptr)){
2207             ret2 = (Flag_SetCallback == 1) ? DRM_ERR_OK : DRM_ERR_INVALID_VAL;
2208         }
2209     } else{ // wiseplay手动删除证书,创建session失败,触发回调
2210         // triggering events: no provision then createSession failed
2211         DRM_CertificateStatus certStatus = CERT_STATUS_UNAVAILABLE;
2212         Drm_ErrCode ret3 = OH_MediaKeySystem_GetCertificateStatus(drmKeySystem,&certStatus);
2213         if(ret3 == DRM_ERR_OK && certStatus != CERT_STATUS_PROVISIONED){
2214             Drm_ErrCode ret4 = OH_MediaKeySystem_CreateMediaKeySession(drmKeySystem,&level,&drmKeySession);
2215             if (ret4 == DRM_ERR_OK) {
2216                  Drm_ErrCode ret5 = OH_MediaKeySession_Destroy(drmKeySession);
2217                  CHECK_PRINTF((ret5 != DRM_ERR_OK), ret5);
2218             }
2219             if((ret2 == DRM_ERR_OK) && (callback_ptr !=nullptr)){
2220                 ret2 = (Flag_SetCallback == 1) ? DRM_ERR_OK : DRM_ERR_INVALID_VAL;
2221             }
2222         }
2223     }
2224     Drm_ErrCode ret6 = OH_MediaKeySystem_Destroy(drmKeySystem);
2225     Flag_SetCallback = 0; // clear flags
2226     if (ret1 == DRM_ERR_OK && ret2 == DRM_ERR_OK && ret6 == DRM_ERR_OK ){
2227         ret = DRM_ERR_OK;
2228     } else {
2229         ret = DRM_ERR_INVALID_VAL;
2230     }
2231     napi_create_int32(env, ret, &result);
2232     return result;
2233 }
2234 // session callBack
2235 uint8_t Flag_SetSesEventCallback = 0;
2236 uint8_t Flag_SetSesChangeCallback = 0;
sessoinEventCallBackTest(MediaKeySession * mediaKeySessoin,DRM_EventType eventType,uint8_t * info,int32_t infoLen,char * extra)2237 Drm_ErrCode sessoinEventCallBackTest(MediaKeySession *mediaKeySessoin, DRM_EventType eventType,
2238     uint8_t *info, int32_t infoLen, char *extra)
2239 {
2240     OH_LOG_DEBUG(LOG_APP, "testCL_sessoinEventCallBackTest ok;eventType = %{public}d, infoLen = %{public}d", eventType, infoLen);
2241     Flag_SetSesEventCallback = 1;
2242     return DRM_ERR_OK;
2243 }
sessoinKeyChangeCallBackTest(MediaKeySession * mediaKeySessoin,DRM_KeysInfo * keysInfo,bool hasNewGoodKeys)2244 Drm_ErrCode sessoinKeyChangeCallBackTest(MediaKeySession *mediaKeySessoin, DRM_KeysInfo *keysInfo, bool hasNewGoodKeys)
2245 {
2246     OH_LOG_DEBUG(LOG_APP, "testCL_sessionKeyChangeCallBackTest ok;hasNewGoodKeys = %{public}d", hasNewGoodKeys);
2247     Flag_SetSesChangeCallback = 1;
2248     return DRM_ERR_OK;
2249 }
Test_MediaKeySession_SetCallback(napi_env env,napi_callback_info info)2250 static napi_value Test_MediaKeySession_SetCallback(napi_env env, napi_callback_info info) {
2251     napi_value result;
2252     size_t argc = 2;
2253     napi_value args[2] = {nullptr};
2254     size_t inSystemLen = 0;
2255     char *inSystem = nullptr;
2256     size_t callBackLen = 0;
2257     char *callBack = nullptr;
2258     Drm_ErrCode ret = DRM_ERR_INVALID_VAL;
2259     MediaKeySystem *drmKeySystem = nullptr;
2260     MediaKeySession *drmKeySession = nullptr;
2261     DRM_ContentProtectionLevel sessionLevel = CONTENT_PROTECTION_LEVEL_SW_CRYPTO;
2262     napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
2263     napi_get_value_string_utf8(env, args[0], nullptr, 0, &inSystemLen);
2264     inSystem = new char[inSystemLen + 1];
2265     napi_get_value_string_utf8(env, args[0], inSystem, inSystemLen + 1, &inSystemLen);
2266     napi_get_value_string_utf8(env, args[1], nullptr, 0, &callBackLen);
2267     callBack = new char[callBackLen + 1];
2268     napi_get_value_string_utf8(env, args[1], callBack, callBackLen + 1, &callBackLen);
2269 
2270     Drm_ErrCode ret1 = OH_MediaKeySystem_Create(judge_uuid(), &drmKeySystem);
2271     Drm_ErrCode ret2 = OH_MediaKeySystem_CreateMediaKeySession(drmKeySystem,&sessionLevel,&drmKeySession);
2272 
2273     OH_MediaKeySession_Callback testCallback;
2274     memset(&testCallback, 0, sizeof(OH_MediaKeySession_Callback));
2275     testCallback.eventCallback = &sessoinEventCallBackTest;
2276     testCallback.keyChangeCallback = &sessoinKeyChangeCallBackTest;
2277     MediaKeySession *session_ptr = nullptr;
2278     OH_MediaKeySession_Callback *callback_ptr = nullptr;
2279     if (0 != strcmp(inSystem, " ")) {
2280         session_ptr = drmKeySession;
2281     }
2282     if (0 != strcmp(callBack, " ")) {
2283         callback_ptr = &testCallback;
2284     }
2285     Drm_ErrCode ret4 = OH_MediaKeySession_SetCallback(session_ptr, callback_ptr);
2286     // triggering events: get license
2287     uint8_t keyData[DATA_BUFF_LEN] = {0};
2288     int32_t keyDataLen = DATA_BUFF_LEN;
2289     int resLice = getLicense(drmKeySession, LICENSE_OFFLINE, keyData, &keyDataLen);
2290     if ((ret4 == DRM_ERR_OK) && (resLice == 0) && (callback_ptr != nullptr)) {
2291          ret4 = ((Flag_SetSesEventCallback == 1) || (Flag_SetSesChangeCallback == 1)) ? DRM_ERR_OK : DRM_ERR_INVALID_VAL;
2292     }
2293     Drm_ErrCode ret5 = OH_MediaKeySession_Destroy(drmKeySession);
2294     Drm_ErrCode ret6 = OH_MediaKeySystem_Destroy(drmKeySystem);
2295     Flag_SetSesChangeCallback = 0;
2296     Flag_SetSesEventCallback = 0;
2297     if (ret1 == DRM_ERR_OK && ret2 == DRM_ERR_OK && ret4 == DRM_ERR_OK && ret5 == DRM_ERR_OK && ret6 == DRM_ERR_OK ) {
2298         ret = DRM_ERR_OK;
2299     } else {
2300         ret = DRM_ERR_INVALID_VAL;
2301     }
2302     napi_create_int32(env, ret, &result);
2303     return result;
2304 }
Test_MediaKeySystem_GetMediaKeySystems(napi_env env,napi_callback_info info)2305 static napi_value Test_MediaKeySystem_GetMediaKeySystems(napi_env env, napi_callback_info info) {
2306     napi_value result;
2307     size_t argc = 2;
2308     napi_value args[2] = {nullptr};
2309     size_t inSystemLen = 0;
2310     char *inSystem = nullptr;
2311     size_t callBackLen = 0;
2312     char *callBack = nullptr;
2313 
2314     napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
2315     napi_get_value_string_utf8(env, args[0], nullptr, 0, &inSystemLen);
2316     inSystem = new char[inSystemLen + 1];
2317     napi_get_value_string_utf8(env, args[0], inSystem, inSystemLen + 1, &inSystemLen);
2318     napi_get_value_string_utf8(env, args[1], nullptr, 0, &callBackLen);
2319     callBack = new char[callBackLen + 1];
2320     napi_get_value_string_utf8(env, args[1], callBack, callBackLen + 1, &callBackLen);
2321 
2322     uint32_t count = DRM_PLUGIN_COUNT;
2323     DRM_MediaKeySystemDescription descriptions[DRM_PLUGIN_COUNT];
2324     memset(descriptions, 0, sizeof(descriptions));
2325     DRM_MediaKeySystemDescription *des_ptr = nullptr;
2326     uint32_t *count_ptr = nullptr;
2327     if (0 != strcmp(inSystem, " ")) {
2328         des_ptr = descriptions;
2329     }
2330     if (0 != strcmp(callBack, " ")) {
2331         count_ptr = &count;
2332     }
2333     Drm_ErrCode ret = OH_MediaKeySystem_GetMediaKeySystems(des_ptr, count_ptr);
2334     if(ret == DRM_ERR_OK){
2335         for (int i = 0; i < count; i++) {
2336             OH_LOG_DEBUG(LOG_APP, "testCL_GetMediaKeySystems, name[%{public}d] is %{public}s", i,
2337                          descriptions[i].name);
2338             OH_LOG_DEBUG(LOG_APP, "testCL_GetMediaKeySystems, uuid[%{public}d] is %{public}s", i,
2339                          descriptions[i].uuid);
2340         }
2341     }
2342     napi_create_int32(env, ret, &result);
2343     return result;
2344 }
2345 
2346 EXTERN_C_START
Init(napi_env env,napi_value exports)2347 static napi_value Init(napi_env env, napi_value exports)
2348 {
2349     napi_property_descriptor desc[] = {
2350         {"GetOfflineMediaKeyIds", nullptr, GetOfflineMediaKeyIds, nullptr, nullptr, nullptr, napi_default, nullptr},
2351         {"GetCertificateStatus", nullptr, GetCertificateStatus, nullptr, nullptr, nullptr, napi_default, nullptr},
2352         {"Test_KeySystemSupported", nullptr, Test_KeySystemSupported, nullptr, nullptr, nullptr, napi_default, nullptr},
2353         {"Test_KeySystemSupported2", nullptr, Test_KeySystemSupported2, nullptr, nullptr, nullptr, napi_default,
2354          nullptr},
2355         {"Test_KeySystemSupported3", nullptr, Test_KeySystemSupported3, nullptr, nullptr, nullptr, napi_default,
2356          nullptr},
2357         {"Test_MediaKeySystem_Create", nullptr, Test_MediaKeySystem_Create, nullptr, nullptr, nullptr, napi_default,
2358          nullptr},
2359         {"Test_MediaKeySystem_SetConfigurationString", nullptr, Test_MediaKeySystem_SetConfigurationString, nullptr,
2360          nullptr, nullptr, napi_default, nullptr},
2361         {"Test_MediaKeySystem_GetConfigurationString", nullptr, Test_MediaKeySystem_GetConfigurationString, nullptr,
2362          nullptr, nullptr, napi_default, nullptr},
2363         {"Test_MediaKeySystem_SetConfigurationByteArray", nullptr, Test_MediaKeySystem_SetConfigurationByteArray,
2364          nullptr, nullptr, nullptr, napi_default, nullptr},
2365         {"Test_MediaKeySystem_GetConfigurationByteArray", nullptr, Test_MediaKeySystem_GetConfigurationByteArray,
2366          nullptr, nullptr, nullptr, napi_default, nullptr},
2367         {"Test_MediaKeySystem_SetMediaKeySystemCallback", nullptr, Test_MediaKeySystem_SetMediaKeySystemCallback,
2368          nullptr, nullptr, nullptr, napi_default, nullptr},
2369         {"Test_MediaKeySystem_GetStatistics", nullptr, Test_MediaKeySystem_GetStatistics, nullptr, nullptr, nullptr,
2370          napi_default, nullptr},
2371         {"Test_MediaKeySystem_GetMaxContentProtectionLevel", nullptr, Test_MediaKeySystem_GetMaxContentProtectionLevel,
2372          nullptr, nullptr, nullptr, napi_default, nullptr},
2373         {"Test_MediaKeySystem_GenerateKeySystemRequest", nullptr, Test_MediaKeySystem_GenerateKeySystemRequest, nullptr,
2374          nullptr, nullptr, napi_default, nullptr},
2375         {"Test_MediaKeySystem_ProcessKeySystemResponse", nullptr, Test_MediaKeySystem_ProcessKeySystemResponse, nullptr,
2376          nullptr, nullptr, napi_default, nullptr},
2377         {"Test_MediaKeySystem_GetOfflineMediaKeyIds", nullptr, Test_MediaKeySystem_GetOfflineMediaKeyIds, nullptr,
2378          nullptr, nullptr, napi_default, nullptr},
2379         {"Test_MediaKeySystem_Destroy", nullptr, Test_MediaKeySystem_Destroy, nullptr, nullptr, nullptr, napi_default,
2380          nullptr},
2381         {"Test_CreateMediaKeySession", nullptr, Test_CreateMediaKeySession, nullptr, nullptr, nullptr, napi_default,
2382          nullptr},
2383         {"Test_MediaKeySystem_GetCertificateStatus", nullptr, Test_MediaKeySystem_GetCertificateStatus, nullptr,
2384          nullptr, nullptr, napi_default, nullptr},
2385         {"Test_MediaKeySession_GenerateMediaKeyRequest", nullptr, Test_MediaKeySession_GenerateMediaKeyRequest, nullptr,
2386          nullptr, nullptr, napi_default, nullptr},
2387         {"Test_MediaKeySession_ProcessMediaKeyResponse", nullptr, Test_MediaKeySession_ProcessMediaKeyResponse, nullptr,
2388          nullptr, nullptr, napi_default, nullptr},
2389         {"Test_MediaKeySession_CheckMediaKeyStatus", nullptr, Test_MediaKeySession_CheckMediaKeyStatus, nullptr,
2390          nullptr, nullptr, napi_default, nullptr},
2391         {"Test_MediaKeySession_ClearMediaKeys", nullptr, Test_MediaKeySession_ClearMediaKeys, nullptr, nullptr, nullptr,
2392          napi_default, nullptr},
2393         {"Test_MediaKeySession_GenerateOfflineReleaseRequest", nullptr,
2394          Test_MediaKeySession_GenerateOfflineReleaseRequest, nullptr, nullptr, nullptr, napi_default, nullptr},
2395         {"Test_MediaKeySession_ProcessOfflineReleaseResponse", nullptr,
2396          Test_MediaKeySession_ProcessOfflineReleaseResponse, nullptr, nullptr, nullptr, napi_default, nullptr},
2397         {"Test_MediaKeySession_RestoreOfflineMediaKeys", nullptr, Test_MediaKeySession_RestoreOfflineMediaKeys, nullptr,
2398          nullptr, nullptr, napi_default, nullptr},
2399         {"Test_MediaKeySession_GetContentProtectionLevel", nullptr, Test_MediaKeySession_GetContentProtectionLevel,
2400          nullptr, nullptr, nullptr, napi_default, nullptr},
2401         {"Test_MediaKeySession_RequireSecureDecoderModule", nullptr, Test_MediaKeySession_RequireSecureDecoderModule,
2402          nullptr, nullptr, nullptr, napi_default, nullptr},
2403         {"Test_MediaKeySession_SetMediaKeySessionCallback", nullptr, Test_MediaKeySession_SetMediaKeySessionCallback,
2404          nullptr, nullptr, nullptr, napi_default, nullptr},
2405         {"Test_MediaKeySystem_GetOfflineMediaKeyStatus", nullptr, Test_MediaKeySystem_GetOfflineMediaKeyStatus, nullptr,
2406          nullptr, nullptr, napi_default, nullptr},
2407         {"Test_MediaKeySystem_ClearOfflineMediaKeys", nullptr, Test_MediaKeySystem_ClearOfflineMediaKeys, nullptr,
2408          nullptr, nullptr, napi_default, nullptr},
2409         {"Test_CreateMediaKeySession_multi", nullptr, Test_CreateMediaKeySession_multi,
2410          nullptr, nullptr, nullptr, napi_default, nullptr},
2411         {"Test_MediaKeySession_ProcessOfflineReleaseResponse_multi", nullptr, Test_MediaKeySession_ProcessOfflineReleaseResponse_multi, nullptr,
2412          nullptr, nullptr, napi_default, nullptr},
2413         {"Test_MediaKeySystem_SetConfigurationString_multi", nullptr, Test_MediaKeySystem_SetConfigurationString_multi, nullptr,
2414          nullptr, nullptr, napi_default, nullptr},
2415         {"Test_MediaKeySystem_SetCallback", nullptr, Test_MediaKeySystem_SetCallback, nullptr,
2416          nullptr, nullptr, napi_default, nullptr},
2417         {"Test_MediaKeySession_SetCallback", nullptr, Test_MediaKeySession_SetCallback, nullptr,
2418          nullptr, nullptr, napi_default, nullptr},
2419         {"Test_MediaKeySystem_GetMediaKeySystems", nullptr, Test_MediaKeySystem_GetMediaKeySystems, nullptr,
2420          nullptr, nullptr, napi_default, nullptr},
2421     };
2422     napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc);
2423     return exports;
2424 }
2425 EXTERN_C_END
2426 
2427 static napi_module demoModule = {
2428     .nm_version = 1,
2429     .nm_flags = 0,
2430     .nm_filename = nullptr,
2431     .nm_register_func = Init,
2432     .nm_modname = "entry",
2433     .nm_priv = ((void*)0),
2434     .reserved = { 0 },
2435 };
2436 
RegisterEntryModule(void)2437 extern "C" __attribute__((constructor)) void RegisterEntryModule(void)
2438 {
2439     napi_module_register(&demoModule);
2440 }
2441