• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "media_key_system_factory_impl.h"
17 #include "i_mediakeysystem_service.h"
18 #include "drm_error_code.h"
19 #include "napi_param_utils.h"
20 
21 namespace OHOS {
22 namespace DrmStandard {
23 sptr<MediaKeySystemFactoryImpl> MediaKeySystemFactoryImpl::mediaKeySystemFactoryImpl_;
MediaKeySystemFactoryImpl()24 MediaKeySystemFactoryImpl::MediaKeySystemFactoryImpl()
25 {
26     DRM_DEBUG_LOG("MediaKeySystemFactoryImpl:0x%{public}06" PRIXPTR "MediaKeySystemFactoryImpl Instances create",
27         FAKE_POINTER(this));
28     Init();
29 }
30 
~MediaKeySystemFactoryImpl()31 MediaKeySystemFactoryImpl::~MediaKeySystemFactoryImpl()
32 {
33     DRM_INFO_LOG("MediaKeySystemFactoryImpl::~MediaKeySystemFactoryImpl enter.");
34     serviceProxy_ = nullptr;
35     DRM_INFO_LOG("MediaKeySystemFactoryImpl::~MediaKeySystemFactoryImpl exit.");
36     deathRecipient_ = nullptr;
37 }
38 
GetInstance()39 sptr<MediaKeySystemFactoryImpl> &MediaKeySystemFactoryImpl::GetInstance()
40 {
41     DRM_INFO_LOG("MediaKeySystemFactoryImpl::GetInstance enter.");
42     if (MediaKeySystemFactoryImpl::mediaKeySystemFactoryImpl_ == nullptr) {
43         DRM_DEBUG_LOG("Initializing MediaKeySystemFactoryImpl for first time!");
44         MediaKeySystemFactoryImpl::mediaKeySystemFactoryImpl_ = new (std::nothrow) MediaKeySystemFactoryImpl();
45         if (MediaKeySystemFactoryImpl::mediaKeySystemFactoryImpl_ == nullptr) {
46             DRM_ERR_LOG("MediaKeySystemFactoryImpl::GetInstance failed to new MediaKeySystemFactoryImpl");
47         }
48     }
49     DRM_INFO_LOG("MediaKeySystemFactoryImpl::GetInstance exit.");
50     return MediaKeySystemFactoryImpl::mediaKeySystemFactoryImpl_;
51 }
52 
Init()53 void MediaKeySystemFactoryImpl::Init()
54 {
55     DRM_INFO_LOG("MediaKeySystemFactoryImpl::Init enter.");
56     sptr<IRemoteObject> object = nullptr;
57 
58     auto samgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
59     if (samgr == nullptr) {
60         DRM_ERR_LOG("Failed to get System ability manager");
61         return;
62     }
63     object = samgr->GetSystemAbility(MEDIA_KEY_SYSTEM_SERVICE_ID);
64     if (object == nullptr) {
65         DRM_ERR_LOG("MediaKeySystemFactoryImpl::GetSystemAbility() is failed");
66         return;
67     }
68     serviceProxy_ = iface_cast<IMediaKeySystemFactoryService>(object);
69     if (serviceProxy_ == nullptr) {
70         DRM_ERR_LOG("MediaKeySystemFactoryImpl::init serviceProxy_ is null.");
71         return;
72     }
73     pid_t pid = 0;
74     deathRecipient_ = new (std::nothrow) DrmDeathRecipient(pid);
75 
76     deathRecipient_->SetNotifyCb(
77         std::bind(&MediaKeySystemFactoryImpl::MediaKeySystemServerDied, this, std::placeholders::_1));
78     bool result = object->AddDeathRecipient(deathRecipient_);
79     if (!result) {
80         DRM_ERR_LOG("failed to add deathRecipient");
81         return;
82     }
83     DRM_INFO_LOG("MediaKeySystemFactoryImpl::Init exit.");
84 }
85 
MediaKeySystemServerDied(pid_t pid)86 void MediaKeySystemFactoryImpl::MediaKeySystemServerDied(pid_t pid)
87 {
88     DRM_ERR_LOG("MediaKeySystemServer has died, pid:%{public}d!", pid);
89     if (serviceProxy_ != nullptr) {
90         (void)serviceProxy_->AsObject()->RemoveDeathRecipient(deathRecipient_);
91         serviceProxy_ = nullptr;
92     }
93     deathRecipient_ = nullptr;
94 }
95 
IsMediaKeySystemSupported(std::string & uuid)96 bool MediaKeySystemFactoryImpl::IsMediaKeySystemSupported(std::string &uuid)
97 {
98     DRM_INFO_LOG("MediaKeySystemFactoryImpl::IsMediaKeySystemSupported enter.");
99     std::lock_guard<std::mutex> lock(mutex_);
100     int32_t ret = DRM_OK;
101     bool isSurpported = false;
102     if (serviceProxy_ == nullptr) {
103         DRM_ERR_LOG("MediaKeySystemFactoryImpl::IsMediaKeySystemSupported serviceProxy_ is null");
104         return isSurpported;
105     }
106     ret = serviceProxy_->IsMediaKeySystemSupported(uuid, &isSurpported);
107     if (ret != DRM_OK) {
108         DRM_ERR_LOG("MediaKeySystemFactoryImpl::IsMediaKeySystemSupported failed, ret: %{public}d", ret);
109     }
110     DRM_INFO_LOG("MediaKeySystemFactoryImpl::IsMediaKeySystemSupported exit.");
111 
112     return isSurpported;
113 }
114 
IsMediaKeySystemSupported(std::string & uuid,std::string & mimeType)115 bool MediaKeySystemFactoryImpl::IsMediaKeySystemSupported(std::string &uuid, std::string &mimeType)
116 {
117     DRM_INFO_LOG("MediaKeySystemFactoryImpl::IsMediaKeySystemSupported enter.");
118     std::lock_guard<std::mutex> lock(mutex_);
119     int32_t ret = DRM_OK;
120     bool isSurpported = false;
121 
122     if (serviceProxy_ == nullptr) {
123         DRM_ERR_LOG("MediaKeySystemFactoryImpl::IsMediaKeySystemSupported serviceProxy_ is null");
124         return isSurpported;
125     }
126     ret = serviceProxy_->IsMediaKeySystemSupported(uuid, mimeType, &isSurpported);
127     if (ret != DRM_OK) {
128         DRM_ERR_LOG("MediaKeySystemFactoryImpl::IsMediaKeySystemSupported failed, ret: %{public}d", ret);
129     }
130     DRM_INFO_LOG("MediaKeySystemFactoryImpl::IsMediaKeySystemSupported exit.");
131 
132     return isSurpported;
133 }
134 
IsMediaKeySystemSupported(std::string & uuid,std::string & mimeType,IMediaKeySessionService::ContentProtectionLevel securityLevel)135 bool MediaKeySystemFactoryImpl::IsMediaKeySystemSupported(std::string &uuid, std::string &mimeType,
136     IMediaKeySessionService::ContentProtectionLevel securityLevel)
137 {
138     DRM_INFO_LOG("MediaKeySystemFactoryImpl::IsMediaKeySystemSupported enter.");
139     std::lock_guard<std::mutex> lock(mutex_);
140     int32_t ret = DRM_OK;
141     bool isSurpported = false;
142 
143     if (serviceProxy_ == nullptr) {
144         DRM_ERR_LOG("MediaKeySystemFactoryImpl::IsMediaKeySystemSupported serviceProxy_ is null");
145         return isSurpported;
146     }
147     ret = serviceProxy_->IsMediaKeySystemSupported(uuid, mimeType, securityLevel, &isSurpported);
148     if (ret != DRM_OK) {
149         DRM_ERR_LOG("MediaKeySystemFactoryImpl::IsMediaKeySystemSupported failed, ret: %{public}d", ret);
150     }
151     DRM_INFO_LOG("MediaKeySystemFactoryImpl::IsMediaKeySystemSupported exit.");
152 
153     return isSurpported;
154 }
155 
CreateMediaKeySystem(std::string & uuid,sptr<MediaKeySystemImpl> * mediaKeySystemImpl)156 int32_t MediaKeySystemFactoryImpl::CreateMediaKeySystem(std::string &uuid, sptr<MediaKeySystemImpl> *mediaKeySystemImpl)
157 {
158     DRM_INFO_LOG("MediaKeySystemFactoryImpl:: CreateMediaKeySystem enter.");
159     sptr<IMediaKeySystemService> mediaKeySystemProxy = nullptr;
160     sptr<MediaKeySystemImpl> localMediaKeySystemImpl = nullptr;
161     int32_t ret = DRM_OK;
162     if (mediaKeySystemImpl == nullptr) {
163         DRM_ERR_LOG("MediaKeySystemImpl:: mediaKeySystemImpl is nullptr");
164         return DRM_INVALID_PARAM;
165     }
166     if (serviceProxy_ == nullptr) {
167         DRM_ERR_LOG("MediaKeySystemFactoryImpl:: serviceProxy_ == nullptr");
168         return DRM_SERVICE_FATAL_ERROR;
169     }
170 
171     ret = serviceProxy_->CreateMediaKeySystem(uuid, mediaKeySystemProxy);
172     if (ret == DRM_OK) {
173         if (mediaKeySystemProxy != nullptr) {
174             localMediaKeySystemImpl = new (std::nothrow) MediaKeySystemImpl(mediaKeySystemProxy);
175             if (localMediaKeySystemImpl == nullptr) {
176                 DRM_ERR_LOG("Failed to new MediaKeySystemImpl");
177                 return DRM_SERVICE_FATAL_ERROR;
178             }
179             keySystemNumber++;
180             if (keySystemNumber > KEY_SYSTEM_MAX_NUMBER) {
181                 keySystemNumber--;
182                 DRM_ERR_LOG("The number of MediaKeySystem is greater than 64");
183                 return DRM_MAX_SYSTEM_NUM_REACHED;
184             }
185         } else {
186             DRM_ERR_LOG("mediaKeySystemProxy is nullptr");
187             return DRM_UNKNOWN_ERROR;
188         }
189     } else {
190         DRM_ERR_LOG("Failed to get session object from mediakeysystem service!, %{public}d", ret);
191         return DRM_SERVICE_FATAL_ERROR;
192     }
193     *mediaKeySystemImpl = localMediaKeySystemImpl;
194     DRM_INFO_LOG("MediaKeySystemFactoryImpl:: CreateMediaKeySystem exit.");
195     return DRM_OK;
196 }
197 } // namespace DrmStandard
198 } // namespace OHOS