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