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 "imedia_key_system_service.h"
18 #include "drm_error_code.h"
19 #include "napi_param_utils.h"
20 #include "iservice_registry.h"
21 #include "system_ability_definition.h"
22
23 namespace OHOS {
24 namespace DrmStandard {
25 using namespace OHOS::HiviewDFX;
26
27 sptr<MediaKeySystemFactoryImpl> MediaKeySystemFactoryImpl::mediaKeySystemFactoryImpl_ = nullptr;
28 constexpr int64_t SLEEP_TIME = 1;
29 constexpr int32_t RETRY_TIMES = 3;
30
MediaKeySystemFactoryImpl()31 MediaKeySystemFactoryImpl::MediaKeySystemFactoryImpl()
32 {
33 DRM_DEBUG_LOG("0x%{public}06" PRIXPTR "MediaKeySystemFactoryImpl Instances create",
34 FAKE_POINTER(this));
35 traceId_ = HiTraceChain::Begin("MediaKeySystemFactory", HITRACE_FLAG_DEFAULT);
36 Init();
37 }
38
~MediaKeySystemFactoryImpl()39 MediaKeySystemFactoryImpl::~MediaKeySystemFactoryImpl()
40 {
41 DRM_INFO_LOG("~MediaKeySystemFactoryImpl enter.");
42 HiTraceChain::End(traceId_);
43 deathRecipient_ = nullptr;
44 }
45
GetServiceProxy()46 const sptr<IMediaKeySystemFactoryService> MediaKeySystemFactoryImpl::GetServiceProxy()
47 {
48 std::lock_guard<std::mutex> lock(serviceProxyMutex_);
49 if (privateServiceProxy_ != nullptr) {
50 return privateServiceProxy_;
51 }
52
53 DRM_INFO_LOG("Connect media key system service.");
54 sptr<IRemoteObject> object = nullptr;
55 auto samgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
56 DRM_CHECK_AND_RETURN_RET_LOG(samgr != nullptr, nullptr,
57 "Failed to get System ability manager!");
58 object = samgr->CheckSystemAbility(MEDIA_KEY_SYSTEM_SERVICE_ID);
59 if (object == nullptr) {
60 object = samgr->LoadSystemAbility(MEDIA_KEY_SYSTEM_SERVICE_ID, 30); // 30: timeout
61 }
62 DRM_CHECK_AND_RETURN_RET_LOG(object != nullptr, nullptr,
63 "the object returned by GetSystemAbility is nullptr!");
64 sptr<IMediaKeySystemFactoryService> tmpProxy = iface_cast<IMediaKeySystemFactoryService>(object);
65 DRM_CHECK_AND_RETURN_RET_LOG(tmpProxy != nullptr, nullptr, "cast the object to privateServiceProxy_ failed!");
66
67 pid_t pid = 0;
68 sptr<DrmDeathRecipient> deathRecipient = new (std::nothrow) DrmDeathRecipient(pid);
69 DRM_CHECK_AND_RETURN_RET_LOG(deathRecipient != nullptr, nullptr,
70 "failed to new DrmDeathRecipient!");
71 deathRecipient->SetNotifyCb([this] (pid_t pid) {
72 this->MediaKeySystemFactoryServerDied(pid);
73 });
74 bool result = object->AddDeathRecipient(deathRecipient);
75 DRM_CHECK_AND_RETURN_RET_LOG(result, nullptr, "failed to new DrmDeathRecipient!");
76
77 DRM_INFO_LOG("Ready to create listener object");
78 sptr<DrmListenerStub> tmpListenerStub = new(std::nothrow) DrmListenerStub();
79 DRM_CHECK_AND_RETURN_RET_LOG(tmpListenerStub != nullptr, nullptr, "failed to new DrmListenerStub object");
80
81 sptr<IRemoteObject> listenerObject = tmpListenerStub->AsObject();
82 DRM_CHECK_AND_RETURN_RET_LOG(listenerObject != nullptr, nullptr, "listener object is nullptr.");
83 int32_t ret = tmpProxy->SetListenerObject(listenerObject);
84 DRM_CHECK_AND_RETURN_RET_LOG(ret == DRM_INNER_ERR_OK, nullptr, "set listener failed.");
85 privateServiceProxy_ = tmpProxy;
86 deathRecipient_ = deathRecipient;
87 listenerStub_ = tmpListenerStub;
88 return privateServiceProxy_;
89 }
90
GetInstance()91 sptr<MediaKeySystemFactoryImpl> &MediaKeySystemFactoryImpl::GetInstance()
92 {
93 DRM_INFO_LOG("GetInstance enter.");
94 if (MediaKeySystemFactoryImpl::mediaKeySystemFactoryImpl_ == nullptr) {
95 DRM_DEBUG_LOG("Initializing MediaKeySystemFactoryImpl for first time!");
96 MediaKeySystemFactoryImpl::mediaKeySystemFactoryImpl_ = new (std::nothrow) MediaKeySystemFactoryImpl();
97 if (MediaKeySystemFactoryImpl::mediaKeySystemFactoryImpl_ == nullptr) {
98 DRM_ERR_LOG("GetInstance failed to new MediaKeySystemFactoryImpl");
99 }
100 }
101 return MediaKeySystemFactoryImpl::mediaKeySystemFactoryImpl_;
102 }
103
Init()104 void MediaKeySystemFactoryImpl::Init()
105 {
106 DRM_INFO_LOG("Init enter.");
107 HiTraceChain::SetId(traceId_);
108 }
109
MediaKeySystemFactoryServerDied(pid_t pid)110 void MediaKeySystemFactoryImpl::MediaKeySystemFactoryServerDied(pid_t pid)
111 {
112 DRM_ERR_LOG("MediaKeySystemFactoryServerDied has died, pid:%{public}d!", pid);
113 {
114 std::lock_guard<std::mutex> lock(serviceProxyMutex_);
115 if (privateServiceProxy_ != nullptr && privateServiceProxy_->AsObject() != nullptr) {
116 (void)privateServiceProxy_->AsObject()->RemoveDeathRecipient(deathRecipient_);
117 privateServiceProxy_ = nullptr;
118 }
119 listenerStub_ = nullptr;
120 deathRecipient_ = nullptr;
121 }
122
123 int32_t retry = RETRY_TIMES;
124 sptr<IMediaKeySystemFactoryService> serviceProxy = nullptr;
125 while (retry--) {
126 // Sleep and wait for 1 second;
127 sleep(SLEEP_TIME);
128 serviceProxy = GetServiceProxy();
129 if (serviceProxy != nullptr) {
130 DRM_INFO_LOG("Reconnect media key system service success!");
131 break;
132 }
133 }
134
135 DRM_CHECK_AND_RETURN_LOG(serviceProxy != nullptr, "failed to reconnect service!");
136 }
137
IsMediaKeySystemSupported(std::string & name)138 bool MediaKeySystemFactoryImpl::IsMediaKeySystemSupported(std::string &name)
139 {
140 DRM_INFO_LOG("IsMediaKeySystemSupported enter.");
141 int32_t ret = DRM_INNER_ERR_OK;
142 bool isSupported = false;
143 const sptr<IMediaKeySystemFactoryService> serviceProxy = GetServiceProxy();
144 DRM_CHECK_AND_RETURN_RET_LOG(serviceProxy != nullptr, isSupported, "service proxy is nullptr!");
145 ret = serviceProxy->IsMediaKeySystemSupported(name, isSupported);
146 if (ret != DRM_INNER_ERR_OK) {
147 DRM_ERR_LOG("IsMediaKeySystemSupported failed, ret: %{public}d", ret);
148 }
149 return isSupported;
150 }
151
IsMediaKeySystemSupported(std::string & name,std::string & mimeType)152 bool MediaKeySystemFactoryImpl::IsMediaKeySystemSupported(std::string &name, std::string &mimeType)
153 {
154 DRM_INFO_LOG("IsMediaKeySystemSupported enter.");
155 int32_t ret = DRM_INNER_ERR_OK;
156 bool isSupported = false;
157
158 const sptr<IMediaKeySystemFactoryService> serviceProxy = GetServiceProxy();
159 DRM_CHECK_AND_RETURN_RET_LOG(serviceProxy != nullptr, isSupported, "service proxy is nullptr!");
160 ret = serviceProxy->IsMediaKeySystemSupported(name, mimeType, isSupported);
161 if (ret != DRM_INNER_ERR_OK) {
162 DRM_ERR_LOG("IsMediaKeySystemSupported failed, ret: %{public}d", ret);
163 }
164 return isSupported;
165 }
166
IsMediaKeySystemSupported(std::string & uuid,std::string & mimeType,ContentProtectionLevel securityLevel)167 bool MediaKeySystemFactoryImpl::IsMediaKeySystemSupported(std::string &uuid, std::string &mimeType,
168 ContentProtectionLevel securityLevel)
169 {
170 DRM_INFO_LOG("IsMediaKeySystemSupported enter.");
171 int32_t ret = DRM_INNER_ERR_OK;
172 bool isSupported = false;
173
174 const sptr<IMediaKeySystemFactoryService> serviceProxy = GetServiceProxy();
175 DRM_CHECK_AND_RETURN_RET_LOG(serviceProxy != nullptr, isSupported, "service proxy is nullptr!");
176 ret = serviceProxy->IsMediaKeySystemSupported(uuid, mimeType, static_cast<int32_t>(securityLevel), isSupported);
177 if (ret != DRM_INNER_ERR_OK) {
178 DRM_ERR_LOG("IsMediaKeySystemSupported failed, ret: %{public}d", ret);
179 }
180 return isSupported;
181 }
182
GetMediaKeySystems(std::map<std::string,std::string> & keySystemNames)183 int32_t MediaKeySystemFactoryImpl::GetMediaKeySystems(std::map<std::string, std::string> &keySystemNames)
184 {
185 DRM_INFO_LOG("GetMediaKeySystems enter.");
186 int32_t ret = DRM_INNER_ERR_OK;
187 const sptr<IMediaKeySystemFactoryService> serviceProxy = GetServiceProxy();
188 DRM_CHECK_AND_RETURN_RET_LOG(serviceProxy != nullptr, DRM_INNER_ERR_BASE, "service proxy is nullptr!");
189 ret = serviceProxy->GetMediaKeySystems(keySystemNames);
190 if (ret != DRM_INNER_ERR_OK) {
191 DRM_ERR_LOG("GetMediaKeySystems failed, ret: %{public}d", ret);
192 return ret;
193 }
194 return DRM_INNER_ERR_OK;
195 }
196
GetMediaKeySystemUuid(std::string & name,std::string & uuid)197 int32_t MediaKeySystemFactoryImpl::GetMediaKeySystemUuid(std::string &name, std::string &uuid)
198 {
199 DRM_INFO_LOG("GetMediaKeySystemUuid enter.");
200 int32_t ret = DRM_INNER_ERR_OK;
201 const sptr<IMediaKeySystemFactoryService> serviceProxy = GetServiceProxy();
202 DRM_CHECK_AND_RETURN_RET_LOG(serviceProxy != nullptr, DRM_INNER_ERR_BASE, "service proxy is nullptr!");
203 ret = serviceProxy->GetMediaKeySystemUuid(name, uuid);
204 if (ret != DRM_INNER_ERR_OK) {
205 DRM_ERR_LOG("GetMediaKeySystemUuid failed, ret: %{public}d", ret);
206 return ret;
207 }
208 return DRM_INNER_ERR_OK;
209 }
210
CreateMediaKeySystem(std::string & name,sptr<MediaKeySystemImpl> * mediaKeySystemImpl)211 int32_t MediaKeySystemFactoryImpl::CreateMediaKeySystem(std::string &name, sptr<MediaKeySystemImpl> *mediaKeySystemImpl)
212 {
213 DRM_INFO_LOG("CreateMediaKeySystem enter.");
214 sptr<IMediaKeySystemService> mediaKeySystemProxy = nullptr;
215 sptr<MediaKeySystemImpl> localMediaKeySystemImpl = nullptr;
216 int32_t ret = DRM_INNER_ERR_OK;
217 if (mediaKeySystemImpl == nullptr) {
218 DRM_ERR_LOG("mediaKeySystemImpl is nullptr");
219 return DRM_INNER_ERR_INVALID_VAL;
220 }
221
222 const sptr<IMediaKeySystemFactoryService> serviceProxy = GetServiceProxy();
223 DRM_CHECK_AND_RETURN_RET_LOG(serviceProxy != nullptr, DRM_INNER_ERR_BASE, "service proxy is nullptr!");
224 ret = serviceProxy->CreateMediaKeySystem(name, mediaKeySystemProxy);
225 if (ret == DRM_INNER_ERR_OK) {
226 if (mediaKeySystemProxy != nullptr) {
227 localMediaKeySystemImpl = new (std::nothrow) MediaKeySystemImpl(mediaKeySystemProxy);
228 if (localMediaKeySystemImpl == nullptr) {
229 DRM_ERR_LOG("Failed to new MediaKeySystemImpl");
230 return DRM_INNER_ERR_SERVICE_FATAL_ERROR;
231 }
232 } else {
233 DRM_ERR_LOG("mediaKeySystemProxy is nullptr");
234 return DRM_INNER_ERR_UNKNOWN;
235 }
236 } else if (ret == DRM_INNER_ERR_MAX_SYSTEM_NUM_REACHED) {
237 DRM_ERR_LOG("The number of MediaKeySystem is greater than 64");
238 return DRM_INNER_ERR_MAX_SYSTEM_NUM_REACHED;
239 } else {
240 DRM_ERR_LOG("Service faltal error");
241 return DRM_INNER_ERR_SERVICE_FATAL_ERROR;
242 }
243 *mediaKeySystemImpl = localMediaKeySystemImpl;
244 return DRM_INNER_ERR_OK;
245 }
246 } // namespace DrmStandard
247 } // namespace OHOS