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 <hdf_base.h>
17 #include <hdf_log.h>
18 #include "v1_0/media_key_system_factory_service.h"
19 #include "v1_0/media_key_system_service.h"
20 #include "clearplay_uuid.h"
21 #include "mime_type.h"
22 
23 #define HDF_LOG_TAG media_key_system_factory_service
24 
25 namespace OHOS {
26 namespace HDI {
27 namespace Drm {
28 namespace V1_0 {
MediaKeySystemFactoryImplGetInstance(void)29 extern "C" IMediaKeySystemFactory *MediaKeySystemFactoryImplGetInstance(void)
30 {
31     HDF_LOGI("%{public}s: start", __func__);
32     HDF_LOGI("%{public}s: end", __func__);
33     return new (std::nothrow) MediaKeySystemFactoryService();
34 }
35 
~MediaKeySystemFactoryService()36 MediaKeySystemFactoryService::~MediaKeySystemFactoryService()
37 {
38     HDF_LOGI("%{public}s: start", __func__);
39     mediaKeySystemMutex_.lock();
40     while (mediaKeySystemMap_.size() > 0) {
41         sptr<OHOS::HDI::Drm::V1_0::MediaKeySystemService> mediaKeySystem = mediaKeySystemMap_.begin()->first;
42         mediaKeySystemMutex_.unlock();
43         CloseMediaKeySystemService(mediaKeySystem);
44         mediaKeySystemMutex_.lock();
45     }
46     mediaKeySystemMutex_.unlock();
47     HDF_LOGI("%{public}s: end", __func__);
48 }
49 
IsMediaKeySystemSupported(const std::string & uuid,const std::string & mimeType,ContentProtectionLevel level,bool & isSupported)50 int32_t MediaKeySystemFactoryService::IsMediaKeySystemSupported(const std::string &uuid, const std::string &mimeType,
51     ContentProtectionLevel level, bool &isSupported)
52 {
53     HDF_LOGI("%{public}s: start", __func__);
54     if (IsClearPlayUuid(uuid) != true) {
55         isSupported = false;
56         HDF_LOGE("%{public}s: uuid is wrown", __func__);
57         return HDF_SUCCESS;
58     }
59     if (mimeType != "" && mimeType != ISO_VIDEO_MIME_TYPE && mimeType != ISO_AUDIO_MIME_TYPE &&
60         mimeType != CENC_INIT_DATA_FORMAT && mimeType != WEBM_INIT_DATA_FORMAT && mimeType != WEBM_AUDIO_DATA_FORMAT &&
61         mimeType != WEBM_VIDEO_DATA_FORMAT) {
62         isSupported = false;
63         return HDF_SUCCESS;
64     }
65     if (level < SECURE_UNKNOWN || level > HW_SECURE_MAX) {
66         isSupported = false;
67         return HDF_SUCCESS;
68     }
69     isSupported = true;
70     HDF_LOGI("%{public}s: end", __func__);
71     return HDF_SUCCESS;
72 }
73 
CreateMediaKeySystem(sptr<OHOS::HDI::Drm::V1_0::IMediaKeySystem> & mediaKeySystem)74 int32_t MediaKeySystemFactoryService::CreateMediaKeySystem(sptr<OHOS::HDI::Drm::V1_0::IMediaKeySystem> &mediaKeySystem)
75 {
76     HDF_LOGI("%{public}s: start", __func__);
77     sptr<MediaKeySystemService> newMediaKeySystem = new (std::nothrow) MediaKeySystemService();
78     if (newMediaKeySystem == nullptr) {
79         HDF_LOGE("new MediaKeySystemService() failed");
80         return HDF_ERR_MALLOC_FAIL;
81     }
82     newMediaKeySystem->SetKeySystemServiceCallback(this);
83     mediaKeySystemMap_[newMediaKeySystem] = true;
84     mediaKeySystem = newMediaKeySystem;
85     HDF_LOGI("%{public}s: end", __func__);
86     return HDF_SUCCESS;
87 }
88 
GetMediaKeySystemDescription(std::string & name,std::string & uuid)89 int32_t MediaKeySystemFactoryService::GetMediaKeySystemDescription(std::string &name, std::string &uuid)
90 {
91     HDF_LOGI("%{public}s: start", __func__);
92     name = CLEARPLAY_NAME;
93     uuid = CLEARPLAY_UUID;
94     HDF_LOGI("%{public}s: end", __func__);
95     return HDF_SUCCESS;
96 }
CloseMediaKeySystemService(sptr<MediaKeySystemService> mediaKeySystem)97 int32_t MediaKeySystemFactoryService::CloseMediaKeySystemService(sptr<MediaKeySystemService> mediaKeySystem)
98 {
99     HDF_LOGI("%{public}s: start", __func__);
100     mediaKeySystemMutex_.lock();
101     auto it = mediaKeySystemMap_.find(mediaKeySystem);
102     if (it == mediaKeySystemMap_.end()) {
103         mediaKeySystemMutex_.unlock();
104         return HDF_FAILURE;
105     }
106     mediaKeySystemMap_.erase(it);
107     mediaKeySystemMutex_.unlock();
108     HDF_LOGI("%{public}s: end", __func__);
109     return HDF_SUCCESS;
110 }
111 } // V1_0
112 } // Drm
113 } // HDI
114 } // OHOS
115