1 /*
2 * Copyright (c) 2023 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include <mutex>
17 #include <map>
18 #include <shared_mutex>
19 #include <string>
20 #include <refbase.h>
21 #include <securec.h>
22 #include "drm_log.h"
23 #include "drm_trace.h"
24 #include "native_drm_common.h"
25 #include "native_drm_base.h"
26 #include "native_drm_object.h"
27 #include "native_mediakeysystem.h"
28 #include "drm_api_operation.h"
29 #include "media_key_system_factory_impl.h"
30
31 using namespace OHOS::DrmStandard;
32
OH_MediaKeySystem_IsSupported(const char * uuid)33 bool OH_MediaKeySystem_IsSupported(const char *uuid)
34 {
35 DRM_INFO_LOG("OH_MediaKeySystem_IsSupported enter.");
36 DRM_CHECK_AND_RETURN_RET_LOG(uuid != nullptr, false, "MediaKeySystem uuid is nullptr!");
37 std::string uuidPtr(uuid);
38 bool isSupported = false;
39 DRM_CHECK_AND_RETURN_RET_LOG(uuidPtr.size() != 0, false, "MediaKeySystem uuidPtr.size is nullptr!");
40 OHOS::sptr<MediaKeySystemFactoryImpl> fatory = MediaKeySystemFactoryImpl::GetInstance();
41 isSupported = fatory->IsMediaKeySystemSupported(uuidPtr);
42 return isSupported;
43 }
44
OH_MediaKeySystem_IsSupported2(const char * uuid,const char * mimeType)45 bool OH_MediaKeySystem_IsSupported2(const char *uuid, const char *mimeType)
46 {
47 DRM_INFO_LOG("OH_MediaKeySystem_IsSupported2 enter.");
48 DRM_CHECK_AND_RETURN_RET_LOG(((uuid != nullptr) && (mimeType != nullptr)), false,
49 "OH_MediaKeySystem_IsSupported2 uuid is nullptr!");
50 bool isSupported = false;
51 std::string uuidPtr(uuid);
52 DRM_CHECK_AND_RETURN_RET_LOG(uuidPtr.size() != 0, false, "MediaKeySystem uuidPtr.size is nullptr!");
53 std::string mimeTypePtr = std::string(mimeType);
54 DRM_CHECK_AND_RETURN_RET_LOG(mimeTypePtr.size() != 0, false, "MediaKeySystem mimeTypePtr.size is nullptr!");
55
56 OHOS::sptr<MediaKeySystemFactoryImpl> fatory = MediaKeySystemFactoryImpl::GetInstance();
57 isSupported = fatory->IsMediaKeySystemSupported(uuidPtr, mimeTypePtr);
58 return isSupported;
59 }
60
OH_MediaKeySystem_IsSupported3(const char * uuid,const char * mimeType,DRM_ContentProtectionLevel ContentProtectionLevel)61 bool OH_MediaKeySystem_IsSupported3(const char *uuid, const char *mimeType,
62 DRM_ContentProtectionLevel ContentProtectionLevel)
63 {
64 DRM_INFO_LOG("OH_MediaKeySystem_IsSupported3 enter.");
65 DRM_CHECK_AND_RETURN_RET_LOG(((uuid != nullptr) && (mimeType != nullptr)), false,
66 "OH_MediaKeySystem_IsSupported3 uuid is nullptr!");
67 bool isSupported = false;
68 std::string uuidPtr(uuid);
69 DRM_CHECK_AND_RETURN_RET_LOG(uuidPtr.size() != 0, false, "MediaKeySystem Uuid.size is nullptr!");
70 std::string mimeTypePtr = std::string(mimeType);
71 DRM_CHECK_AND_RETURN_RET_LOG(mimeTypePtr.size() != 0, false, "MediaKeySystem mimeTypePtr.size is nullptr!");
72
73 OHOS::sptr<MediaKeySystemFactoryImpl> fatory = MediaKeySystemFactoryImpl::GetInstance();
74
75 IMediaKeySessionService::ContentProtectionLevel securityLevel =
76 (IMediaKeySessionService::ContentProtectionLevel)ContentProtectionLevel;
77 if ((securityLevel <= IMediaKeySessionService::CONTENT_PROTECTION_LEVEL_UNKNOWN) ||
78 (securityLevel >= IMediaKeySessionService::CONTENT_PROTECTION_LEVEL_MAX)) {
79 DRM_ERR_LOG("ContentProtectionLevel is invalid");
80 return false;
81 }
82 isSupported = fatory->IsMediaKeySystemSupported(uuidPtr, mimeTypePtr, securityLevel);
83 return isSupported;
84 }
85
OH_MediaKeySystem_GetMediaKeySystems(DRM_MediaKeySystemDescription * description,uint32_t * count)86 Drm_ErrCode OH_MediaKeySystem_GetMediaKeySystems(DRM_MediaKeySystemDescription *description, uint32_t *count)
87 {
88 DRM_INFO_LOG("OH_MediaKeySystem_GetMediaKeySystems enter");
89 DRM_CHECK_AND_RETURN_RET_LOG((description != nullptr), DRM_ERR_INVALID_VAL, "description is nullptr");
90 DRM_CHECK_AND_RETURN_RET_LOG((count != nullptr), DRM_ERR_INVALID_VAL, "count is nullptr");
91 std::map<std::string, std::string> keySystemNames;
92 OHOS::sptr<MediaKeySystemFactoryImpl> fatory = MediaKeySystemFactoryImpl::GetInstance();
93 int32_t ret = fatory->GetMediaKeySystems(keySystemNames);
94 DRM_CHECK_AND_RETURN_RET_LOG((*count >= keySystemNames.size()), DRM_ERR_INVALID_VAL,
95 "GetMediaKeySystems failed because the count passed by is too small.");
96 int32_t times = 0;
97 DRM_CHECK_AND_RETURN_RET_LOG((ret == DRM_ERR_OK), DRM_ERR_UNKNOWN,
98 "GetMediaKeySystems call Failed!");
99 for (auto it = keySystemNames.begin(); it != keySystemNames.end(); it++) {
100 if (it->first.size() != 0) {
101 ret = memcpy_s(description[times].name, sizeof(description[times].name), it->first.c_str(),
102 it->first.size());
103 if (ret != 0) {
104 DRM_ERR_LOG("OH_MediaKeySystem_GetMediaKeySystems memcpy_s description faild!");
105 return DRM_ERR_NO_MEMORY;
106 }
107 }
108 if (it->second.size() != 0) {
109 for (size_t i = 0; i < sizeof(description[times].uuid) * BASE_CONVERSION_OPERATOR;
110 i += BASE_CONVERSION_OPERATOR) {
111 std::string byteStr = it->second.substr(i, BASE_CONVERSION_OPERATOR);
112 uint8_t byte = static_cast<u_int8_t>(std::stoi(byteStr, nullptr, HEXADECIMAL));
113 description[times].uuid[i/BASE_CONVERSION_OPERATOR] = byte;
114 }
115 }
116 times++;
117 }
118 if (keySystemNames.size() == 0) {
119 DRM_ERR_LOG("plugin not exist.");
120 return DRM_ERR_UNKNOWN;
121 }
122 *count = keySystemNames.size();
123 return DRM_ERR_OK;
124 }
125
OH_MediaKeySystem_Create(const char * name,MediaKeySystem ** mediaKeySystem)126 Drm_ErrCode OH_MediaKeySystem_Create(const char *name, MediaKeySystem **mediaKeySystem)
127 {
128 DRM_INFO_LOG("OH_MediaKeySystem_Create enter.");
129 DrmTrace trace("OH_MediaKeySystem_Create");
130 std::map<int32_t, Drm_ErrCode> errCodeMaps = {
131 {401, DRM_ERR_INVALID_VAL},
132 {24700201, DRM_ERR_SERVICE_DIED},
133 {24700103, DRM_ERR_MAX_SYSTEM_NUM_REACHED},
134 {24700101, DRM_ERR_UNKNOWN},
135 {0, DRM_ERR_OK}
136 };
137 DRM_CHECK_AND_RETURN_RET_LOG(((name != nullptr) && (mediaKeySystem != nullptr)), DRM_ERR_INVALID_VAL,
138 "parameter is error!");
139 std::string nameStr = name;
140 DRM_CHECK_AND_RETURN_RET_LOG(nameStr.size() != 0, DRM_ERR_INVALID_VAL, "the size of nameStr is zero");
141
142 OHOS::sptr<MediaKeySystemFactoryImpl> factory = MediaKeySystemFactoryImpl::GetInstance();
143 DRM_CHECK_AND_RETURN_RET_LOG(factory != nullptr, DRM_ERR_UNKNOWN, "factory is nullptr!");
144 OHOS::sptr<OHOS::DrmStandard::MediaKeySystemImpl> system = nullptr;
145 int32_t result = factory->CreateMediaKeySystem(nameStr, &system);
146 Drm_ErrCode retCode = DRM_ERR_UNKNOWN;
147 if (errCodeMaps.find(result) != errCodeMaps.end()) {
148 retCode = errCodeMaps[result];
149 }
150 DRM_CHECK_AND_RETURN_RET_LOG(system != nullptr, retCode, "system create by name failed!");
151
152 struct MediaKeySystemObject *object = new (std::nothrow) MediaKeySystemObject(system);
153 DRM_CHECK_AND_RETURN_RET_LOG(object != nullptr, DRM_ERR_UNKNOWN, "MediaKeySystemObject create failed!");
154
155 object->systemCallback_ = new (std::nothrow) MediaKeySystemCallbackCapi();
156 if (object->systemCallback_ == nullptr) {
157 delete object;
158 DRM_ERR_LOG("MediaKeySystemObject create systemCallback failed!");
159 return DRM_ERR_UNKNOWN;
160 }
161 int32_t ret = object->systemImpl_->SetCallback(object->systemCallback_);
162 if (ret != DRM_ERR_OK) {
163 delete object;
164 DRM_ERR_LOG("system set callback failed!");
165 return DRM_ERR_UNKNOWN;
166 }
167
168 *mediaKeySystem = object;
169 return DRM_ERR_OK;
170 }
171
OH_MediaKeySystem_SetConfigurationString(MediaKeySystem * mediaKeySystem,const char * configName,const char * value)172 Drm_ErrCode OH_MediaKeySystem_SetConfigurationString(MediaKeySystem *mediaKeySystem, const char *configName,
173 const char *value)
174 {
175 DRM_INFO_LOG("OH_MediaKeySystem_SetConfigurationString enter.");
176 DRM_CHECK_AND_RETURN_RET_LOG(((mediaKeySystem != nullptr) && (configName != nullptr) && (value != nullptr)),
177 DRM_ERR_INVALID_VAL, "OH_MediaKeySystem_SetConfigurationString parameter is error!");
178
179 int32_t result = DRM_ERR_OK;
180 std::string name(configName);
181 DRM_CHECK_AND_RETURN_RET_LOG(name.size() != 0, DRM_ERR_INVALID_VAL,
182 "OH_MediaKeySystem_SetConfigurationString configName.size is not zero!");
183 std::string valuePtr(value);
184 DRM_CHECK_AND_RETURN_RET_LOG(valuePtr.size() != 0, DRM_ERR_INVALID_VAL,
185 "OH_MediaKeySystem_SetConfigurationString value.size is not zero!");
186
187 MediaKeySystemObject *systemObject = reinterpret_cast<MediaKeySystemObject *>(mediaKeySystem);
188 DRM_CHECK_AND_RETURN_RET_LOG(systemObject->systemImpl_ != nullptr, DRM_ERR_INVALID_VAL,
189 "OH_MediaKeySystem_SetConfigurationString inner systemImpl is nullptr!");
190 result = systemObject->systemImpl_->SetConfigurationString(name, valuePtr);
191 DRM_CHECK_AND_RETURN_RET_LOG(result == DRM_ERR_OK, DRM_ERR_UNKNOWN,
192 "OH_MediaKeySystem_SetConfigurationString mediaKeySystemImpl::SetConfigurationString faild!");
193 return DRM_ERR_OK;
194 }
195
OH_MediaKeySystem_GetConfigurationString(MediaKeySystem * mediaKeySystem,const char * configName,char * value,int32_t valueLen)196 Drm_ErrCode OH_MediaKeySystem_GetConfigurationString(MediaKeySystem *mediaKeySystem, const char *configName,
197 char *value, int32_t valueLen)
198 {
199 DRM_INFO_LOG("OH_MediaKeySystem_GetConfigurationString enter");
200 DRM_CHECK_AND_RETURN_RET_LOG(
201 ((mediaKeySystem != nullptr) && (configName != nullptr) && (value != nullptr) && (valueLen > 0)),
202 DRM_ERR_INVALID_VAL, "OH_MediaKeySystem_GetConfigurationString params is error!");
203
204 std::string valuePtr;
205 int32_t result = DRM_ERR_UNKNOWN;
206 std::string name = std::string(configName);
207 DRM_CHECK_AND_RETURN_RET_LOG(name.size() != 0, DRM_ERR_INVALID_VAL,
208 "configName.size is not zero!");
209 MediaKeySystemObject *systemObject = reinterpret_cast<MediaKeySystemObject *>(mediaKeySystem);
210 DRM_CHECK_AND_RETURN_RET_LOG(systemObject->systemImpl_ != nullptr, DRM_ERR_INVALID_VAL,
211 "OH_MediaKeySystem_GetConfigurationString faild!");
212 result = systemObject->systemImpl_->GetConfigurationString(name, valuePtr);
213 DRM_CHECK_AND_RETURN_RET_LOG(result == DRM_ERR_OK, DRM_ERR_UNKNOWN,
214 "mediaKeySystemImpl::GetConfigurationString faild!");
215 DRM_CHECK_AND_RETURN_RET_LOG(valueLen >= (int32_t)valuePtr.size(), DRM_ERR_INVALID_VAL,
216 "The space for value is too small");
217 memset_s(value, valueLen, 0, valueLen);
218 int32_t ret = memcpy_s(value, valuePtr.size(), valuePtr.c_str(), valuePtr.size());
219 if (ret != 0) {
220 DRM_ERR_LOG("OH_MediaKeySystem_GetConfigurationString memcpy_s value failed!");
221 return DRM_ERR_NO_MEMORY;
222 }
223 return DRM_ERR_OK;
224 }
225
OH_MediaKeySystem_SetConfigurationByteArray(MediaKeySystem * mediaKeySystem,const char * configName,uint8_t * value,int32_t valueLen)226 Drm_ErrCode OH_MediaKeySystem_SetConfigurationByteArray(MediaKeySystem *mediaKeySystem,
227 const char *configName, uint8_t *value, int32_t valueLen)
228
229 {
230 DRM_INFO_LOG("OH_MediaKeySystem_SetConfigurationByteArray enter.");
231 DRM_CHECK_AND_RETURN_RET_LOG(
232 ((mediaKeySystem != nullptr) && (configName != nullptr) && (value != nullptr) && (valueLen > 0)),
233 DRM_ERR_INVALID_VAL, "OH_MediaKeySystem_SetConfigurationByteArray params is error!");
234
235 int32_t result = DRM_ERR_OK;
236 std::string name(configName);
237 DRM_CHECK_AND_RETURN_RET_LOG(name.size() != 0, DRM_ERR_INVALID_VAL,
238 "OH_MediaKeySystem_SetConfigurationByteArray configName.size is not zero!");
239 uint8_t *valueDataPtr = reinterpret_cast<uint8_t *>(value);
240 DRM_CHECK_AND_RETURN_RET_LOG(valueDataPtr != nullptr, DRM_ERR_INVALID_VAL,
241 "OH_MediaKeySystem_SetConfigurationByteArray value is nullptr!");
242 std::vector<uint8_t> valueptr(valueDataPtr, valueDataPtr + valueLen);
243 DRM_CHECK_AND_RETURN_RET_LOG(valueptr.size() != 0, DRM_ERR_INVALID_VAL,
244 "OH_MediaKeySystem_SetConfigurationByteArray value.size is not zero!");
245
246 MediaKeySystemObject *systemObject = reinterpret_cast<MediaKeySystemObject *>(mediaKeySystem);
247 DRM_CHECK_AND_RETURN_RET_LOG(systemObject->systemImpl_ != nullptr, DRM_ERR_INVALID_VAL,
248 "mediaKeySystemImpl::SetConfigurationByteArray faild!");
249 result = systemObject->systemImpl_->SetConfigurationByteArray(name, valueptr);
250 DRM_CHECK_AND_RETURN_RET_LOG(result == DRM_ERR_OK, DRM_ERR_UNKNOWN,
251 "OH_MediaKeySystem_SetConfigurationByteArray mediaKeySystemImpl::SetConfigurationByteArray faild!");
252 return DRM_ERR_OK;
253 }
254
OH_MediaKeySystem_GetConfigurationByteArray(MediaKeySystem * mediaKeySystem,const char * configName,uint8_t * value,int32_t * valueLen)255 Drm_ErrCode OH_MediaKeySystem_GetConfigurationByteArray(MediaKeySystem *mediaKeySystem,
256 const char *configName, uint8_t *value, int32_t *valueLen)
257 {
258 DRM_INFO_LOG("OH_MediaKeySystem_GetConfigurationByteArray enter");
259 DRM_CHECK_AND_RETURN_RET_LOG(
260 ((mediaKeySystem != nullptr) && (configName != nullptr) && (value != nullptr) && (valueLen != nullptr)),
261 DRM_ERR_INVALID_VAL, "OH_MediaKeySystem_GetConfigurationByteArray parameter is error!");
262
263 std::vector<uint8_t> valuePtr;
264 int32_t result = DRM_ERR_OK;
265 std::string name = std::string(configName);
266 DRM_CHECK_AND_RETURN_RET_LOG(name.size() != 0, DRM_ERR_INVALID_VAL,
267 "configName.size is not zero!");
268
269 MediaKeySystemObject *systemObject = reinterpret_cast<MediaKeySystemObject *>(mediaKeySystem);
270 DRM_CHECK_AND_RETURN_RET_LOG(systemObject->systemImpl_ != nullptr, DRM_ERR_INVALID_VAL,
271 "OH_MediaKeySystem_GetConfigurationByteArray faild!");
272 result = systemObject->systemImpl_->GetConfigurationByteArray(name, valuePtr);
273 DRM_CHECK_AND_RETURN_RET_LOG(result == DRM_ERR_OK, DRM_ERR_UNKNOWN,
274 "mediaKeySystemImpl::GetConfigurationByteArray faild!");
275 DRM_CHECK_AND_RETURN_RET_LOG(*valueLen >= (int32_t)valuePtr.size(), DRM_ERR_INVALID_VAL,
276 "The space for value is too small!");
277 *valueLen = valuePtr.size();
278 int32_t ret = memcpy_s(value, valuePtr.size(), valuePtr.data(), valuePtr.size());
279 if (ret != 0) {
280 DRM_ERR_LOG("OH_MediaKeySystem_GetConfigurationByteArray memcpy_s value faild!");
281 return DRM_ERR_NO_MEMORY;
282 }
283 return DRM_ERR_OK;
284 }
285
vectorToClist(std::vector<IMediaKeySystemService::MetircKeyValue> & metrics,DRM_Statistics * statistics)286 static Drm_ErrCode vectorToClist(std::vector<IMediaKeySystemService::MetircKeyValue> &metrics,
287 DRM_Statistics *statistics)
288 {
289 DRM_INFO_LOG("vectorToCArray start.");
290 memset_s(statistics, sizeof(DRM_Statistics), 0, sizeof(DRM_Statistics));
291 statistics->statisticsCount = metrics.size();
292 DRM_CHECK_AND_RETURN_RET_LOG((statistics->statisticsCount <= MAX_STATISTICS_COUNT), DRM_ERR_NO_MEMORY,
293 "statisticsCount err!");
294 for (size_t i = 0; i < metrics.size(); i++) {
295 if (metrics[i].name.size() == 0 || metrics[i].name.size() > sizeof(statistics->statisticsName[i])) {
296 continue;
297 }
298 int32_t ret = memcpy_s(statistics->statisticsName[i],
299 sizeof(statistics->statisticsName[i]), metrics[i].name.c_str(), metrics[i].name.size());
300 if (ret != 0) {
301 DRM_ERR_LOG(" memcpy_s faild!");
302 return DRM_ERR_NO_MEMORY;
303 }
304 if (metrics[i].value.size() != 0) {
305 ret = memcpy_s(statistics->statisticsDescription[i],
306 sizeof(statistics->statisticsDescription[i]), metrics[i].value.c_str(), metrics[i].value.size());
307 if (ret != 0) {
308 DRM_ERR_LOG(" memcpy_s faild!");
309 return DRM_ERR_NO_MEMORY;
310 }
311 }
312 }
313 return DRM_ERR_OK;
314 }
315
OH_MediaKeySystem_GetStatistics(MediaKeySystem * mediaKeySystem,DRM_Statistics * statistics)316 Drm_ErrCode OH_MediaKeySystem_GetStatistics(MediaKeySystem *mediaKeySystem, DRM_Statistics *statistics)
317 {
318 DRM_INFO_LOG("OH_MediaKeySystem_GetStatistics enter.");
319 DRM_CHECK_AND_RETURN_RET_LOG(((mediaKeySystem != nullptr) && (statistics != nullptr)), DRM_ERR_INVALID_VAL,
320 "OH_MediaKeySystem_GetStatistics params is error!");
321 MediaKeySystemObject *systemObject = reinterpret_cast<MediaKeySystemObject *>(mediaKeySystem);
322 std::vector<IMediaKeySystemService::MetircKeyValue> metrics;
323 DRM_CHECK_AND_RETURN_RET_LOG(systemObject->systemImpl_ != nullptr, DRM_ERR_INVALID_VAL,
324 "mediaKeySystemImpl::GetStatistics inner systemImpl is nullptr!");
325 int32_t result = systemObject->systemImpl_->GetStatistics(metrics);
326 DRM_CHECK_AND_RETURN_RET_LOG(result == DRM_ERR_OK, DRM_ERR_UNKNOWN,
327 "OH_MediaKeySystem_GetStatistics systemObject is nullptr!");
328 Drm_ErrCode ret = vectorToClist(metrics, statistics);
329 DRM_CHECK_AND_RETURN_RET_LOG(statistics != nullptr, DRM_ERR_UNKNOWN,
330 "OH_MediaKeySystem_GetStatistics statistics obtained is nullptr!");
331 return ret;
332 }
333
OH_MediaKeySystem_GetMaxContentProtectionLevel(MediaKeySystem * mediaKeySystem,DRM_ContentProtectionLevel * contentProtectionLevel)334 Drm_ErrCode OH_MediaKeySystem_GetMaxContentProtectionLevel(MediaKeySystem *mediaKeySystem,
335 DRM_ContentProtectionLevel *contentProtectionLevel)
336 {
337 DRM_INFO_LOG("GetMaxContentProtectionLevel enter.");
338 DRM_CHECK_AND_RETURN_RET_LOG(((mediaKeySystem != nullptr) && (contentProtectionLevel != nullptr)),
339 DRM_ERR_INVALID_VAL, "OH_MediaKeySystem_GetMaxContentProtectionLevel parameter is error!");
340 MediaKeySystemObject *systemObject = reinterpret_cast<MediaKeySystemObject *>(mediaKeySystem);
341 DRM_CHECK_AND_RETURN_RET_LOG(systemObject->systemImpl_ != nullptr, DRM_ERR_INVALID_VAL,
342 "mediaKeySystemImpl::GetMaxContentProtectionLevel faild!");
343 int32_t result = DRM_ERR_OK;
344 IMediaKeySessionService::ContentProtectionLevel level =
345 IMediaKeySessionService::CONTENT_PROTECTION_LEVEL_UNKNOWN;
346 result = systemObject->systemImpl_->GetMaxContentProtectionLevel(&level);
347 DRM_CHECK_AND_RETURN_RET_LOG(result == DRM_ERR_OK, DRM_ERR_UNKNOWN,
348 "OH_MediaKeySystem_GetMaxContentProtectionLevel fail!");
349 if (level <= IMediaKeySessionService::CONTENT_PROTECTION_LEVEL_UNKNOWN ||
350 level >= IMediaKeySessionService::CONTENT_PROTECTION_LEVEL_MAX) {
351 DRM_ERR_LOG("the level obtained is beyond reasonable range!");
352 return DRM_ERR_UNKNOWN;
353 }
354 *contentProtectionLevel = static_cast<DRM_ContentProtectionLevel>(level);
355 return DRM_ERR_OK;
356 }
357
OH_MediaKeySystem_GenerateKeySystemRequest(MediaKeySystem * mediaKeySystem,uint8_t * request,int32_t * requestLen,char * defaultUrl,int32_t defaultUrlLen)358 Drm_ErrCode OH_MediaKeySystem_GenerateKeySystemRequest(MediaKeySystem *mediaKeySystem, uint8_t *request,
359 int32_t *requestLen, char *defaultUrl, int32_t defaultUrlLen)
360 {
361 DRM_INFO_LOG("OH_MediaKeySystem_GenerateKeySystemRequest enter");
362 DrmTrace trace("OH_MediaKeySystem_GenerateKeySystemRequest");
363 DRM_CHECK_AND_RETURN_RET_LOG(((mediaKeySystem != nullptr) && (request != nullptr) && (requestLen != nullptr) &&
364 (*requestLen > 0) && (defaultUrl != nullptr) && (defaultUrlLen > 0)),
365 DRM_ERR_INVALID_VAL, "Incorrect parameters of OH_MediaKeySystem_GenerateKeySystemRequest!");
366 std::vector<uint8_t> requestData;
367 std::string defaultUrlData;
368
369 MediaKeySystemObject *systemObject = reinterpret_cast<MediaKeySystemObject *>(mediaKeySystem);
370 DRM_CHECK_AND_RETURN_RET_LOG(systemObject->systemImpl_ != nullptr, DRM_ERR_INVALID_VAL,
371 "mediaKeySystemImpl::GenerateKeySystemRequest faild!");
372 int32_t result = systemObject->systemImpl_->GenerateKeySystemRequest(requestData, defaultUrlData);
373 DRM_CHECK_AND_RETURN_RET_LOG(((result == DRM_ERR_OK) && (requestData.size() != 0)), DRM_ERR_UNKNOWN,
374 "MediaKeySystemImpl GenerateKeySystemRequest failed!");
375 int32_t ret = memcpy_s(request, *requestLen, requestData.data(), requestData.size());
376 DRM_CHECK_AND_RETURN_RET_LOG(ret == 0, DRM_ERR_NO_MEMORY,
377 "OH_MediaKeySystem_GenerateKeySystemRequest memcpy_s request failed!");
378 *requestLen = requestData.size();
379 ret = memset_s(defaultUrl, defaultUrlLen, 0, defaultUrlLen);
380 DRM_CHECK_AND_RETURN_RET_LOG(ret == 0, DRM_ERR_NO_MEMORY,
381 "OH_MediaKeySystem_GenerateKeySystemRequest memset_s defaultUrl failed!");
382 if (defaultUrlData.size() != 0) {
383 ret = memcpy_s(defaultUrl, defaultUrlLen, defaultUrlData.data(), defaultUrlData.size());
384 DRM_CHECK_AND_RETURN_RET_LOG(ret == 0, DRM_ERR_NO_MEMORY,
385 "OH_MediaKeySystem_GenerateKeySystemRequest memcpy_s defaultUrl failed!");
386 }
387 return DRM_ERR_OK;
388 }
389
OH_MediaKeySystem_ProcessKeySystemResponse(MediaKeySystem * mediaKeySystem,uint8_t * response,int32_t responseLen)390 Drm_ErrCode OH_MediaKeySystem_ProcessKeySystemResponse(MediaKeySystem *mediaKeySystem,
391 uint8_t *response, int32_t responseLen)
392 {
393 DRM_INFO_LOG("OH_MediaKeySystem_ProcessKeySystemResponse enter.");
394 int64_t beginTime = std::chrono::duration_cast<std::chrono::milliseconds>(
395 std::chrono::system_clock::now().time_since_epoch()).count();
396 DrmTrace trace("OH_MediaKeySystem_ProcessKeySystemResponse");
397 DRM_CHECK_AND_RETURN_RET_LOG(((mediaKeySystem != nullptr) && (response != nullptr) && (responseLen > 0)),
398 DRM_ERR_INVALID_VAL, "OH_MediaKeySystem_ProcessKeySystemResponse parameter is error!");
399 int32_t result = DRM_ERR_OK;
400 std::vector<uint8_t> keySystemResponse(response, response + responseLen);
401 MediaKeySystemObject *systemObject = reinterpret_cast<MediaKeySystemObject *>(mediaKeySystem);
402 DRM_CHECK_AND_RETURN_RET_LOG(systemObject->systemImpl_ != nullptr, DRM_ERR_INVALID_VAL,
403 "mediaKeySystemImpl::ProcessKeySystemResponse faild!");
404 result = systemObject->systemImpl_->ProcessKeySystemResponse(keySystemResponse);
405 DRM_CHECK_AND_RETURN_RET_LOG(result == DRM_ERR_OK, DRM_ERR_UNKNOWN,
406 "OH_MediaKeySystem_ProcessKeySystemResponse systemObject is nullptr!");
407 ConfigParser::WriteEndEvent(0, 0, std::string("OH_MediaKeySystem_ProcessKeySystemResponse"), beginTime);
408 return DRM_ERR_OK;
409 }
410
OH_MediaKeySystem_GetCertificateStatus(MediaKeySystem * mediaKeySystem,DRM_CertificateStatus * certStatus)411 Drm_ErrCode OH_MediaKeySystem_GetCertificateStatus(MediaKeySystem *mediaKeySystem, DRM_CertificateStatus *certStatus)
412 {
413 DRM_INFO_LOG("OH_MediaKeySystem_GetCertificateStatus enter.");
414 DRM_CHECK_AND_RETURN_RET_LOG(((mediaKeySystem != nullptr) && (certStatus != nullptr)), DRM_ERR_INVALID_VAL,
415 "OH_MediaKeySystem_GetCertificateStatus parameter is error!");
416 IMediaKeySystemService::CertificateStatus CertStatus;
417 MediaKeySystemObject *systemObject = reinterpret_cast<MediaKeySystemObject *>(mediaKeySystem);
418 DRM_CHECK_AND_RETURN_RET_LOG(systemObject->systemImpl_ != nullptr, DRM_ERR_INVALID_VAL,
419 "mediaKeySystemImpl::GetCertificateStatus faild!");
420 int32_t result = systemObject->systemImpl_->GetCertificateStatus(&CertStatus);
421 if (result != DRM_ERR_OK) {
422 *certStatus = CERT_STATUS_UNAVAILABLE;
423 DRM_ERR_LOG("OH_MediaKeySystem_GetCertificateStatus faild!");
424 return DRM_ERR_UNKNOWN;
425 }
426 *certStatus = (DRM_CertificateStatus)((int32_t)(CertStatus));
427 return DRM_ERR_OK;
428 }
429
OH_MediaKeySystem_SetMediaKeySystemCallback(MediaKeySystem * mediaKeySystem,MediaKeySystem_Callback callback)430 Drm_ErrCode OH_MediaKeySystem_SetMediaKeySystemCallback(MediaKeySystem *mediaKeySystem,
431 MediaKeySystem_Callback callback)
432 {
433 DRM_INFO_LOG("OH_MediaKeySystem_SetMediaKeySystemCallback enter.");
434 DRM_CHECK_AND_RETURN_RET_LOG(((mediaKeySystem != nullptr) && (callback != nullptr)), DRM_ERR_INVALID_VAL,
435 "parameter is error!");
436 MediaKeySystemObject *systemObject = reinterpret_cast<MediaKeySystemObject *>(mediaKeySystem);
437 DRM_CHECK_AND_RETURN_RET_LOG(systemObject->systemImpl_ != nullptr, DRM_ERR_INVALID_VAL,
438 "OH_MediaKeySystem_SetMediaKeySystemCallback faild!");
439 systemObject->systemCallback_->SetCallbackReference(callback);
440 return DRM_ERR_OK;
441 }
442
OH_MediaKeySystem_SetCallback(MediaKeySystem * mediaKeySystem,OH_MediaKeySystem_Callback callback)443 Drm_ErrCode OH_MediaKeySystem_SetCallback(MediaKeySystem *mediaKeySystem, OH_MediaKeySystem_Callback callback)
444 {
445 DRM_INFO_LOG("OH_MediaKeySystem_SetCallback enter.");
446 DRM_CHECK_AND_RETURN_RET_LOG(((mediaKeySystem != nullptr) && (callback != nullptr)), DRM_ERR_INVALID_VAL,
447 "parameter is error!");
448 MediaKeySystemObject *systemObject = reinterpret_cast<MediaKeySystemObject *>(mediaKeySystem);
449 DRM_CHECK_AND_RETURN_RET_LOG(systemObject->systemImpl_ != nullptr, DRM_ERR_INVALID_VAL,
450 "OH_MediaKeySystem_SetCallback faild!");
451 systemObject->systemCallback_->SetCallbackReference(mediaKeySystem, callback);
452 return DRM_ERR_OK;
453 }
454
OH_MediaKeySystem_CreateMediaKeySession(MediaKeySystem * mediaKeySystem,DRM_ContentProtectionLevel * level,MediaKeySession ** mediaKeySession)455 Drm_ErrCode OH_MediaKeySystem_CreateMediaKeySession(MediaKeySystem *mediaKeySystem, DRM_ContentProtectionLevel *level,
456 MediaKeySession **mediaKeySession)
457 {
458 DRM_INFO_LOG("OH_MediaKeySystem_CreateMediaKeySession enter.");
459 DrmTrace trace("OH_MediaKeySystem_CreateMediaKeySession");
460 std::map<int32_t, Drm_ErrCode> errCodeMaps = {
461 {24700201, DRM_ERR_SERVICE_DIED},
462 {24700104, DRM_ERR_MAX_SESSION_NUM_REACHED},
463 {24700101, DRM_ERR_UNKNOWN},
464 {0, DRM_ERR_OK}
465 };
466 DRM_CHECK_AND_RETURN_RET_LOG(((mediaKeySystem != nullptr) && (level != nullptr) && (mediaKeySession != nullptr) &&
467 (*level > CONTENT_PROTECTION_LEVEL_UNKNOWN) && (*level < CONTENT_PROTECTION_LEVEL_MAX)),
468 DRM_ERR_INVALID_VAL, "mediaKeySystem is nullptr!");
469 struct MediaKeySystemObject *systemObject = reinterpret_cast<MediaKeySystemObject *>(mediaKeySystem);
470 DRM_CHECK_AND_RETURN_RET_LOG(systemObject->systemImpl_ != nullptr, DRM_ERR_INVALID_VAL,
471 "mediaKeySystemImpl::CreateMediaKeySession faild!");
472 int32_t secure = static_cast<int32_t>(*level);
473 IMediaKeySessionService::ContentProtectionLevel secureLevel =
474 static_cast<IMediaKeySessionService::ContentProtectionLevel>(secure);
475 OHOS::sptr<MediaKeySessionImpl> keySessionImpl = nullptr;
476 int32_t ret = systemObject->systemImpl_->CreateMediaKeySession(secureLevel, &keySessionImpl);
477 Drm_ErrCode retCode = DRM_ERR_UNKNOWN;
478 if (errCodeMaps.find(ret) != errCodeMaps.end()) {
479 retCode = errCodeMaps[ret];
480 }
481 DRM_CHECK_AND_RETURN_RET_LOG(ret == DRM_ERR_OK, retCode, "session create return failed!");
482 DRM_CHECK_AND_RETURN_RET_LOG(keySessionImpl != nullptr, DRM_ERR_INVALID_VAL, "session create failed!");
483
484 struct MediaKeySessionObject *sessionObject = new (std::nothrow) MediaKeySessionObject(keySessionImpl);
485 DRM_CHECK_AND_RETURN_RET_LOG(sessionObject != nullptr, DRM_ERR_NO_MEMORY, "MediaKeySessionObject create failed!");
486
487 sessionObject->sessionCallback_ = new (std::nothrow) MediaKeySessionCallbackCapi();
488 if (sessionObject->sessionCallback_ == nullptr) {
489 delete sessionObject;
490 DRM_ERR_LOG("MediaKeySessionObject create sessionCallback failed!");
491 return DRM_ERR_NO_MEMORY;
492 }
493 ret = sessionObject->sessionImpl_->SetCallback(sessionObject->sessionCallback_);
494 if (ret != DRM_ERR_OK) {
495 delete sessionObject;
496 DRM_ERR_LOG("session set callback failed!");
497 return DRM_ERR_UNKNOWN;
498 }
499
500 *mediaKeySession = static_cast<MediaKeySession *>(sessionObject);
501 return DRM_ERR_OK;
502 }
503
vectorToC2DArray(std::vector<std::vector<uint8_t>> licenseIds,DRM_OfflineMediakeyIdArray * offlineMediaKeyIds)504 static Drm_ErrCode vectorToC2DArray(std::vector<std::vector<uint8_t>> licenseIds,
505 DRM_OfflineMediakeyIdArray *offlineMediaKeyIds)
506 {
507 DRM_INFO_LOG("vectorToC2DArray enter.");
508 if (licenseIds.size() >= MAX_OFFLINE_MEDIA_KEY_ID_COUNT) {
509 DRM_ERR_LOG("licenseIds size too large!");
510 return DRM_ERR_NO_MEMORY;
511 }
512
513 offlineMediaKeyIds->idsCount = (uint32_t)(licenseIds.size());
514 for (size_t i = 0; i < licenseIds.size(); i++) {
515 if (licenseIds[i].size() != 0) {
516 offlineMediaKeyIds->idsLen[i] = (int32_t)(licenseIds[i].size());
517 int32_t ret = memcpy_s(offlineMediaKeyIds->ids[i], MAX_OFFLINE_MEDIA_KEY_ID_LEN, licenseIds[i].data(),
518 licenseIds[i].size());
519 if (ret != 0) {
520 DRM_ERR_LOG("memcpy_s faild!");
521 return DRM_ERR_NO_MEMORY;
522 }
523 }
524 }
525 return DRM_ERR_OK;
526 }
527
OH_MediaKeySystem_GetOfflineMediaKeyIds(MediaKeySystem * mediaKeySystem,DRM_OfflineMediakeyIdArray * offlineMediaKeyIds)528 Drm_ErrCode OH_MediaKeySystem_GetOfflineMediaKeyIds(MediaKeySystem *mediaKeySystem,
529 DRM_OfflineMediakeyIdArray *offlineMediaKeyIds)
530 {
531 DRM_INFO_LOG("OH_MediaKeySystem_GetOfflineMediaKeyIds enter.");
532 DRM_CHECK_AND_RETURN_RET_LOG(((mediaKeySystem != nullptr) && (offlineMediaKeyIds != nullptr)), DRM_ERR_INVALID_VAL,
533 "OH_MediaKeySystem_GetOfflineMediaKeyIds parameter is error!");
534 std::vector<std::vector<uint8_t>> licenseIds;
535
536 MediaKeySystemObject *systemObject = reinterpret_cast<MediaKeySystemObject *>(mediaKeySystem);
537 DRM_CHECK_AND_RETURN_RET_LOG(systemObject->systemImpl_ != nullptr, DRM_ERR_INVALID_VAL,
538 "mediaKeySystemImpl::GetOfflineMediaKeyIds faild!");
539 int32_t result = systemObject->systemImpl_->GetOfflineMediaKeyIds(licenseIds);
540 DRM_CHECK_AND_RETURN_RET_LOG(result == DRM_ERR_OK, DRM_ERR_UNKNOWN,
541 "OH_MediaKeySystem_GetOfflineMediaKeyIds faild!");
542 if (licenseIds.size() == 0) {
543 DRM_DEBUG_LOG("licenseIds.data() is nullptr!");
544 return DRM_ERR_OK;
545 }
546 result = vectorToC2DArray(licenseIds, offlineMediaKeyIds);
547 DRM_CHECK_AND_RETURN_RET_LOG(result == DRM_ERR_OK, DRM_ERR_NO_MEMORY,
548 "vectorToC2DArray faild!");
549 return DRM_ERR_OK;
550 }
551
OH_MediaKeySystem_GetOfflineMediaKeyStatus(MediaKeySystem * mediaKeySystem,uint8_t * offlineMediaKeyId,int32_t offlineMediaKeyIdLen,DRM_OfflineMediaKeyStatus * status)552 Drm_ErrCode OH_MediaKeySystem_GetOfflineMediaKeyStatus(MediaKeySystem *mediaKeySystem,
553 uint8_t *offlineMediaKeyId, int32_t offlineMediaKeyIdLen, DRM_OfflineMediaKeyStatus *status)
554 {
555 DRM_INFO_LOG("OH_MediaKeySystem_GetOfflineMediaKeyStatus enter");
556 DRM_CHECK_AND_RETURN_RET_LOG(((mediaKeySystem != nullptr) && (offlineMediaKeyId != nullptr) &&
557 (offlineMediaKeyIdLen > 0) && (status != nullptr)),
558 DRM_ERR_INVALID_VAL, "OH_MediaKeySystem_GetOfflineMediaKeyStatus parameter is error!");
559 int32_t result = OFFLINE_MEDIA_KEY_STATUS_UNKNOWN;
560
561 std::vector<uint8_t> licenseIdVec(offlineMediaKeyId, offlineMediaKeyId + offlineMediaKeyIdLen);
562 IMediaKeySessionService::OfflineMediaKeyStatus offlineMediaKeyStatus =
563 IMediaKeySessionService::OFFLINELICENSESTATUS_UNKNOWN;
564
565 MediaKeySystemObject *systemObject = reinterpret_cast<MediaKeySystemObject *>(mediaKeySystem);
566 DRM_CHECK_AND_RETURN_RET_LOG(systemObject->systemImpl_ != nullptr, DRM_ERR_INVALID_VAL,
567 "mediaKeySystemImpl::GetOfflineMediaKeyStatus faild!");
568 result = systemObject->systemImpl_->GetOfflineMediaKeyStatus(licenseIdVec, offlineMediaKeyStatus);
569 if (result != DRM_ERR_OK) {
570 DRM_ERR_LOG("OH_MediaKeySystem_GetOfflineMediaKeyStatus faild!");
571 return DRM_ERR_UNKNOWN;
572 }
573 DRM_OfflineMediaKeyStatus CofflineMediaKeyStatus = (DRM_OfflineMediaKeyStatus)((int32_t)(offlineMediaKeyStatus));
574 if (CofflineMediaKeyStatus < OFFLINE_MEDIA_KEY_STATUS_UNKNOWN ||
575 CofflineMediaKeyStatus > OFFLINE_MEDIA_KEY_STATUS_INACTIVE) {
576 DRM_ERR_LOG("OH_MediaKeySystem_GetOfflineMediaKeyStatus faild!");
577 return DRM_ERR_UNKNOWN;
578 }
579 *status = CofflineMediaKeyStatus;
580 return DRM_ERR_OK;
581 }
582
OH_MediaKeySystem_ClearOfflineMediaKeys(MediaKeySystem * mediaKeySystem,uint8_t * offlineMediaKeyId,int32_t offlineMediaKeyIdLen)583 Drm_ErrCode OH_MediaKeySystem_ClearOfflineMediaKeys(MediaKeySystem *mediaKeySystem,
584 uint8_t *offlineMediaKeyId, int32_t offlineMediaKeyIdLen)
585 {
586 DRM_INFO_LOG("OH_MediaKeySystem_ClearOfflineMediaKeys enter.");
587 DRM_CHECK_AND_RETURN_RET_LOG(((mediaKeySystem != nullptr) && (offlineMediaKeyId != nullptr) &&
588 (offlineMediaKeyIdLen > 0)),
589 DRM_ERR_INVALID_VAL, "OH_MediaKeySystem_ClearOfflineMediaKeys parameter is error!");
590 int32_t result = DRM_ERR_OK;
591 std::vector<uint8_t> licenseIdVec(offlineMediaKeyId, offlineMediaKeyId + offlineMediaKeyIdLen);
592 DRM_CHECK_AND_RETURN_RET_LOG(licenseIdVec.size() != 0, DRM_ERR_INVALID_VAL,
593 "OH_MediaKeySystem_ClearOfflineMediaKeys configName.size is not zero!");
594 MediaKeySystemObject *systemObject = reinterpret_cast<MediaKeySystemObject *>(mediaKeySystem);
595 DRM_CHECK_AND_RETURN_RET_LOG(systemObject->systemImpl_ != nullptr, DRM_ERR_INVALID_VAL,
596 "mediaKeySystemImpl::OH_MediaKeySystem_ClearOfflineMediaKeys inner systemImpl is nullptr!");
597 result = systemObject->systemImpl_->ClearOfflineMediaKeys(licenseIdVec);
598 DRM_CHECK_AND_RETURN_RET_LOG(result == DRM_ERR_OK, DRM_ERR_UNKNOWN,
599 "OH_MediaKeySystem_ClearOfflineMediaKeys mediaKeySystemImpl::ClearOfflineMediaKeys faild!");
600 return DRM_ERR_OK;
601 }
602
OH_MediaKeySystem_Destroy(MediaKeySystem * mediaKeySystem)603 Drm_ErrCode OH_MediaKeySystem_Destroy(MediaKeySystem *mediaKeySystem)
604 {
605 DRM_INFO_LOG("OH_MediaKeySystem_Destroy enter.");
606 DRM_CHECK_AND_RETURN_RET_LOG(mediaKeySystem != nullptr, DRM_ERR_INVALID_VAL, "mediaKeySystem is nullptr!");
607
608 struct MediaKeySystemObject *systemObject = reinterpret_cast<MediaKeySystemObject *>(mediaKeySystem);
609 DRM_CHECK_AND_RETURN_RET_LOG(systemObject->systemImpl_ != nullptr, DRM_ERR_INVALID_VAL,
610 "mediaKeySystemImpl::OH_MediaKeySystem_Destroy faild!");
611 int32_t ret = systemObject->systemImpl_->Release();
612 DRM_CHECK_AND_RETURN_RET_LOG(ret == DRM_ERR_OK, DRM_ERR_UNKNOWN, "call media key system release failed!");
613 delete mediaKeySystem;
614 mediaKeySystem = nullptr;
615 return DRM_ERR_OK;
616 }