• 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 <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     OH_AudioCodec_SetDecryptionConfig(codec_ptr, session_ptr,secureVideoPath);
71     Drm_ErrCode ret4 = OH_MediaKeySession_Destroy(drmKeySession);
72     Drm_ErrCode ret5 = OH_MediaKeySystem_Destroy(drmKeySystem);
73     if (ret1 == DRM_ERR_OK && ret2 == DRM_ERR_OK  && ret4 == DRM_ERR_OK && ret5 == DRM_ERR_OK) { //
74         ret = DRM_ERR_OK;
75     } else {
76         ret = DRM_ERR_INVALID_VAL;
77     }
78     napi_create_int32(env, ret, &res);
79     return res;
80 }
Test_AVDemuxer_GetMediaKeySystemInfo(napi_env env,napi_callback_info info)81 static napi_value Test_AVDemuxer_GetMediaKeySystemInfo(napi_env env, napi_callback_info info) {
82     napi_value res;
83     size_t argc = 2;
84     napi_value args[2] = {nullptr};
85     size_t demuxerStringLen = 0;
86     char *demuxerString = nullptr;
87     size_t infoStringLen = 0;
88     char *infoString = nullptr;
89     Drm_ErrCode ret = DRM_ERR_INVALID_VAL;
90     MediaKeySystem *drmKeySystem = nullptr;
91     MediaKeySession *drmKeySession = nullptr;
92 
93     napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
94     napi_get_value_string_utf8(env, args[0], nullptr, 0, &demuxerStringLen);
95     demuxerString = new char[demuxerStringLen + 1];
96     napi_get_value_string_utf8(env, args[0], demuxerString, demuxerStringLen + 1, &demuxerStringLen);
97     napi_get_value_string_utf8(env, args[1], nullptr, 0, &infoStringLen);
98     infoString = new char[infoStringLen + 1];
99     napi_get_value_string_utf8(env, args[1], infoString, infoStringLen + 1, &infoStringLen);
100 
101     Drm_ErrCode ret1 = OH_MediaKeySystem_Create(judge_uuid(), &drmKeySystem);
102     DRM_ContentProtectionLevel sessionLevel = CONTENT_PROTECTION_LEVEL_SW_CRYPTO;
103     Drm_ErrCode ret2 = OH_MediaKeySystem_CreateMediaKeySession(drmKeySystem,&sessionLevel,&drmKeySession);
104     DRM_MediaKeySystemInfo mediaKeySystemInfo;
105     memset(&mediaKeySystemInfo,0,sizeof(DRM_MediaKeySystemInfo));
106     DRM_MediaKeySystemInfo *mediaKeySystemInfo_ptr = nullptr;
107     if(strcmp(infoString," ") != 0){
108         mediaKeySystemInfo_ptr = &mediaKeySystemInfo;
109     }
110     OH_AVDemuxer *demuxer_ptr = nullptr;
111     if(strcmp(demuxerString," ") != 0){
112 //        OH_AVSource *source = OH_AVSource_CreateWithURI("http://192.168.200.62:8000/ChinaDRMLab-264-30-480P-SM4C/1_enc.m3u8");
113         OH_AVSource *source = nullptr;
114         demuxer_ptr = OH_AVDemuxer_CreateWithSource(source);
115     }
116     OH_AVDemuxer_GetMediaKeySystemInfo(demuxer_ptr, mediaKeySystemInfo_ptr);
117     Drm_ErrCode ret4 = OH_MediaKeySession_Destroy(drmKeySession);
118     Drm_ErrCode ret5 = OH_MediaKeySystem_Destroy(drmKeySystem);
119     if (ret1 == DRM_ERR_OK && ret2 == DRM_ERR_OK  && ret4 == DRM_ERR_OK && ret5 == DRM_ERR_OK) {
120         ret = DRM_ERR_OK;
121     } else {
122         ret = DRM_ERR_INVALID_VAL;
123     }
124     napi_create_int32(env, ret, &res);
125     return res;
126 }
OnDrmInfoChangedInApp(DRM_MediaKeySystemInfo * drmInfo)127 static void OnDrmInfoChangedInApp(DRM_MediaKeySystemInfo *drmInfo)
128 {
129     for (uint32_t i = 0; i < drmInfo->psshCount; i++)
130     {
131         for (uint32_t k = 0; k < drmInfo->psshInfo[i].dataLen; k++) {
132             unsigned char *pssh = static_cast<unsigned char*>(drmInfo->psshInfo[i].data);
133 			printf("OnDrmInfoChangedInApp print pssh %d", pssh[k]);
134         }
135     }
136 }
Test_AVDemuxer_SetMediaKeySystemInfoCallback(napi_env env,napi_callback_info info)137 static napi_value Test_AVDemuxer_SetMediaKeySystemInfoCallback(napi_env env, napi_callback_info info) {
138     napi_value res;
139     size_t argc = 2;
140     napi_value args[2] = {nullptr};
141     size_t demuxerStringLen = 0;
142     char *demuxerString = nullptr;
143     size_t infoStringLen = 0;
144     char *infoString = nullptr;
145     Drm_ErrCode ret = DRM_ERR_INVALID_VAL;
146     MediaKeySystem *drmKeySystem = nullptr;
147     MediaKeySession *drmKeySession = nullptr;
148 
149     napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
150     napi_get_value_string_utf8(env, args[0], nullptr, 0, &demuxerStringLen);
151     demuxerString = new char[demuxerStringLen + 1];
152     napi_get_value_string_utf8(env, args[0], demuxerString, demuxerStringLen + 1, &demuxerStringLen);
153     napi_get_value_string_utf8(env, args[1], nullptr, 0, &infoStringLen);
154     infoString = new char[infoStringLen + 1];
155     napi_get_value_string_utf8(env, args[1], infoString, infoStringLen + 1, &infoStringLen);
156 
157     Drm_ErrCode ret1 = OH_MediaKeySystem_Create(judge_uuid(), &drmKeySystem);
158     DRM_ContentProtectionLevel sessionLevel = CONTENT_PROTECTION_LEVEL_SW_CRYPTO;
159     Drm_ErrCode ret2 = OH_MediaKeySystem_CreateMediaKeySession(drmKeySystem,&sessionLevel,&drmKeySession);
160     DRM_MediaKeySystemInfo mediaKeySystemInfo;
161     memset(&mediaKeySystemInfo,0,sizeof(DRM_MediaKeySystemInfo));
162     DRM_MediaKeySystemInfoCallback callback = nullptr;
163     if(strcmp(infoString," ") != 0){
164         callback = &OnDrmInfoChangedInApp;
165     }
166     OH_AVDemuxer *demuxer_ptr = nullptr;
167     if(strcmp(demuxerString," ") != 0){
168 //        OH_AVSource *source = OH_AVSource_CreateWithURI("http://192.168.200.62:8000/ChinaDRMLab-264-30-480P-SM4C/1_enc.m3u8");
169         OH_AVSource *source = nullptr;
170         demuxer_ptr = OH_AVDemuxer_CreateWithSource(source);
171     }
172 
173     OH_AVDemuxer_SetMediaKeySystemInfoCallback(demuxer_ptr, callback);
174     OH_AVErrCode ret1 = OH_AVDemuxer_SetDemuxerMediaKeySystemInfoCallback(demuxer_ptr, callback);
175     Drm_ErrCode ret4 = OH_MediaKeySession_Destroy(drmKeySession);
176     Drm_ErrCode ret5 = OH_MediaKeySystem_Destroy(drmKeySystem);
177     if (ret1 == DRM_ERR_OK && ret2 == DRM_ERR_OK  && ret4 == DRM_ERR_OK && ret5 == DRM_ERR_OK) {
178         ret = DRM_ERR_OK;
179     } else {
180         ret = DRM_ERR_INVALID_VAL;
181     }
182     napi_create_int32(env, ret, &res);
183     return res;
184 }
185 EXTERN_C_START
Init(napi_env env,napi_value exports)186 static napi_value Init(napi_env env, napi_value exports) {
187     napi_property_descriptor desc[] = {
188         {"Test_VideoDecoder_SetDecryptionConfig", nullptr, Test_VideoDecoder_SetDecryptionConfig, nullptr,
189          nullptr, nullptr, napi_default, nullptr},
190         {"Test_AVDemuxer_GetMediaKeySystemInfo", nullptr, Test_AVDemuxer_GetMediaKeySystemInfo, nullptr,
191          nullptr, nullptr, napi_default, nullptr},
192         {"Test_AVDemuxer_SetMediaKeySystemInfoCallback", nullptr, Test_AVDemuxer_SetMediaKeySystemInfoCallback, nullptr,
193          nullptr, nullptr, napi_default, nullptr},
194     };
195     napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc);
196     return exports;
197 }
198 EXTERN_C_END
199 
200 static napi_module demoModule = {
201     .nm_version = 1,
202     .nm_flags = 0,
203     .nm_filename = nullptr,
204     .nm_register_func = Init,
205     .nm_modname = "entry",
206     .nm_priv = ((void *)0),
207     .reserved = {0},
208 };
209 
RegisterEntryModule(void)210 extern "C" __attribute__((constructor)) void RegisterEntryModule(void) { napi_module_register(&demoModule); }
211