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 #ifndef OHOS_DRM_MEDIA_KEY_SYSTEMP_IMPL_H 17 #define OHOS_DRM_MEDIA_KEY_SYSTEMP_IMPL_H 18 19 #include <thread> 20 #include <queue> 21 #include "ipc_skeleton.h" 22 #include "nocopyable.h" 23 #include "drm_log.h" 24 #include "drm_death_recipient.h" 25 #include "key_session_impl.h" 26 #include "drm_listener_stub.h" 27 #include "imedia_key_system_service.h" 28 #include "media_key_system_service_callback_stub.h" 29 30 namespace OHOS { 31 namespace DrmStandard { 32 namespace MediaKeySystemEvent { 33 const std::string EVENT_STR_PROVISION_REQUIRED = "keySystemRequired"; 34 } 35 class MediaKeySystemImplCallback : public RefBase { 36 public: 37 MediaKeySystemImplCallback() = default; 38 virtual ~MediaKeySystemImplCallback() = default; 39 virtual void SendEvent(const std::string &event, int32_t extra, const std::vector<uint8_t> &data) = 0; 40 }; 41 42 class MediaKeySystemCallback; 43 44 class MediaKeySystemImpl : public RefBase { 45 public: 46 explicit MediaKeySystemImpl(sptr<IMediaKeySystemService> &mediaKeysystem); 47 ~MediaKeySystemImpl(); 48 49 int32_t SetConfigurationString(std::string &configName, std::string &value); 50 int32_t GetConfigurationString(std::string &configName, std::string &value); 51 int32_t SetConfigurationByteArray(std::string &configName, std::vector<uint8_t> &value); 52 int32_t GetConfigurationByteArray(std::string &configName, std::vector<uint8_t> &value); 53 int32_t CreateMediaKeySession(ContentProtectionLevel securityLevel, 54 sptr<MediaKeySessionImpl> *keySessionImpl); 55 int32_t GetStatistics(std::vector<MetircKeyValue> &metrics); 56 int32_t GetMaxContentProtectionLevel(ContentProtectionLevel *securityLevel); 57 58 int32_t GenerateKeySystemRequest(std::vector<uint8_t> &request, std::string &defaultUrl); 59 int32_t ProcessKeySystemResponse(const std::vector<uint8_t> &response); 60 61 int32_t GetOfflineMediaKeyIds(std::vector<std::vector<uint8_t>> &licenseIds); 62 int32_t GetOfflineMediaKeyStatus(std::vector<uint8_t> &licenseId, 63 OfflineMediaKeyStatus &status); 64 int32_t ClearOfflineMediaKeys(std::vector<uint8_t> &licenseId); 65 66 int32_t GetCertificateStatus(CertificateStatus *certStatus); 67 sptr<MediaKeySystemImplCallback> GetApplicationCallback(); 68 int32_t SetCallback(const sptr<MediaKeySystemImplCallback> &callback); 69 int32_t Release(); 70 71 private: 72 void MediaKeySystemServerDied(pid_t pid); 73 std::recursive_mutex mutex_; 74 sptr<IMediaKeySystemService> serviceProxy_; 75 sptr<MediaKeySystemImplCallback> mediaKeySystemApplicationCallback_; 76 sptr<MediaKeySystemCallback> serviceCallback_; 77 sptr<DrmDeathRecipient> deathRecipient_ = nullptr; 78 }; 79 80 struct MediaKeySystemEventMessage { 81 DrmEventType event; 82 int32_t extra; 83 std::vector<uint8_t> data; MediaKeySystemEventMessageMediaKeySystemEventMessage84 MediaKeySystemEventMessage(DrmEventType t, int32_t e, std::vector<uint8_t> d) : event(t), extra(e), data(d) 85 {} 86 }; 87 88 class MediaKeySystemCallback : public MediaKeySystemServiceCallbackStub { 89 public: MediaKeySystemCallback()90 MediaKeySystemCallback() : systemImpl_(nullptr) 91 { 92 InitEventMap(); 93 }; MediaKeySystemCallback(MediaKeySystemImpl * systemImpl)94 explicit MediaKeySystemCallback(MediaKeySystemImpl *systemImpl) : systemImpl_(systemImpl) 95 { 96 InitEventMap(); 97 }; 98 ~MediaKeySystemCallback(); 99 void InitEventMap(); 100 std::string GetEventName(DrmEventType event); 101 int32_t SendEvent(DrmEventType event, int32_t extra, const std::vector<uint8_t> &data) override; 102 void Release(); 103 void Init(); 104 private: 105 int32_t SendEventHandler(DrmEventType event, int32_t extra, const std::vector<uint8_t> &data); 106 std::recursive_mutex mutex_; 107 MediaKeySystemImpl *systemImpl_; 108 std::unordered_map<int32_t, std::string> eventMap_; 109 bool serviceThreadRunning = false; 110 void ProcessEventMessage(); 111 std::thread eventQueueThread; 112 std::queue<MediaKeySystemEventMessage> eventQueue; 113 std::mutex queueMutex; 114 std::condition_variable cv; 115 }; 116 } // DrmStandard 117 } // OHOS 118 #endif // OHOS_DRM_MEDIA_KEY_SYSTEMP_IMPL_H 119