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 "key_session_impl.h"
17 #include "drm_log.h"
18 #include "drm_error_code.h"
19 #include "drm_trace.h"
20
21 namespace OHOS {
22 namespace DrmStandard {
23
MediaKeySessionImpl(sptr<IMediaKeySessionService> & keySession)24 MediaKeySessionImpl::MediaKeySessionImpl(sptr<IMediaKeySessionService> &keySession)
25 : keySessionServiceCallback_(nullptr), keySessionServiceProxy_(keySession)
26 {
27 DRM_DEBUG_LOG("0x%{public}06" PRIXPTR "MediaKeySessionImpl Instances create",
28 FAKE_POINTER(this));
29
30 sptr<IRemoteObject> object = keySessionServiceProxy_->AsObject();
31 pid_t pid = 0;
32 deathRecipient_ = new(std::nothrow) DrmDeathRecipient(pid);
33 DRM_CHECK_AND_RETURN_LOG(deathRecipient_ != nullptr, "failed to new DrmDeathRecipient.");
34 deathRecipient_->SetNotifyCb([this] (pid_t pid) {
35 this->MediaKeySessionServerDied(pid);
36 });
37 bool result = object->AddDeathRecipient(deathRecipient_);
38 if (!result) {
39 DRM_ERR_LOG("failed to add deathRecipient");
40 return;
41 }
42 }
43
~MediaKeySessionImpl()44 MediaKeySessionImpl::~MediaKeySessionImpl()
45 {
46 DRM_INFO_LOG("~MediaKeySessionImpl enter.");
47 std::lock_guard<std::recursive_mutex> lock(mutex_);
48 keySessionServiceProxy_ = nullptr;
49 keySessionServiceCallback_ = nullptr;
50 DRM_DEBUG_LOG("0x%{public}06" PRIXPTR "MediaKeySessionImpl Instances release",
51 FAKE_POINTER(this));
52 }
53
MediaKeySessionServerDied(pid_t pid)54 void MediaKeySessionImpl::MediaKeySessionServerDied(pid_t pid)
55 {
56 DRM_ERR_LOG("MediaKeySession server has died, pid:%{public}d!", pid);
57 std::lock_guard<std::recursive_mutex> lock(mutex_);
58 if (keySessionServiceProxy_ != nullptr && keySessionServiceProxy_->AsObject() != nullptr
59 && deathRecipient_ != nullptr) {
60 (void)keySessionServiceProxy_->AsObject()->RemoveDeathRecipient(deathRecipient_);
61 keySessionServiceProxy_ = nullptr;
62 }
63 deathRecipient_ = nullptr;
64 }
65
Release()66 int32_t MediaKeySessionImpl::Release()
67 {
68 DRM_INFO_LOG("MediaKeySessionImpl Release enter.");
69 std::lock_guard<std::recursive_mutex> lock(mutex_);
70 int32_t ret = DRM_UNKNOWN_ERROR;
71 if (keySessionServiceProxy_ != nullptr) {
72 sptr<IRemoteObject> object = keySessionServiceProxy_->AsObject();
73 if (object != nullptr && deathRecipient_ != nullptr) {
74 object->RemoveDeathRecipient(deathRecipient_);
75 deathRecipient_ = nullptr;
76 }
77 ret = keySessionServiceProxy_->Release();
78 if (ret != DRM_OK) {
79 DRM_ERR_LOG("Failed to Release key session!, %{public}d", ret);
80 }
81 } else {
82 DRM_ERR_LOG("MediaKeySessionServiceProxy_ == nullptr");
83 }
84 keySessionServiceProxy_ = nullptr;
85 keySessionServiceCallback_ = nullptr;
86 return ret;
87 }
88
GenerateMediaKeyRequest(IMediaKeySessionService::MediaKeyRequestInfo & licenseRequestInfo,IMediaKeySessionService::MediaKeyRequest & licenseRequest)89 int32_t MediaKeySessionImpl::GenerateMediaKeyRequest(IMediaKeySessionService::MediaKeyRequestInfo &licenseRequestInfo,
90 IMediaKeySessionService::MediaKeyRequest &licenseRequest)
91 {
92 DrmTrace trace("GenerateMediaKeyRequest");
93 DRM_INFO_LOG("GenerateMediaKeyRequest enter.");
94 std::lock_guard<std::recursive_mutex> lock(mutex_);
95 int32_t retCode = DRM_OK;
96 if (keySessionServiceProxy_ == nullptr) {
97 DRM_ERR_LOG("GenerateMediaKeyRequest keySessionServiceProxy_ is null");
98 return DRM_SERVICE_ERROR;
99 }
100 retCode = keySessionServiceProxy_->GenerateMediaKeyRequest(licenseRequestInfo, licenseRequest);
101 if (retCode != DRM_OK) {
102 DRM_ERR_LOG("GenerateMediaKeyRequest failed, retCode: %{public}d", retCode);
103 return DRM_SERVICE_ERROR;
104 }
105 return DRM_OK;
106 }
107
ProcessMediaKeyResponse(std::vector<uint8_t> & licenseId,std::vector<uint8_t> & licenseResponse)108 int32_t MediaKeySessionImpl::ProcessMediaKeyResponse(std::vector<uint8_t> &licenseId,
109 std::vector<uint8_t> &licenseResponse)
110 {
111 DrmTrace trace("MediaKeySessionImpl::ProcessMediaKeyResponse");
112 DRM_INFO_LOG("ProcessMediaKeyResponse enter.");
113 std::lock_guard<std::recursive_mutex> lock(mutex_);
114 int32_t retCode = DRM_OK;
115
116 if (keySessionServiceProxy_ == nullptr) {
117 DRM_ERR_LOG("ProcessMediaKeyResponse keySessionServiceProxy_ is null");
118 return DRM_SERVICE_ERROR;
119 }
120 retCode = keySessionServiceProxy_->ProcessMediaKeyResponse(licenseId, licenseResponse);
121 if (retCode != DRM_OK) {
122 DRM_ERR_LOG("ProcessMediaKeyResponse failed, retCode: %{public}d", retCode);
123 return DRM_SERVICE_ERROR;
124 }
125 return DRM_OK;
126 }
127
GenerateOfflineReleaseRequest(std::vector<uint8_t> & licenseId,std::vector<uint8_t> & releaseRequest)128 int32_t MediaKeySessionImpl::GenerateOfflineReleaseRequest(std::vector<uint8_t> &licenseId,
129 std::vector<uint8_t> &releaseRequest)
130 {
131 DRM_INFO_LOG("GenerateOfflineReleaseRequest enter.");
132 std::lock_guard<std::recursive_mutex> lock(mutex_);
133 int32_t retCode = DRM_OK;
134 if (keySessionServiceProxy_ == nullptr) {
135 DRM_ERR_LOG("GenerateOfflineReleaseRequest keySessionServiceProxy_ is null");
136 return DRM_SERVICE_ERROR;
137 }
138
139 retCode = keySessionServiceProxy_->GenerateOfflineReleaseRequest(licenseId, releaseRequest);
140 if (retCode != DRM_OK) {
141 DRM_ERR_LOG("GenerateOfflineReleaseRequest failed, retCode: %{public}d", retCode);
142 return DRM_SERVICE_ERROR;
143 }
144 return DRM_OK;
145 }
146
ProcessOfflineReleaseResponse(std::vector<uint8_t> & licenseId,std::vector<uint8_t> & releaseResponse)147 int32_t MediaKeySessionImpl::ProcessOfflineReleaseResponse(std::vector<uint8_t> &licenseId,
148 std::vector<uint8_t> &releaseResponse)
149 {
150 DRM_INFO_LOG("ProcessOfflineReleaseResponse enter.");
151 std::lock_guard<std::recursive_mutex> lock(mutex_);
152 int32_t retCode = DRM_OK;
153
154 if (keySessionServiceProxy_ == nullptr) {
155 DRM_ERR_LOG("ProcessOfflineReleaseResponse keySessionServiceProxy_ is null");
156 return DRM_SERVICE_ERROR;
157 }
158 retCode = keySessionServiceProxy_->ProcessOfflineReleaseResponse(licenseId, releaseResponse);
159 if (retCode != DRM_OK) {
160 DRM_ERR_LOG("ProcessOfflineReleaseResponse failed, retCode: %{public}d", retCode);
161 return DRM_SERVICE_ERROR;
162 }
163 return DRM_OK;
164 }
165
GetContentProtectionLevel(IMediaKeySessionService::ContentProtectionLevel * securityLevel)166 int32_t MediaKeySessionImpl::GetContentProtectionLevel(IMediaKeySessionService::ContentProtectionLevel *securityLevel)
167 {
168 DRM_INFO_LOG("GetContentProtectionLevel enter.");
169 std::lock_guard<std::recursive_mutex> lock(mutex_);
170 int32_t retCode = DRM_OK;
171
172 if (keySessionServiceProxy_ == nullptr) {
173 DRM_ERR_LOG("GetContentProtectionLevel serviceProxy_ is null");
174 return DRM_SERVICE_ERROR;
175 }
176 retCode = keySessionServiceProxy_->GetContentProtectionLevel(
177 (IMediaKeySessionService::ContentProtectionLevel *)securityLevel);
178 if (retCode != DRM_OK) {
179 DRM_ERR_LOG("GetContentProtectionLevel failed, retCode: %{public}d", retCode);
180 return DRM_SERVICE_ERROR;
181 }
182 return DRM_OK;
183 }
184
CheckMediaKeyStatus(std::map<std::string,std::string> & licenseStatus)185 int32_t MediaKeySessionImpl::CheckMediaKeyStatus(std::map<std::string, std::string> &licenseStatus)
186 {
187 DRM_INFO_LOG("CheckMediaKeyStatus enter.");
188 std::lock_guard<std::recursive_mutex> lock(mutex_);
189 int32_t retCode = DRM_OK;
190
191 if (keySessionServiceProxy_ == nullptr) {
192 DRM_ERR_LOG("CheckMediaKeyStatus keySessionServiceProxy_ is null");
193 return DRM_SERVICE_ERROR;
194 }
195 retCode = keySessionServiceProxy_->CheckMediaKeyStatus(licenseStatus);
196 if (retCode != DRM_OK) {
197 DRM_ERR_LOG("CheckMediaKeyStatus failed, retCode: %{public}d", retCode);
198 return DRM_SERVICE_ERROR;
199 }
200 return DRM_OK;
201 }
202
RestoreOfflineMediaKeys(std::vector<uint8_t> & licenseId)203 int32_t MediaKeySessionImpl::RestoreOfflineMediaKeys(std::vector<uint8_t> &licenseId)
204 {
205 DRM_INFO_LOG("RestoreOfflineMediaKeys enter.");
206 std::lock_guard<std::recursive_mutex> lock(mutex_);
207 int32_t retCode = DRM_OK;
208
209 if (keySessionServiceProxy_ == nullptr) {
210 DRM_ERR_LOG("RestoreOfflineMediaKeys keySessionServiceProxy_ is null");
211 return DRM_SERVICE_ERROR;
212 }
213 retCode = keySessionServiceProxy_->RestoreOfflineMediaKeys(licenseId);
214 if (retCode != DRM_OK) {
215 DRM_ERR_LOG("RestoreOfflineMediaKeys failed, retCode: %{public}d", retCode);
216 return DRM_SERVICE_ERROR;
217 }
218 return DRM_OK;
219 }
220
ClearMediaKeys()221 int32_t MediaKeySessionImpl::ClearMediaKeys()
222 {
223 DRM_INFO_LOG("ClearMediaKeys enter.");
224 std::lock_guard<std::recursive_mutex> lock(mutex_);
225 int32_t retCode = DRM_OK;
226
227 if (keySessionServiceProxy_ == nullptr) {
228 DRM_ERR_LOG("ClearMediaKeys keySessionServiceProxy_ is null");
229 return DRM_SERVICE_ERROR;
230 }
231 retCode = keySessionServiceProxy_->ClearMediaKeys();
232 if (retCode != DRM_OK) {
233 DRM_ERR_LOG("ClearMediaKeys failed, retCode: %{public}d", retCode);
234 return DRM_SERVICE_ERROR;
235 }
236 return DRM_OK;
237 }
238
RequireSecureDecoderModule(std::string & mimeType,bool * status)239 int32_t MediaKeySessionImpl::RequireSecureDecoderModule(std::string &mimeType, bool *status)
240 {
241 DRM_INFO_LOG("RequireSecureDecoderModule enter.");
242 std::lock_guard<std::recursive_mutex> lock(mutex_);
243 int32_t retCode = DRM_OK;
244
245 if (keySessionServiceProxy_ == nullptr) {
246 DRM_ERR_LOG("RequireSecureDecoderModule keySessionServiceProxy_ is null");
247 return DRM_SERVICE_ERROR;
248 }
249 retCode = keySessionServiceProxy_->RequireSecureDecoderModule(mimeType, status);
250 if (retCode != DRM_OK) {
251 DRM_ERR_LOG("status: %{public}d", *status);
252 return retCode;
253 }
254 return DRM_OK;
255 }
256
GetMediaKeySessionServiceProxy()257 sptr<IMediaKeySessionService> MediaKeySessionImpl::GetMediaKeySessionServiceProxy()
258 {
259 DRM_INFO_LOG("GetMediaKeySessionServiceProxy enter.");
260 std::lock_guard<std::recursive_mutex> lock(mutex_);
261 if (keySessionServiceProxy_ != nullptr) {
262 DRM_DEBUG_LOG("MediaKeySessionImpl MediaKeySessionServiceProxy is not nullptr");
263 }
264 return keySessionServiceProxy_;
265 }
266
GetApplicationCallback()267 sptr<MediaKeySessionImplCallback> MediaKeySessionImpl::GetApplicationCallback()
268 {
269 DRM_INFO_LOG("GetApplicationCallback enter.");
270 std::lock_guard<std::recursive_mutex> lock(mutex_);
271 return keySessionApplicationCallback_;
272 }
273
SetCallback(const sptr<MediaKeySessionImplCallback> & callback)274 int32_t MediaKeySessionImpl::SetCallback(const sptr<MediaKeySessionImplCallback> &callback)
275 {
276 DRM_DEBUG_LOG("0x%{public}06" PRIXPTR " SetCallback in", FAKE_POINTER(this));
277 std::lock_guard<std::recursive_mutex> lock(mutex_);
278 DRM_CHECK_AND_RETURN_RET_LOG(callback != nullptr, DRM_INVALID_ARG, "callback is nullptr");
279 keySessionApplicationCallback_ = callback;
280
281 int32_t ret = DRM_ERROR;
282 keySessionServiceCallback_ = new (std::nothrow) MediaKeySessionServiceCallback(this);
283 if (keySessionServiceCallback_ == nullptr) {
284 DRM_ERR_LOG("MediaKeySessionServiceCallback alloc failed.");
285 return ret;
286 }
287
288 if (keySessionServiceProxy_ == nullptr) {
289 DRM_ERR_LOG("SetCallback keySessionServiceProxy_ is null");
290 return DRM_SERVICE_ERROR;
291 }
292 ret = keySessionServiceProxy_->SetCallback(keySessionServiceCallback_);
293 if (ret != DRM_OK) {
294 DRM_ERR_LOG("SetCallback failed, retCode: %{public}d", ret);
295 return DRM_SERVICE_ERROR;
296 }
297 return ret;
298 }
299
InitEventMap()300 void MediaKeySessionServiceCallback::InitEventMap()
301 {
302 DRM_INFO_LOG("InitEventMap enter");
303 std::lock_guard<std::recursive_mutex> lock(mutex_);
304 eventMap_[static_cast<int32_t>(DRM_EVENT_KEY_NEEDED)] = MediaKeySessionEvent::EVENT_STR_KEY_NEEDED;
305 eventMap_[static_cast<int32_t>(DRM_EVENT_KEY_EXPIRED)] = MediaKeySessionEvent::EVENT_STR_KEY_EXPIRED;
306 eventMap_[static_cast<int32_t>(DRM_EVENT_EXPIRATION_UPDATED)] = MediaKeySessionEvent::EVENT_STR_EXPIRATION_UPDATED;
307 eventMap_[static_cast<int32_t>(DRM_EVENT_KEY_CHANGED)] = MediaKeySessionEvent::EVENT_STR_KEY_CHANGED;
308 eventMap_[static_cast<int32_t>(DRM_EVENT_VENDOR_DEFINED)] = MediaKeySessionEvent::EVENT_STR_VENDOR_DEFINED;
309 }
310
GetEventName(DrmEventType event)311 std::string MediaKeySessionServiceCallback::GetEventName(DrmEventType event)
312 {
313 DRM_INFO_LOG("GetEventName enter");
314 std::string eventName = "";
315 std::lock_guard<std::recursive_mutex> lock(mutex_);
316 int32_t eventType = static_cast<int32_t>(event);
317 if (eventMap_.find(eventType) == eventMap_.end()) {
318 return eventName;
319 }
320 return eventMap_[eventType];
321 }
322
SendEvent(DrmEventType event,int32_t extra,const std::vector<uint8_t> & data)323 int32_t MediaKeySessionServiceCallback::SendEvent(DrmEventType event, int32_t extra, const std::vector<uint8_t> &data)
324 {
325 DRM_INFO_LOG("SendEvent enter");
326 std::string eventName = GetEventName(event);
327 std::lock_guard<std::recursive_mutex> lock(mutex_);
328 if (keySessionImpl_ != nullptr && eventName.length() != 0) {
329 sptr<MediaKeySessionImplCallback> applicationCallback = keySessionImpl_->GetApplicationCallback();
330 if (applicationCallback != nullptr) {
331 applicationCallback->SendEvent(eventName, extra, data);
332 return DRM_OK;
333 }
334 }
335 DRM_DEBUG_LOG("SendEvent failed.");
336 return DRM_ERROR;
337 }
338
SendEventKeyChanged(std::map<std::vector<uint8_t>,MediaKeySessionKeyStatus> statusTable,bool hasNewGoodLicense)339 int32_t MediaKeySessionServiceCallback::SendEventKeyChanged(
340 std::map<std::vector<uint8_t>, MediaKeySessionKeyStatus> statusTable, bool hasNewGoodLicense)
341 {
342 DRM_INFO_LOG("SendEventKeyChanged enter.");
343 std::lock_guard<std::recursive_mutex> lock(mutex_);
344 if (keySessionImpl_ != nullptr) {
345 sptr<MediaKeySessionImplCallback> callback = keySessionImpl_->GetApplicationCallback();
346 if (callback != nullptr) {
347 callback->SendEventKeyChanged(statusTable, hasNewGoodLicense);
348 return DRM_OK;
349 }
350 }
351 DRM_ERR_LOG("SendEventKeyChanged failed.");
352 return DRM_ERROR;
353 }
354 } // DrmStandard
355 } // OHOS