• 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 <cstring>
18 #include <unistd.h>
19 #include <cstdio>
20 #include <cstdlib>
21 #include <ctime>
22 #include <unistd.h>
23 #include "multimedia/drm_framework/native_mediakeysystem.h"
24 #include "multimedia/drm_framework/native_drm_common.h"
25 #include "multimedia/drm_framework/native_drm_err.h"
26 #include "multimedia/drm_framework/native_mediakeysession.h"
27 #include "multimedia/player_framework/avplayer.h"
28 #include "multimedia/player_framework/avplayer_base.h"
29 #include "multimedia/player_framework/native_averrors.h"
30 #include "multimedia/player_framework/native_avdemuxer.h"
31 #include "multimedia/player_framework/native_avsource.h"
32 #include "multimedia/player_framework/native_avcodec_base.h"
33 #include "multimedia/player_framework/native_avbuffer.h"
34 #include "multimedia/player_framework/native_avcodec_audiocodec.h"
35 #include "multimedia/player_framework/native_avcodec_videodecoder.h"
36 #include <multimedia/player_framework/native_avcapability.h>
37 
38 #include <sys/stat.h>
39 #include <fcntl.h>
40 #include <hilog/log.h>
41 // hilog必加的宏,不加无法打印
42 #undef LOG_DOMAIN
43 #undef LOG_TAG
44 #define LOG_DOMAIN 0x3200 // 全局domain宏,标识业务领域
45 #define LOG_TAG "MY_DRM"  // 全局tag宏,标识模块日志tag
46 
47 #define CURLEASYGETINFO 200
48 #define HTTP_TIMEOUT 10
49 
50 #define PATH "/data/storage/el2/base/haps/entry/files/ChinaDRMLab_Promo_210525-264-30-1080P-10000K.sm4c.ts"
51 #define IS_CLEARPLAY "com.clearplay.drm"
52 #define IS_WISEPLAY "com.wiseplay.drm"
53 #define DATA_UUID_LEN 16
54 #define DATA_BUFF_LEN 12288
55 #define REQUESTINFODATA                                                                                                \
56     {                                                                                                                  \
57         0x00, 0x00, 0x00, 0x8B, 0x70, 0x73, 0x73, 0x68, 0x00, 0x00, 0x00, 0x00, 0x3D, 0x5E, 0x6D, 0x35, 0x9B, 0x9A,    \
58             0x41, 0xE8, 0xB8, 0x43, 0xDD, 0x3C, 0x6E, 0x72, 0xC4, 0x2C, 0x00, 0x00, 0x00, 0x6B, 0x7B, 0x22, 0x76,      \
59             0x65, 0x72, 0x73, 0x69, 0x6F, 0x6E, 0x22, 0x3A, 0x22, 0x56, 0x31, 0x2E, 0x30, 0x22, 0x2C, 0x22, 0x63,      \
60             0x6F, 0x6E, 0x74, 0x65, 0x6E, 0x74, 0x49, 0x44, 0x22, 0x3A, 0x22, 0x64, 0x48, 0x4D, 0x74, 0x4D, 0x6A,      \
61             0x59, 0x30, 0x4C, 0x54, 0x45, 0x77, 0x4F, 0x44, 0x41, 0x74, 0x59, 0x57, 0x56, 0x7A, 0x22, 0x2C, 0x22,      \
62             0x6B, 0x69, 0x64, 0x73, 0x22, 0x3A, 0x5B, 0x22, 0x47, 0x2B, 0x45, 0x6B, 0x2F, 0x2B, 0x58, 0x6D, 0x55,      \
63             0x6B, 0x70, 0x42, 0x48, 0x51, 0x67, 0x58, 0x59, 0x57, 0x51, 0x51, 0x49, 0x67, 0x3D, 0x3D, 0x22, 0x5D,      \
64             0x2C, 0x22, 0x65, 0x6E, 0x73, 0x63, 0x68, 0x65, 0x6D, 0x61, 0x22, 0x3A, 0x22, 0x63, 0x62, 0x63, 0x31,      \
65             0x22, 0x7D                                                                                                 \
66     }
67 #define OFFLINERESPONSE                                                                                                \
68     {                                                                                                                  \
69         0x31, 0x64, 0x6E, 0x5A, 0x32, 0x4E, 0x57, 0x74, 0x76, 0x4D, 0x47, 0x34, 0x34, 0x4E, 0x6A, 0x42, 0x30, 0x4D,    \
70             0x32, 0x77, 0x33, 0x4E, 0x67, 0x3D, 0x3D, 0x3A, 0x59, 0x7A, 0x56, 0x78, 0x63, 0x48, 0x64, 0x70, 0x61,      \
71             0x6D, 0x30, 0x34, 0x59, 0x57, 0x45, 0x34, 0x5A, 0x48, 0x6B, 0x79, 0x4D, 0x67, 0x3D, 0x3D                   \
72     }
73 #define ONLINERESPONSE                                                                                                 \
74     {                                                                                                                  \
75         0x30, 0x64, 0x6E, 0x5A, 0x32, 0x4E, 0x57, 0x74, 0x76, 0x4D, 0x47, 0x34, 0x34, 0x4E, 0x6A, 0x42, 0x30, 0x4D,    \
76             0x32, 0x77, 0x33, 0x4E, 0x67, 0x3D, 0x3D, 0x3A, 0x59, 0x7A, 0x56, 0x78, 0x63, 0x48, 0x64, 0x70, 0x61,      \
77             0x6D, 0x30, 0x34, 0x59, 0x57, 0x45, 0x34, 0x5A, 0x48, 0x6B, 0x79, 0x4D, 0x67, 0x3D, 0x3D                   \
78     }
79 
80 #define CHECK_RETURN(cond, ret)                                                                                        \
81     do {                                                                                                               \
82         if ((cond)) {                                                                                                  \
83             return ret;                                                                                                \
84         }                                                                                                              \
85     } while (0)
86 #define UNUSED_PARAMETER(P) (P)
87 
GetFileSize(const char * fileName)88 static int64_t GetFileSize(const char *fileName) {
89     int64_t fileSize = 0;
90     if (fileName != nullptr) {
91         struct stat st;
92         int fd = open(fileName, O_RDWR);
93         fstat(fd, &st);
94         fileSize = st.st_size;
95         close(fd);
96     }
97     return fileSize;
98 }
99 
judge_uuid()100 static const char *judge_uuid() {
101     bool isSupport = OH_MediaKeySystem_IsSupported(IS_CLEARPLAY);
102     if (isSupport == 1) {
103         return IS_CLEARPLAY;
104     } else {
105         return IS_WISEPLAY;
106     }
107 }
108 int IsCallBack = 0;
testMediaKeySystemInfo(OH_AVPlayer * play,DRM_MediaKeySystemInfo * mediaKeySystemInfo)109 void testMediaKeySystemInfo(OH_AVPlayer *play, DRM_MediaKeySystemInfo *mediaKeySystemInfo) {
110     printf("\nEnter SetSystemInfo callBack!");
111     printf("\ntest count = %d", mediaKeySystemInfo->psshCount);
112     for (int i = 0; i < mediaKeySystemInfo->psshCount; i++) {
113         printf("\nuuid[%d] = ", i);
114         for (int j = 0; j < DATA_UUID_LEN; j++) {
115             printf("%.2x", mediaKeySystemInfo->psshInfo[i].uuid[j]);
116         }
117         printf("\n pussh[%d] = ", i);
118         for (int j = 0; j < mediaKeySystemInfo->psshInfo[i].dataLen; j++) {
119             printf("%.2x", mediaKeySystemInfo->psshInfo[i].data[j]);
120         }
121     }
122     IsCallBack = 0x55;
123 }
Test_AVPlayer_SetMediaKeySystemInfoCallback(napi_env env,napi_callback_info info)124 static napi_value Test_AVPlayer_SetMediaKeySystemInfoCallback(napi_env env, napi_callback_info info) {
125     int fileDescribe = open(PATH, O_RDONLY, 0666);
126     int64_t fileSize = GetFileSize(PATH);
127 
128     napi_value result;
129     OH_AVErrCode ret = AV_ERR_INVALID_VAL;
130     size_t argc = 2;
131     napi_value args[2] = {nullptr};
132 
133     size_t inSystemLen = 0;
134     char *inSystem = nullptr;
135     size_t callBackLen = 0;
136     char *callBack = nullptr;
137 
138     napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
139     napi_valuetype valuetype0;
140     napi_typeof(env, args[0], &valuetype0);
141     napi_valuetype valuetype1;
142     napi_typeof(env, args[1], &valuetype1);
143 
144     napi_get_value_string_utf8(env, args[0], nullptr, 0, &inSystemLen);
145     inSystem = new char[inSystemLen + 1];
146     napi_get_value_string_utf8(env, args[0], inSystem, inSystemLen + 1, &inSystemLen);
147     napi_get_value_string_utf8(env, args[1], nullptr, 0, &callBackLen);
148     callBack = new char[callBackLen + 1];
149     napi_get_value_string_utf8(env, args[1], callBack, callBackLen + 1, &callBackLen);
150 
151     OH_AVPlayer *playerHandle = OH_AVPlayer_Create();
152     OH_AVPlayer *handle_ptr = nullptr;
153     Player_MediaKeySystemInfoCallback callback_ptr = nullptr;
154     if (0 != strcmp(inSystem, " ")) {
155         handle_ptr = playerHandle;
156     }
157     if (0 != strcmp(callBack, " ")) {
158         callback_ptr = &testMediaKeySystemInfo;
159     }
160 
161     OH_AVErrCode ret1 = OH_AVPlayer_SetMediaKeySystemInfoCallback(handle_ptr, callback_ptr);
162     OH_AVErrCode ret2 = OH_AVPlayer_SetFDSource(playerHandle, fileDescribe, 0, fileSize);
163     OH_AVErrCode ret3 = OH_AVPlayer_Prepare(playerHandle);
164     OH_AVErrCode ret4 = OH_AVPlayer_Play(playerHandle);
165     OH_AVErrCode ret5 = OH_AVPlayer_Release(playerHandle);
166     if (ret1 == AV_ERR_OK && ret2 == AV_ERR_OK && ret3 == AV_ERR_OK && ret4 == AV_ERR_OK && ret5 == AV_ERR_OK) {
167         ret = AV_ERR_OK;
168     } else {
169         ret = AV_ERR_INVALID_VAL;
170     }
171     napi_create_int32(env, ret, &result);
172     return result;
173 }
174 
Test_AVPlayer_GetMediaKeySystemInfo(napi_env env,napi_callback_info info)175 static napi_value Test_AVPlayer_GetMediaKeySystemInfo(napi_env env, napi_callback_info info) {
176     int fileDescribe = open(PATH, O_RDONLY, 0666);
177     int64_t fileSize = GetFileSize(PATH);
178 
179     napi_value result;
180     OH_AVErrCode ret = AV_ERR_INVALID_VAL;
181     size_t argc = 2;
182     napi_value args[2] = {nullptr};
183     size_t inSystemLen = 0;
184     char *inSystem = nullptr;
185     size_t callBackLen = 0;
186     char *callBack = nullptr;
187     napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
188     napi_get_value_string_utf8(env, args[0], nullptr, 0, &inSystemLen);
189     inSystem = new char[inSystemLen + 1];
190     napi_get_value_string_utf8(env, args[0], inSystem, inSystemLen + 1, &inSystemLen);
191     napi_get_value_string_utf8(env, args[1], nullptr, 0, &callBackLen);
192     callBack = new char[callBackLen + 1];
193     napi_get_value_string_utf8(env, args[1], callBack, callBackLen + 1, &callBackLen);
194 
195     OH_AVPlayer *playerHandle = OH_AVPlayer_Create();
196     if (playerHandle == nullptr) {
197         napi_create_int32(env, AV_ERR_INVALID_VAL, &result);
198         return result;
199     }
200     IsCallBack = 0;
201     OH_AVErrCode ret1 = OH_AVPlayer_SetMediaKeySystemInfoCallback(playerHandle, &testMediaKeySystemInfo);
202     OH_AVErrCode ret2 = OH_AVPlayer_SetFDSource(playerHandle, fileDescribe, 0, fileSize);
203     OH_AVErrCode ret3 = OH_AVPlayer_Prepare(playerHandle);
204     OH_AVErrCode ret4 = OH_AVPlayer_Play(playerHandle);
205     sleep(5);
206     DRM_MediaKeySystemInfo getInfo;
207     memset(&getInfo, 0, sizeof(DRM_MediaKeySystemInfo));
208     OH_AVPlayer *handle_ptr = nullptr;
209     DRM_MediaKeySystemInfo *info_ptr = nullptr;
210     if (0 != strcmp(inSystem, " ")) {
211         handle_ptr = playerHandle;
212     }
213     if (0 != strcmp(callBack, " ")) {
214         info_ptr = &getInfo;
215     }
216     OH_AVErrCode ret5 = AV_ERR_INVALID_VAL;
217     if (IsCallBack == 0x55) {
218         IsCallBack = 0;
219         ret5 = OH_AVPlayer_GetMediaKeySystemInfo(handle_ptr, info_ptr);
220         printf("psshCount = %d\n", getInfo.psshCount);
221         for (int i = 0; i < DATA_UUID_LEN; i++) {
222             printf("psshinfo.uuid = %x\n", getInfo.psshInfo[0].uuid[i]);
223         }
224         printf("psshinfo.dateLen = %x\n", getInfo.psshInfo[0].dataLen);
225         for (int j = 0; j < getInfo.psshInfo[0].dataLen; j++) {
226             printf("psshinfo.date = %x\n", getInfo.psshInfo[0].data[j]);
227         }
228     }
229     OH_AVErrCode ret6 = OH_AVPlayer_Release(playerHandle);
230 
231     if (ret1 == AV_ERR_OK && ret2 == AV_ERR_OK && ret3 == AV_ERR_OK && ret4 == AV_ERR_OK && ret5 == AV_ERR_OK &&
232         ret6 == AV_ERR_OK) {
233         ret = AV_ERR_OK;
234     } else {
235         ret = AV_ERR_INVALID_VAL;
236     }
237     napi_create_int32(env, ret, &result);
238     return result;
239 }
240 
Test_AVPlayer_SetDecryptionConfig(napi_env env,napi_callback_info info)241 static napi_value Test_AVPlayer_SetDecryptionConfig(napi_env env, napi_callback_info info) {
242     int fileDescribe = open(PATH, O_RDONLY, 0666);
243     int64_t fileSize = GetFileSize(PATH);
244 
245     napi_value result;
246     OH_AVErrCode ret = AV_ERR_INVALID_VAL;
247     size_t argc = 2;
248     napi_value args[2] = {nullptr};
249     size_t inSystemLen = 0;
250     char *inSystem = nullptr;
251     size_t callBackLen = 0;
252     char *callBack = nullptr;
253     napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
254     napi_get_value_string_utf8(env, args[0], nullptr, 0, &inSystemLen);
255     inSystem = new char[inSystemLen + 1];
256     napi_get_value_string_utf8(env, args[0], inSystem, inSystemLen + 1, &inSystemLen);
257     napi_get_value_string_utf8(env, args[1], nullptr, 0, &callBackLen);
258     callBack = new char[callBackLen + 1];
259     napi_get_value_string_utf8(env, args[1], callBack, callBackLen + 1, &callBackLen);
260 
261     OH_AVPlayer *playerHandle = OH_AVPlayer_Create();
262     OH_AVErrCode ret1 = OH_AVPlayer_SetMediaKeySystemInfoCallback(playerHandle, &testMediaKeySystemInfo);
263     OH_AVErrCode ret2 = OH_AVPlayer_SetFDSource(playerHandle, fileDescribe, 0, fileSize);
264 
265     MediaKeySystem *drmKeySystem = nullptr;
266     Drm_ErrCode ret3 = OH_MediaKeySystem_Create(judge_uuid(), &drmKeySystem);
267     UNUSED_PARAMETER(ret3);
268     MediaKeySession *drmKeySession = nullptr;
269     DRM_ContentProtectionLevel contentProtectionLevel = CONTENT_PROTECTION_LEVEL_SW_CRYPTO;
270     Drm_ErrCode ret5 = OH_MediaKeySystem_CreateMediaKeySession(drmKeySystem, &contentProtectionLevel, &drmKeySession);
271     UNUSED_PARAMETER(ret5);
272     uint8_t keyData[DATA_BUFF_LEN] = {0};
273     int32_t keyDataLen = DATA_BUFF_LEN;
274 
275     bool secureVideoPath = false;
276     Drm_ErrCode ret6 = OH_MediaKeySession_RequireSecureDecoderModule(drmKeySession, "video/mp4", &secureVideoPath);
277     OH_AVErrCode ret7 = OH_AVPlayer_SetDecryptionConfig(playerHandle, drmKeySession, secureVideoPath);
278     OH_AVErrCode ret8 = OH_AVPlayer_Prepare(playerHandle);
279     OH_AVErrCode ret9 = OH_AVPlayer_Play(playerHandle);
280     OH_AVErrCode ret10 = OH_AVPlayer_Release(playerHandle);
281     Drm_ErrCode ret11 = OH_MediaKeySession_Destroy(drmKeySession);
282     Drm_ErrCode ret12 = OH_MediaKeySystem_Destroy(drmKeySystem);
283     UNUSED_PARAMETER(ret11);
284     UNUSED_PARAMETER(ret12);
285     if (ret1 == AV_ERR_OK && ret2 == AV_ERR_OK && ret6 == DRM_ERR_OK && ret7 == AV_ERR_OK && ret8 == AV_ERR_OK &&
286         ret9 == AV_ERR_OK && ret10 == AV_ERR_OK) {
287         ret = AV_ERR_OK;
288     } else {
289         ret = AV_ERR_INVALID_VAL;
290     }
291     napi_create_int32(env, ret, &result);
292     return result;
293 }
294 
test(napi_env env,napi_callback_info info)295 static napi_value test(napi_env env, napi_callback_info info) {
296     size_t argc = 2;
297     napi_value args[2] = {nullptr};
298     size_t inSystemLen = 0;
299     char *inSystem = nullptr;
300 
301     napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
302     //
303     napi_valuetype valuetype0;
304     napi_typeof(env, args[0], &valuetype0);
305 
306     napi_valuetype valuetype1;
307     napi_typeof(env, args[1], &valuetype1);
308 
309     //     double value0 = 0;
310     //     napi_get_value_double(env, args[0], &value0);
311     //
312     napi_get_value_string_utf8(env, args[1], nullptr, 0, &inSystemLen);
313     inSystem = new char[inSystemLen + 1];
314     napi_get_value_string_utf8(env, args[1], inSystem, inSystemLen + 1, &inSystemLen);
315 
316     int32_t value1 = 5;
317     if (strcmp(inSystem, " ") != 0) {
318         value1 = 32;
319     }
320     napi_value sum;
321     napi_create_int32(env, value1, &sum);
322     return sum;
323 }
324 
Test_AudioCodec_SetDecryptionConfig(napi_env env,napi_callback_info info)325 static napi_value Test_AudioCodec_SetDecryptionConfig(napi_env env, napi_callback_info info) {
326     napi_value result;
327     OH_AVErrCode ret = AV_ERR_INVALID_VAL;
328     size_t argc = 3;
329     napi_value args[3] = {nullptr};
330     size_t argc_oneLen = 0;
331     char *argc_one = nullptr;
332     size_t argc_twoLen = 0;
333     char *argc_two = nullptr;
334     uint32_t argc_three;
335     napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
336     napi_get_value_string_utf8(env, args[0], nullptr, 0, &argc_oneLen);
337     argc_one = new char[argc_oneLen + 1];
338     napi_get_value_string_utf8(env, args[0], argc_one, argc_oneLen + 1, &argc_oneLen);
339     napi_get_value_string_utf8(env, args[1], nullptr, 0, &argc_twoLen);
340     argc_two = new char[argc_twoLen + 1];
341     napi_get_value_string_utf8(env, args[1], argc_two, argc_twoLen + 1, &argc_twoLen);
342     napi_get_value_uint32(env, args[2], &argc_three);
343     OH_AVCodec *handle = OH_AudioCodec_CreateByName("OH.Media.Codec.Decoder.Audio.AAC");
344     if (handle == nullptr) {
345         OH_LOG_ERROR(LOG_APP, "testCL_AudioCodec handle is nullptr!");
346         ret = AV_ERR_INVALID_VAL;
347     } else {
348         MediaKeySystem *drmKeySystem = nullptr;
349         MediaKeySession *drmKeySession = nullptr;
350         DRM_ContentProtectionLevel sessionLevel = CONTENT_PROTECTION_LEVEL_SW_CRYPTO;
351         Drm_ErrCode ret1 = OH_MediaKeySystem_Create(judge_uuid(), &drmKeySystem);
352         Drm_ErrCode ret2 = OH_MediaKeySystem_CreateMediaKeySession(drmKeySystem, &sessionLevel, &drmKeySession);
353         bool isSecureAudio = false;
354         OH_AVCodec *handle_ptr = nullptr;
355         if (0 != strcmp(argc_one, " ")) {
356             handle_ptr = handle;
357         }
358         MediaKeySession *session_ptr = nullptr;
359         if (0 != strcmp(argc_two, " ")) {
360             session_ptr = drmKeySession;
361         }
362         isSecureAudio = (bool)argc_three;
363         OH_AVErrCode ret3 = OH_AudioCodec_SetDecryptionConfig(handle_ptr, session_ptr, isSecureAudio);
364         OH_AVErrCode ret4 = OH_AudioCodec_Destroy(handle);
365         Drm_ErrCode ret5 = OH_MediaKeySession_Destroy(drmKeySession);
366         Drm_ErrCode ret6 = OH_MediaKeySystem_Destroy(drmKeySystem);
367         if (ret1 == DRM_ERR_OK && ret2 == DRM_ERR_OK && ret3 == AV_ERR_OK && ret4 == AV_ERR_OK && ret5 == DRM_ERR_OK &&
368             ret6 == DRM_ERR_OK) {
369             ret = AV_ERR_OK;
370         } else {
371             ret = AV_ERR_INVALID_VAL;
372         }
373     }
374     OH_LOG_INFO(LOG_APP, "xx success ret is %{public}d", ret);
375     napi_create_int32(env, ret, &result);
376     return result;
377 }
378 
Test_VideoDecoder_SetDecryptionConfig(napi_env env,napi_callback_info info)379 static napi_value Test_VideoDecoder_SetDecryptionConfig(napi_env env, napi_callback_info info) {
380     napi_value result;
381     OH_AVErrCode ret = AV_ERR_INVALID_VAL;
382     size_t argc = 3;
383     napi_value args[3] = {nullptr};
384     size_t argc_oneLen = 0;
385     char *argc_one = nullptr;
386     size_t argc_twoLen = 0;
387     char *argc_two = nullptr;
388     uint32_t argc_three;
389     napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
390     napi_get_value_string_utf8(env, args[0], nullptr, 0, &argc_oneLen);
391     argc_one = new char[argc_oneLen + 1];
392     napi_get_value_string_utf8(env, args[0], argc_one, argc_oneLen + 1, &argc_oneLen);
393     napi_get_value_string_utf8(env, args[1], nullptr, 0, &argc_twoLen);
394     argc_two = new char[argc_twoLen + 1];
395     napi_get_value_string_utf8(env, args[1], argc_two, argc_twoLen + 1, &argc_twoLen);
396     napi_get_value_uint32(env, args[2], &argc_three);
397 
398     OH_AVCapability *capability = OH_AVCodec_GetCapability(OH_AVCODEC_MIMETYPE_VIDEO_AVC, false);
399     const char *name = OH_AVCapability_GetName(capability);
400     OH_AVCodec *handle = OH_VideoDecoder_CreateByName(name);
401     if (handle == nullptr) {
402         OH_LOG_ERROR(LOG_APP, "testCL_VideoDecoder handle is nullptr!");
403         ret = AV_ERR_INVALID_VAL;
404     } else {
405         MediaKeySystem *drmKeySystem = nullptr;
406         MediaKeySession *drmKeySession = nullptr;
407         DRM_ContentProtectionLevel sessionLevel = CONTENT_PROTECTION_LEVEL_SW_CRYPTO;
408         Drm_ErrCode ret1 = OH_MediaKeySystem_Create(judge_uuid(), &drmKeySystem);
409         Drm_ErrCode ret2 = OH_MediaKeySystem_CreateMediaKeySession(drmKeySystem, &sessionLevel, &drmKeySession);
410         bool isSecureAudio = false;
411         OH_AVCodec *handle_ptr = nullptr;
412         if (0 != strcmp(argc_one, " ")) {
413             handle_ptr = handle;
414         }
415         MediaKeySession *session_ptr = nullptr;
416         if (0 != strcmp(argc_two, " ")) {
417             session_ptr = drmKeySession;
418         }
419         isSecureAudio = (bool)argc_three;
420         OH_AVErrCode ret3 = OH_VideoDecoder_SetDecryptionConfig(handle_ptr, session_ptr, isSecureAudio);
421         OH_AVErrCode ret4 = OH_VideoDecoder_Destroy(handle);
422         Drm_ErrCode ret5 = OH_MediaKeySession_Destroy(drmKeySession);
423         Drm_ErrCode ret6 = OH_MediaKeySystem_Destroy(drmKeySystem);
424         if (ret1 == DRM_ERR_OK && ret2 == DRM_ERR_OK && ret3 == AV_ERR_OK && ret4 == AV_ERR_OK && ret5 == DRM_ERR_OK &&
425             ret6 == DRM_ERR_OK) {
426             ret = AV_ERR_OK;
427         } else {
428             ret = AV_ERR_INVALID_VAL;
429         }
430     }
431     OH_LOG_INFO(LOG_APP, "xx success ret is %{public}d", ret);
432     napi_create_int32(env, ret, &result);
433     return result;
434 }
435 // DRM信息监听回调OnDrmInfoChanged实现
OnDrmInfoChanged(DRM_MediaKeySystemInfo * drmInfo)436 static void OnDrmInfoChanged(DRM_MediaKeySystemInfo *drmInfo) { OH_LOG_DEBUG(LOG_APP, "Enter OnDrmInfoChanged!"); }
TestAVDemuxer_SetMediaKeySystemInfoCallback(napi_env env,napi_callback_info info)437 static napi_value TestAVDemuxer_SetMediaKeySystemInfoCallback(napi_env env, napi_callback_info info) {
438     napi_value result;
439     OH_AVErrCode ret = AV_ERR_INVALID_VAL;
440     size_t argc = 2;
441     napi_value args[2] = {nullptr};
442     size_t args_oneLen = 0;
443     char *args_one = nullptr;
444     size_t args_twoLen = 0;
445     char *args_two = nullptr;
446     napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
447     napi_get_value_string_utf8(env, args[0], nullptr, 0, &args_oneLen);
448     args_one = new char[args_oneLen + 1];
449     napi_get_value_string_utf8(env, args[0], args_one, args_oneLen + 1, &args_oneLen);
450     napi_get_value_string_utf8(env, args[1], nullptr, 0, &args_twoLen);
451     args_two = new char[args_twoLen + 1];
452     napi_get_value_string_utf8(env, args[1], args_two, args_twoLen + 1, &args_twoLen);
453     OH_AVSource *source = nullptr;
454 
455     int fileDescribe = open(PATH, O_RDONLY, 0666);
456     int64_t fileSize = GetFileSize(PATH);
457 
458     if (judge_uuid() == IS_CLEARPLAY) {
459         napi_create_int32(env, AV_ERR_OK, &result);
460         return result;
461     } else {
462         source = OH_AVSource_CreateWithFD(fileDescribe, 0, fileSize);
463     }
464     if (source == nullptr) {
465         napi_create_int32(env, -2, &result); // 异常result
466         return result;
467     }
468     // 为资源对象创建对应的解封装器
469     OH_AVDemuxer *hdemuxer = OH_AVDemuxer_CreateWithSource(source);
470     if (hdemuxer == nullptr) {
471         OH_LOG_ERROR(LOG_APP, "create demuxer failed");
472         napi_create_int32(env, -3, &result); // 异常result
473         return result;
474     }
475     DRM_MediaKeySystemInfoCallback callback = &OnDrmInfoChanged;
476     OH_AVDemuxer *handle_ptr = nullptr;
477     if (0 != strcmp(args_one, " ")) {
478         handle_ptr = hdemuxer;
479     }
480     DRM_MediaKeySystemInfoCallback callback_ptr = nullptr;
481     if (0 != strcmp(args_two, " ")) {
482         callback_ptr = callback;
483     }
484     OH_AVErrCode ret1 = OH_AVDemuxer_SetMediaKeySystemInfoCallback(handle_ptr, callback_ptr);
485     OH_AVErrCode ret2 = OH_AVDemuxer_Destroy(hdemuxer);
486 
487     if (ret1 == AV_ERR_OK && ret2 == AV_ERR_OK) {
488         ret = AV_ERR_OK;
489     } else {
490         ret = AV_ERR_INVALID_VAL;
491     }
492     napi_create_int32(env, ret, &result);
493     return result;
494 }
495 
496 // DRM信息监听回调OnDrmInfoChangedWithObj实现
OnDrmInfoChangedWithObj(OH_AVDemuxer * demuxer,DRM_MediaKeySystemInfo * drmInfo)497 static void OnDrmInfoChangedWithObj(OH_AVDemuxer *demuxer, DRM_MediaKeySystemInfo *drmInfo) {
498     OH_LOG_DEBUG(LOG_APP, "Enter OnDrmInfoChangedWithObj!");
499 }
TestAVDemuxer_SetDemuxerMediaKeySystemInfoCallback(napi_env env,napi_callback_info info)500 static napi_value TestAVDemuxer_SetDemuxerMediaKeySystemInfoCallback(napi_env env, napi_callback_info info) {
501     napi_value result;
502     OH_AVErrCode ret = AV_ERR_INVALID_VAL;
503     size_t argc = 2;
504     napi_value args[2] = {nullptr};
505     size_t args_oneLen = 0;
506     char *args_one = nullptr;
507     size_t args_twoLen = 0;
508     char *args_two = nullptr;
509     napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
510     napi_get_value_string_utf8(env, args[0], nullptr, 0, &args_oneLen);
511     args_one = new char[args_oneLen + 1];
512     napi_get_value_string_utf8(env, args[0], args_one, args_oneLen + 1, &args_oneLen);
513     napi_get_value_string_utf8(env, args[1], nullptr, 0, &args_twoLen);
514     args_two = new char[args_twoLen + 1];
515     napi_get_value_string_utf8(env, args[1], args_two, args_twoLen + 1, &args_twoLen);
516 
517     napi_create_int32(env, -1, &result); // 异常result
518     OH_AVSource *source = nullptr;
519 
520     int fileDescribe = open(PATH, O_RDONLY, 0666);
521     int64_t fileSize = GetFileSize(PATH);
522 
523     if (judge_uuid() == IS_CLEARPLAY) {
524         napi_create_int32(env, AV_ERR_OK, &result);
525         return result;
526     } else {
527         source = OH_AVSource_CreateWithFD(fileDescribe, 0, fileSize);
528     }
529     if (source == nullptr) {
530         OH_LOG_ERROR(LOG_APP, "create source failed");
531         return result;
532     }
533     // 为资源对象创建对应的解封装器
534     OH_AVDemuxer *hdemuxer = OH_AVDemuxer_CreateWithSource(source);
535     if (hdemuxer == nullptr) {
536         OH_LOG_ERROR(LOG_APP, "create demuxer failed");
537         return result;
538     }
539     Demuxer_MediaKeySystemInfoCallback callback = &OnDrmInfoChangedWithObj;
540     OH_AVDemuxer *handle_ptr = nullptr;
541     if (0 != strcmp(args_one, " ")) {
542         handle_ptr = hdemuxer;
543     }
544     Demuxer_MediaKeySystemInfoCallback callback_ptr = nullptr;
545     if (0 != strcmp(args_two, " ")) {
546         callback_ptr = callback;
547     }
548     OH_AVErrCode ret1 = OH_AVDemuxer_SetDemuxerMediaKeySystemInfoCallback(handle_ptr, callback_ptr);
549     OH_AVErrCode ret2 = OH_AVDemuxer_Destroy(hdemuxer);
550 
551     if (ret1 == AV_ERR_OK && ret2 == AV_ERR_OK) {
552         ret = AV_ERR_OK;
553     } else {
554         ret = AV_ERR_INVALID_VAL;
555     }
556     napi_create_int32(env, ret, &result);
557     return result;
558 }
TestAVDemuxer_GetMediaKeySystemInfo(napi_env env,napi_callback_info info)559 static napi_value TestAVDemuxer_GetMediaKeySystemInfo(napi_env env, napi_callback_info info) {
560     napi_value result;
561     OH_AVErrCode ret = AV_ERR_INVALID_VAL;
562     size_t argc = 2;
563     napi_value args[2] = {nullptr};
564     size_t args_oneLen = 0;
565     char *args_one = nullptr;
566     size_t args_twoLen = 0;
567     char *args_two = nullptr;
568     napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
569     napi_get_value_string_utf8(env, args[0], nullptr, 0, &args_oneLen);
570     args_one = new char[args_oneLen + 1];
571     napi_get_value_string_utf8(env, args[0], args_one, args_oneLen + 1, &args_oneLen);
572     napi_get_value_string_utf8(env, args[1], nullptr, 0, &args_twoLen);
573     args_two = new char[args_twoLen + 1];
574     napi_get_value_string_utf8(env, args[1], args_two, args_twoLen + 1, &args_twoLen);
575 
576     napi_create_int32(env, -1, &result); // 异常result
577 
578     OH_AVSource *source = nullptr;
579 
580     int fileDescribe = open(PATH, O_RDONLY, 0666);
581     int64_t fileSize = GetFileSize(PATH);
582 
583     if (judge_uuid() == IS_CLEARPLAY) {
584         napi_create_int32(env, AV_ERR_OK, &result);
585         return result;
586     } else {
587         source = OH_AVSource_CreateWithFD(fileDescribe, 0, fileSize);
588     }
589     if (source == nullptr) {
590         OH_LOG_ERROR(LOG_APP, "create source failed");
591         return result;
592     }
593 
594     // 为资源对象创建对应的解封装器
595     OH_AVDemuxer *hdemuxer = OH_AVDemuxer_CreateWithSource(source);
596     if (hdemuxer == nullptr) {
597         OH_LOG_ERROR(LOG_APP, "create demuxer failed");
598         return result;
599     }
600     Demuxer_MediaKeySystemInfoCallback callback = &OnDrmInfoChangedWithObj;
601     OH_AVErrCode ret1 = OH_AVDemuxer_SetDemuxerMediaKeySystemInfoCallback(hdemuxer, callback);
602     DRM_MediaKeySystemInfo mediaKeySystemInfo;
603     memset(&mediaKeySystemInfo, 0x00, sizeof(DRM_MediaKeySystemInfo));
604     OH_AVDemuxer *handle_ptr = nullptr;
605     if (0 != strcmp(args_one, " ")) {
606         handle_ptr = hdemuxer;
607     }
608     DRM_MediaKeySystemInfo *callback_ptr = nullptr;
609     if (0 != strcmp(args_two, " ")) {
610         callback_ptr = &mediaKeySystemInfo;
611     }
612     OH_AVErrCode ret2 = OH_AVDemuxer_GetMediaKeySystemInfo(handle_ptr, callback_ptr);
613     if ((ret2 == AV_ERR_OK) && (callback_ptr != nullptr)) {
614         OH_LOG_ERROR(LOG_APP, "GetMediaKeySystemInfo count %d", mediaKeySystemInfo.psshCount);
615         for (uint32_t i = 0; i < mediaKeySystemInfo.psshCount; i++) {
616             OH_LOG_ERROR(LOG_APP, "GetMediaKeySystemInfo print");
617             const uint32_t uuidLen = 16;
618             for (uint32_t index = 0; index < uuidLen; index++) {
619                 OH_LOG_ERROR(LOG_APP, "GetMediaKeySystemInfo print uuid %x \n",
620                              mediaKeySystemInfo.psshInfo[i].uuid[index]);
621             }
622             OH_LOG_ERROR(LOG_APP, "GetMediaKeySystemInfo print pssh length %d \n",
623                          mediaKeySystemInfo.psshInfo[i].dataLen);
624             for (uint32_t k = 0; k < mediaKeySystemInfo.psshInfo[i].dataLen; k++) {
625                 unsigned char *pssh = static_cast<unsigned char *>(mediaKeySystemInfo.psshInfo[i].data);
626                 OH_LOG_ERROR(LOG_APP, "GetMediaKeySystemInfo print pssh %x \n", pssh[k]);
627             }
628         }
629     }
630 
631     OH_AVErrCode ret3 = OH_AVDemuxer_Destroy(hdemuxer);
632     if (ret1 == AV_ERR_OK && ret2 == AV_ERR_OK && ret3 == AV_ERR_OK) {
633         ret = AV_ERR_OK;
634     } else {
635         ret = AV_ERR_INVALID_VAL;
636     }
637     napi_create_int32(env, ret, &result);
638     return result;
639 }
640 
641 EXTERN_C_START
Init(napi_env env,napi_value exports)642 static napi_value Init(napi_env env, napi_value exports) {
643     napi_property_descriptor desc[] = {
644         {"Test_AVPlayer_SetMediaKeySystemInfoCallback", nullptr, Test_AVPlayer_SetMediaKeySystemInfoCallback, nullptr,
645          nullptr, nullptr, napi_default, nullptr},
646         {"Test_AVPlayer_GetMediaKeySystemInfo", nullptr, Test_AVPlayer_GetMediaKeySystemInfo, nullptr, nullptr, nullptr,
647          napi_default, nullptr},
648         {"Test_AVPlayer_SetDecryptionConfig", nullptr, Test_AVPlayer_SetDecryptionConfig, nullptr, nullptr, nullptr,
649          napi_default, nullptr},
650         {"Test_AudioCodec_SetDecryptionConfig", nullptr, Test_AudioCodec_SetDecryptionConfig, nullptr, nullptr, nullptr,
651          napi_default, nullptr},
652         {"Test_VideoDecoder_SetDecryptionConfig", nullptr, Test_VideoDecoder_SetDecryptionConfig, nullptr, nullptr,
653          nullptr, napi_default, nullptr},
654         {"TestAVDemuxer_SetMediaKeySystemInfoCallback", nullptr, TestAVDemuxer_SetMediaKeySystemInfoCallback, nullptr,
655          nullptr, nullptr, napi_default, nullptr},
656         {"TestAVDemuxer_SetDemuxerMediaKeySystemInfoCallback", nullptr,
657          TestAVDemuxer_SetDemuxerMediaKeySystemInfoCallback, nullptr, nullptr, nullptr, napi_default, nullptr},
658         {"TestAVDemuxer_GetMediaKeySystemInfo", nullptr, TestAVDemuxer_GetMediaKeySystemInfo, nullptr, nullptr, nullptr,
659          napi_default, nullptr},
660     };
661     napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc);
662     return exports;
663 }
664 EXTERN_C_END
665 
666 static napi_module demoModule = {
667     .nm_version = 1,
668     .nm_flags = 0,
669     .nm_filename = nullptr,
670     .nm_register_func = Init,
671     .nm_modname = "entry",
672     .nm_priv = ((void *)0),
673     .reserved = {0},
674 };
675 
RegisterEntryModule(void)676 extern "C" __attribute__((constructor)) void RegisterEntryModule(void) { napi_module_register(&demoModule); }
677