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