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 <thread> 19 #include <queue> 20 #include <cstring> 21 #include <map> 22 #include <unordered_map> 23 #include "nocopyable.h" 24 #include "ipc_skeleton.h" 25 #include "imedia_key_session_service.h" 26 #include "imedia_key_session_service_callback.h" 27 #include "drm_listener_stub.h" 28 #include "media_key_session_service_callback_stub.h" 29 #include "drm_death_recipient.h" 30 31 namespace OHOS { 32 namespace DrmStandard { 33 namespace MediaKeySessionEvent { 34 const std::string EVENT_STR_KEY_NEEDED = "keyRequired"; 35 const std::string EVENT_STR_KEY_EXPIRED = "keyExpired"; 36 const std::string EVENT_STR_EXPIRATION_UPDATED = "expirationUpdate"; 37 const std::string EVENT_STR_KEY_CHANGED = "keysChange"; 38 const std::string EVENT_STR_VENDOR_DEFINED = "vendorDefined"; 39 } 40 41 class MediaKeySessionImplCallback : public RefBase { 42 public: 43 MediaKeySessionImplCallback() = default; 44 virtual ~MediaKeySessionImplCallback() = default; 45 virtual void SendEvent(const std::string &event, int32_t extra, const std::vector<uint8_t> &data) = 0; 46 virtual void SendEventKeyChanged(std::map<std::vector<uint8_t>, MediaKeySessionKeyStatus> statusTable, 47 bool hasNewGoodLicense) = 0; 48 }; 49 50 class MediaKeySessionServiceCallback; 51 52 class MediaKeySessionImpl : public RefBase { 53 public: 54 explicit MediaKeySessionImpl(sptr<IMediaKeySessionService> &keySession); 55 ~MediaKeySessionImpl(); 56 int32_t Release(); 57 int32_t Init(); 58 59 int32_t GenerateMediaKeyRequest(MediaKeyRequestInfo &licenseRequestInfo, MediaKeyRequest &licenseRequest); 60 int32_t ProcessMediaKeyResponse(std::vector<uint8_t> &licenseId, std::vector<uint8_t> &licenseResponse); 61 int32_t GenerateOfflineReleaseRequest(std::vector<uint8_t> &licenseId, std::vector<uint8_t> &releaseRequest); 62 int32_t ProcessOfflineReleaseResponse(std::vector<uint8_t> &licenseId, std::vector<uint8_t> &releaseReponse); 63 int32_t CheckMediaKeyStatus(std::map<std::string, std::string> &licenseStatus); 64 int32_t RestoreOfflineMediaKeys(std::vector<uint8_t> &licenseId); 65 66 int32_t ClearMediaKeys(); 67 68 int32_t GetContentProtectionLevel(ContentProtectionLevel *securityLevel); 69 70 sptr<IMediaKeySessionService> GetMediaKeySessionServiceProxy(); 71 sptr<MediaKeySessionImplCallback> GetApplicationCallback(); 72 int32_t SetCallback(const sptr<MediaKeySessionImplCallback> &callback); 73 74 int32_t RequireSecureDecoderModule(std::string &mimeType, bool *status); 75 76 private: 77 void MediaKeySessionServerDied(pid_t pid); 78 sptr<MediaKeySessionImplCallback> keySessionApplicationCallback_; 79 sptr<MediaKeySessionServiceCallback> keySessionServiceCallback_; 80 sptr<OHOS::DrmStandard::IMediaKeySessionService> keySessionServiceProxy_; 81 std::recursive_mutex mutex_; 82 sptr<DrmDeathRecipient> deathRecipient_ = nullptr; 83 }; 84 85 struct KeySessionEventMessage { 86 enum MessageType { EventKeyChanged = 0, EventKey }; 87 MessageType messageType; 88 DrmEventType event; 89 int32_t extra; 90 std::vector<uint8_t> data; 91 std::map<std::vector<uint8_t>, MediaKeySessionKeyStatus> statusTable; 92 bool hasNewGoodLicense; 93 }; 94 95 class MediaKeySessionServiceCallback : public MediaKeySessionServiceCallbackStub { 96 public: MediaKeySessionServiceCallback()97 MediaKeySessionServiceCallback() : keySessionImpl_(nullptr) 98 { 99 InitEventMap(); 100 }; MediaKeySessionServiceCallback(MediaKeySessionImpl * keySessionImpl)101 explicit MediaKeySessionServiceCallback(MediaKeySessionImpl *keySessionImpl) 102 : keySessionImpl_(keySessionImpl) 103 { 104 InitEventMap(); 105 } 106 ~MediaKeySessionServiceCallback()107 ~MediaKeySessionServiceCallback() 108 { 109 Release(); 110 } 111 112 void InitEventMap(); 113 std::string GetEventName(DrmEventType event); 114 int32_t SendEvent(DrmEventType event, int32_t extra, const std::vector<uint8_t> &data) override; 115 int32_t SendEventKeyChanged(const std::map<std::vector<uint8_t>, MediaKeySessionKeyStatus> &statusTable, 116 bool hasNewGoodLicense) override; 117 void Release(); 118 void Init(); 119 private: 120 int32_t SendEventHandler(DrmEventType event, int32_t extra, const std::vector<uint8_t> &data); 121 int32_t SendEventKeyChangedHandler( 122 const std::map<std::vector<uint8_t>, MediaKeySessionKeyStatus> &statusTable, bool hasNewGoodLicense); 123 std::recursive_mutex mutex_; 124 MediaKeySessionImpl *keySessionImpl_; 125 std::unordered_map<int32_t, std::string> eventMap_; 126 bool serviceThreadRunning = false; 127 void ProcessEventMessage(); 128 std::thread eventQueueThread; 129 std::queue<KeySessionEventMessage> eventQueue; 130 std::mutex queueMutex; 131 std::condition_variable cv; 132 }; 133 } // DrmStandard 134 } // OHOS 135 136 137 #endif