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 #ifndef OHOS_DRM_KEY_SESSION_IMPL_H_ 16 #define OHOS_DRM_KEY_SESSION_IMPL_H_ 17 18 #include <cstring> 19 #include <map> 20 #include <unordered_map> 21 #include "nocopyable.h" 22 #include "ipc_skeleton.h" 23 #include "i_keysession_service.h" 24 #include "i_keysession_service_callback.h" 25 #include "drm_listener_stub.h" 26 #include "key_session_service_callback_stub.h" 27 #include "drm_death_recipient.h" 28 29 namespace OHOS { 30 namespace DrmStandard { 31 namespace MediaKeySessionEvent { 32 const std::string EVENT_STR_KEY_NEEDED = "keyRequired"; 33 const std::string EVENT_STR_KEY_EXPIRED = "keyExpired"; 34 const std::string EVENT_STR_EXPIRATION_UPDATED = "expirationUpdate"; 35 const std::string EVENT_STR_KEY_CHANGED = "keysChange"; 36 const std::string EVENT_STR_VENDOR_DEFINED = "vendorDefined"; 37 } 38 39 class MediaKeySessionImplCallback : public RefBase { 40 public: 41 MediaKeySessionImplCallback() = default; 42 virtual ~MediaKeySessionImplCallback() = default; 43 virtual void SendEvent(const std::string &event, int32_t extra, const std::vector<uint8_t> &data) = 0; 44 virtual void SendEventKeyChanged(std::map<std::vector<uint8_t>, MediaKeySessionKeyStatus> statusTable, 45 bool hasNewGoodLicense) = 0; 46 }; 47 48 class MediaKeySessionImpl : public RefBase { 49 public: 50 explicit MediaKeySessionImpl(sptr<IMediaKeySessionService> &keySession); 51 ~MediaKeySessionImpl(); 52 int32_t Release(); 53 int32_t Init(); 54 55 int32_t GenerateMediaKeyRequest(IMediaKeySessionService::MediaKeyRequestInfo &licenseRequestInfo, 56 IMediaKeySessionService::MediaKeyRequest &licenseRequest); 57 int32_t ProcessMediaKeyResponse(std::vector<uint8_t> &licenseId, std::vector<uint8_t> &licenseResponse); 58 int32_t GenerateOfflineReleaseRequest(std::vector<uint8_t> &licenseId, std::vector<uint8_t> &releaseRequest); 59 int32_t ProcessOfflineReleaseResponse(std::vector<uint8_t> &licenseId, std::vector<uint8_t> &releaseReponse); 60 int32_t CheckMediaKeyStatus(std::map<std::string, std::string> &licenseStatus); 61 int32_t RestoreOfflineMediaKeys(std::vector<uint8_t> &licenseId); 62 63 int32_t ClearMediaKeys(); 64 65 int32_t GetContentProtectionLevel(IMediaKeySessionService::ContentProtectionLevel *securityLevel); 66 67 sptr<IMediaKeySessionService> GetMediaKeySessionServiceProxy(); 68 sptr<MediaKeySessionImplCallback> GetApplicationCallback(); 69 int32_t SetCallback(const sptr<MediaKeySessionImplCallback> &callback); 70 71 int32_t RequireSecureDecoderModule(std::string &mimeType, bool *status); 72 73 private: 74 void MediaKeySessionServerDied(pid_t pid); 75 sptr<MediaKeySessionImplCallback> keySessionApplicationCallback_; 76 sptr<IMediaKeySessionServiceCallback> keySessionServiceCallback_; 77 sptr<OHOS::DrmStandard::IMediaKeySessionService> keySessionServiceProxy_; 78 std::recursive_mutex mutex_; 79 sptr<DrmDeathRecipient> deathRecipient_ = nullptr; 80 }; 81 82 class MediaKeySessionServiceCallback : public MediaKeySessionServiceCallbackStub { 83 public: MediaKeySessionServiceCallback()84 MediaKeySessionServiceCallback() : keySessionImpl_(nullptr) 85 { 86 InitEventMap(); 87 }; MediaKeySessionServiceCallback(MediaKeySessionImpl * keySessionImpl)88 explicit MediaKeySessionServiceCallback(MediaKeySessionImpl *keySessionImpl) 89 : keySessionImpl_(keySessionImpl) 90 { 91 InitEventMap(); 92 } 93 ~MediaKeySessionServiceCallback()94 ~MediaKeySessionServiceCallback() 95 { 96 std::lock_guard<std::recursive_mutex> lock(mutex_); 97 keySessionImpl_ = nullptr; 98 } 99 100 void InitEventMap(); 101 std::string GetEventName(DrmEventType event); 102 int32_t SendEvent(DrmEventType event, int32_t extra, const std::vector<uint8_t> &data) override; 103 int32_t SendEventKeyChanged(std::map<std::vector<uint8_t>, MediaKeySessionKeyStatus> statusTable, 104 bool hasNewGoodLicense) override; 105 106 private: 107 std::recursive_mutex mutex_; 108 MediaKeySessionImpl *keySessionImpl_; 109 std::unordered_map<int32_t, std::string> eventMap_; 110 }; 111 } // DrmStandard 112 } // OHOS 113 114 115 #endif