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 <multimedia/drm_framework/native_drm_common.h>
17 #include <multimedia/drm_framework/native_drm_err.h>
18 #include <multimedia/drm_framework/native_mediakeysystem.h>
19 #include <multimedia/drm_framework/native_mediakeysession.h>
20 #include <multimedia/player_framework/native_avcodec_videodecoder.h>
21 #include <multimedia/player_framework/native_avsource.h>
22 #include <multimedia/player_framework/native_avdemuxer.h>
23 #include <string.h>
24
25 #define BLUE_AREA "com.clearplay.drm"
26 #define YELLOW_AREA "com.wiseplay.drm"
judge_uuid()27 static const char* judge_uuid() {
28 bool isSupport = OH_MediaKeySystem_IsSupported(BLUE_AREA);
29 if(isSupport == 1) {
30 return BLUE_AREA;
31 } else {
32 return YELLOW_AREA;
33 }
34 }
35 // avcodec
Test_VideoDecoder_SetDecryptionConfig(napi_env env,napi_callback_info info)36 static napi_value Test_VideoDecoder_SetDecryptionConfig(napi_env env, napi_callback_info info) {
37 napi_value res;
38 size_t argc = 3;
39 napi_value args[3] = {nullptr};
40 size_t codecStringLen = 0;
41 char *codecString = nullptr;
42 size_t sessionStringLen = 0;
43 char *sessionString = nullptr;
44 bool secureVideoPath = 0;
45 Drm_ErrCode ret = DRM_ERR_INVALID_VAL;
46 MediaKeySystem *drmKeySystem = nullptr;
47 MediaKeySession *drmKeySession = nullptr;
48
49 napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
50 napi_get_value_string_utf8(env, args[0], nullptr, 0, &codecStringLen);
51 codecString = new char[codecStringLen + 1];
52 napi_get_value_string_utf8(env, args[0], codecString, codecStringLen + 1, &codecStringLen);
53 napi_get_value_string_utf8(env, args[1], nullptr, 0, &sessionStringLen);
54 sessionString = new char[sessionStringLen + 1];
55 napi_get_value_string_utf8(env, args[1], sessionString, sessionStringLen + 1, &sessionStringLen);
56 napi_get_value_bool(env, args[2], &secureVideoPath);
57
58 Drm_ErrCode ret1 = OH_MediaKeySystem_Create(judge_uuid(), &drmKeySystem);
59 DRM_ContentProtectionLevel sessionLevel = CONTENT_PROTECTION_LEVEL_SW_CRYPTO;
60 Drm_ErrCode ret2 = OH_MediaKeySystem_CreateMediaKeySession(drmKeySystem,&sessionLevel,&drmKeySession);
61 MediaKeySession *session_ptr = nullptr;
62 if(strcmp(sessionString," ") != 0){
63 session_ptr = drmKeySession;
64 }
65 OH_AVCodec *codec_ptr = nullptr;
66 if(strcmp(codecString," ") != 0){
67 codec_ptr = OH_VideoDecoder_CreateByName("OMX.hisi.video.decoder.avc.seure");
68 }
69 OH_VideoDecoder_SetDecryptionConfig(codec_ptr, session_ptr,secureVideoPath);
70 Drm_ErrCode ret4 = OH_MediaKeySession_Destroy(drmKeySession);
71 Drm_ErrCode ret5 = OH_MediaKeySystem_Destroy(drmKeySystem);
72 if (ret1 == DRM_ERR_OK && ret2 == DRM_ERR_OK && ret4 == DRM_ERR_OK && ret5 == DRM_ERR_OK) { //
73 ret = DRM_ERR_OK;
74 } else {
75 ret = DRM_ERR_INVALID_VAL;
76 }
77 napi_create_int32(env, ret, &res);
78 return res;
79 }
Test_AVDemuxer_GetMediaKeySystemInfo(napi_env env,napi_callback_info info)80 static napi_value Test_AVDemuxer_GetMediaKeySystemInfo(napi_env env, napi_callback_info info) {
81 napi_value res;
82 size_t argc = 2;
83 napi_value args[2] = {nullptr};
84 size_t demuxerStringLen = 0;
85 char *demuxerString = nullptr;
86 size_t infoStringLen = 0;
87 char *infoString = nullptr;
88 Drm_ErrCode ret = DRM_ERR_INVALID_VAL;
89 MediaKeySystem *drmKeySystem = nullptr;
90 MediaKeySession *drmKeySession = nullptr;
91
92 napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
93 napi_get_value_string_utf8(env, args[0], nullptr, 0, &demuxerStringLen);
94 demuxerString = new char[demuxerStringLen + 1];
95 napi_get_value_string_utf8(env, args[0], demuxerString, demuxerStringLen + 1, &demuxerStringLen);
96 napi_get_value_string_utf8(env, args[1], nullptr, 0, &infoStringLen);
97 infoString = new char[infoStringLen + 1];
98 napi_get_value_string_utf8(env, args[1], infoString, infoStringLen + 1, &infoStringLen);
99
100 Drm_ErrCode ret1 = OH_MediaKeySystem_Create(judge_uuid(), &drmKeySystem);
101 DRM_ContentProtectionLevel sessionLevel = CONTENT_PROTECTION_LEVEL_SW_CRYPTO;
102 Drm_ErrCode ret2 = OH_MediaKeySystem_CreateMediaKeySession(drmKeySystem,&sessionLevel,&drmKeySession);
103 DRM_MediaKeySystemInfo mediaKeySystemInfo;
104 memset(&mediaKeySystemInfo,0,sizeof(DRM_MediaKeySystemInfo));
105 DRM_MediaKeySystemInfo *mediaKeySystemInfo_ptr = nullptr;
106 if(strcmp(infoString," ") != 0){
107 mediaKeySystemInfo_ptr = &mediaKeySystemInfo;
108 }
109 OH_AVDemuxer *demuxer_ptr = nullptr;
110 if(strcmp(demuxerString," ") != 0){
111 // OH_AVSource *source = OH_AVSource_CreateWithURI("http://192.168.200.62:8000/ChinaDRMLab-264-30-480P-SM4C/1_enc.m3u8");
112 OH_AVSource *source = nullptr;
113 demuxer_ptr = OH_AVDemuxer_CreateWithSource(source);
114 }
115 OH_AVDemuxer_GetMediaKeySystemInfo(demuxer_ptr, mediaKeySystemInfo_ptr);
116 Drm_ErrCode ret4 = OH_MediaKeySession_Destroy(drmKeySession);
117 Drm_ErrCode ret5 = OH_MediaKeySystem_Destroy(drmKeySystem);
118 if (ret1 == DRM_ERR_OK && ret2 == DRM_ERR_OK && ret4 == DRM_ERR_OK && ret5 == DRM_ERR_OK) {
119 ret = DRM_ERR_OK;
120 } else {
121 ret = DRM_ERR_INVALID_VAL;
122 }
123 napi_create_int32(env, ret, &res);
124 return res;
125 }
OnDrmInfoChangedInApp(DRM_MediaKeySystemInfo * drmInfo)126 static void OnDrmInfoChangedInApp(DRM_MediaKeySystemInfo *drmInfo)
127 {
128 for (uint32_t i = 0; i < drmInfo->psshCount; i++)
129 {
130 for (uint32_t k = 0; k < drmInfo->psshInfo[i].dataLen; k++) {
131 unsigned char *pssh = static_cast<unsigned char*>(drmInfo->psshInfo[i].data);
132 printf("OnDrmInfoChangedInApp print pssh %d", pssh[k]);
133 }
134 }
135 }
Test_AVDemuxer_SetMediaKeySystemInfoCallback(napi_env env,napi_callback_info info)136 static napi_value Test_AVDemuxer_SetMediaKeySystemInfoCallback(napi_env env, napi_callback_info info) {
137 napi_value res;
138 size_t argc = 2;
139 napi_value args[2] = {nullptr};
140 size_t demuxerStringLen = 0;
141 char *demuxerString = nullptr;
142 size_t infoStringLen = 0;
143 char *infoString = nullptr;
144 Drm_ErrCode ret = DRM_ERR_INVALID_VAL;
145 MediaKeySystem *drmKeySystem = nullptr;
146 MediaKeySession *drmKeySession = nullptr;
147
148 napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
149 napi_get_value_string_utf8(env, args[0], nullptr, 0, &demuxerStringLen);
150 demuxerString = new char[demuxerStringLen + 1];
151 napi_get_value_string_utf8(env, args[0], demuxerString, demuxerStringLen + 1, &demuxerStringLen);
152 napi_get_value_string_utf8(env, args[1], nullptr, 0, &infoStringLen);
153 infoString = new char[infoStringLen + 1];
154 napi_get_value_string_utf8(env, args[1], infoString, infoStringLen + 1, &infoStringLen);
155
156 Drm_ErrCode ret1 = OH_MediaKeySystem_Create(judge_uuid(), &drmKeySystem);
157 DRM_ContentProtectionLevel sessionLevel = CONTENT_PROTECTION_LEVEL_SW_CRYPTO;
158 Drm_ErrCode ret2 = OH_MediaKeySystem_CreateMediaKeySession(drmKeySystem,&sessionLevel,&drmKeySession);
159 DRM_MediaKeySystemInfo mediaKeySystemInfo;
160 memset(&mediaKeySystemInfo,0,sizeof(DRM_MediaKeySystemInfo));
161 DRM_MediaKeySystemInfoCallback callback = nullptr;
162 if(strcmp(infoString," ") != 0){
163 callback = &OnDrmInfoChangedInApp;
164 }
165 OH_AVDemuxer *demuxer_ptr = nullptr;
166 if(strcmp(demuxerString," ") != 0){
167 // OH_AVSource *source = OH_AVSource_CreateWithURI("http://192.168.200.62:8000/ChinaDRMLab-264-30-480P-SM4C/1_enc.m3u8");
168 OH_AVSource *source = nullptr;
169 demuxer_ptr = OH_AVDemuxer_CreateWithSource(source);
170 }
171
172 OH_AVDemuxer_SetMediaKeySystemInfoCallback(demuxer_ptr, callback);
173 Drm_ErrCode ret4 = OH_MediaKeySession_Destroy(drmKeySession);
174 Drm_ErrCode ret5 = OH_MediaKeySystem_Destroy(drmKeySystem);
175 if (ret1 == DRM_ERR_OK && ret2 == DRM_ERR_OK && ret4 == DRM_ERR_OK && ret5 == DRM_ERR_OK) {
176 ret = DRM_ERR_OK;
177 } else {
178 ret = DRM_ERR_INVALID_VAL;
179 }
180 napi_create_int32(env, ret, &res);
181 return res;
182 }
183 EXTERN_C_START
Init(napi_env env,napi_value exports)184 static napi_value Init(napi_env env, napi_value exports) {
185 napi_property_descriptor desc[] = {
186 {"Test_VideoDecoder_SetDecryptionConfig", nullptr, Test_VideoDecoder_SetDecryptionConfig, nullptr,
187 nullptr, nullptr, napi_default, nullptr},
188 {"Test_AVDemuxer_GetMediaKeySystemInfo", nullptr, Test_AVDemuxer_GetMediaKeySystemInfo, nullptr,
189 nullptr, nullptr, napi_default, nullptr},
190 {"Test_AVDemuxer_SetMediaKeySystemInfoCallback", nullptr, Test_AVDemuxer_SetMediaKeySystemInfoCallback, nullptr,
191 nullptr, nullptr, napi_default, nullptr},
192 };
193 napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc);
194 return exports;
195 }
196 EXTERN_C_END
197
198 static napi_module demoModule = {
199 .nm_version = 1,
200 .nm_flags = 0,
201 .nm_filename = nullptr,
202 .nm_register_func = Init,
203 .nm_modname = "entry",
204 .nm_priv = ((void *)0),
205 .reserved = {0},
206 };
207
RegisterEntryModule(void)208 extern "C" __attribute__((constructor)) void RegisterEntryModule(void) { napi_module_register(&demoModule); }
209